Tuesday, 14 May 2013

Saving Array to file Using Xcode.


hi all.

hereunder there code of program generates ascii codes 0-9 , a-z serial numbers. example generates line this:
abcde-fghij-klmn0-12345-67899

want have 50,000 serial numbers generated , saved single file output.txt

first made code , saved 1 last 1 edited code (as shown below) opens file , adds new line serials saved not working. can please me out procedure.
code follows:
code:
#include <iostream>  #include <time.h>  #include <stdlib.h>  #include <fstream>    void savearray (char* array, int length);    int main (int argc, const char * argv[])  {      int n, l, ln;      int ascii[25] = {0};      char serial[25] = {0};            srand((unsigned)time(0));            for(int z=0; z<50000; z++)      {          (int =0; < 25; i++)          {              ln = (rand()%2);                    if (ln == 0)              {                  n = (rand()%10)+48;                  ascii[i] = n;              }              else if (ln == 1)              {                  l = (rand()%26)+65;                  ascii[i] = l;              }                        serial[i] = static_cast<char>(ascii[i]);          }          savearray(serial, 25);        }        }    void savearray (char* array, int length)  {      std::fstream filestr;      std::ofstream output ("/users/manuelportelli/documents/c++ projects/serial_generator/serial_generator/ouput.txt");            (int j=0; j<length; j++)      {                    //for (int k=0; k<5; k++)         // {              filestr.open ("/users/manuelportelli/documents/c++ projects/serial_generator/serial_generator/ouput.txt", std::fstream::in | std::fstream::out | std::fstream::app);           filestr<<array[j]<<std::endl;                        //}      }                          }
thanks & regards
combinu
 

code:
 #include <ctime> #include <fstream> #include <iostream>  const size_t    count_of_serial_nums  = 50000; const size_t    length_segment * * * *    = 5; void generatenumericseg(std::string& str, size_t count) {     while ( count-- ) { str += ('0' + (rand() % (('9' - '0') + 1))); } }  void generatealphaseg(std::string& str, size_t count) {     while ( count-- ) { str += ('a' + (rand() % (('z' - 'a') + 1))); } }  int main(int argc, const char* argv[]) {     using std::string;     using std::ofstream;          srand(time(null));      ofstream   ofs("ouput.txt");     if ( ofs.is_open() )     {         // example: abcde-fghij-klmn0-12345-67899         string  str;         size_t  sn = count_of_serial_nums;         while ( sn-- )         {             str.clear();              generatealphaseg(str, length_segment);   str += '-';             generatealphaseg(str, length_segment);   str += '-';             generatealphaseg(str, length_segment);   str += '-';             generatenumericseg(str, length_segment); str += '-';             generatenumericseg(str, length_segment); str += '\n';              ofs << str;         }          ofs.close();     } } 
 


Forums Macs Mac Programming


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

No comments:

Post a Comment