By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,128 Members | 1,034 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,128 IT Pros & Developers. It's quick & easy.

output from Files

P: 9
Hi!!!!
I have some results in a results.txt file like this

#-50#-49#-48#-47#-46#-45#-44#-43#-42#-41#-40#-39#-38#-
-9#-8#-7#-6#-5#-4#-3#-2#-1#0#1#2#3#4#5#6#7#8#9#10#11#
12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#27#
97#98#99#100#101#102#103#104#105#106#107#108#109#

and i want to put these results in an array . How i could do that ?
Dec 30 '06 #1
Share this Question
Share on Google+
3 Replies


P: 9
I wrote this piece of code :
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int ctr = 0;
  10.     int x;
  11.     int i = 0;
  12.     int array[161] ;
  13. //============================================================================== 
  14. //==============================================================================       
  15.     //prints on the screen  numbers from -50 to 109
  16.     for( x = -50 ; x < 110 ; x++ )
  17.     {
  18.        cout<< setw(3) << x << "#" << endl ;
  19.     }
  20.  
  21. //==============================================================================        
  22. //==============================================================================    
  23.     ofstream SaveFile("lala.txt" , ios::app );
  24.     //writes numbers from -50 to 109 in lala.txt
  25.     for( x = -50 ; x < 110 ; x++ )
  26.     {
  27.        SaveFile << setw(3) << x << "#" ;
  28.     }
  29.  
  30.     SaveFile.close();
  31.  
  32.     cout<< endl <<" =========" << " PHASE  1  " << "=========" << endl;
  33. //==============================================================================    
  34. //==============================================================================        
  35.     ifstream OpenFile("lala.txt");
  36.     static char str[4];
  37.     char ch ;
  38.  
  39.     while ( !OpenFile.eof() )
  40.     {
  41.        OpenFile.get(ch);
  42.        if ( ch == '#' )                               //when u meet ' # '
  43.        {
  44.            OpenFile.seekg(-3);                 //go 3 characters back
  45.            OpenFile >> str ;                     //and put the 3 characters in str
  46.            array[ctr] = atoi(str);                //convert str to int
  47.            OpenFile.seekg(4);                 //go 4 characters front so the inside -
  48.            ctr++ ;                                   //pointers shows on the next character 
  49.        }
  50. }
  51.     OpenFile.close();
  52.     cout<< endl <<" =========" << "END" << "=========" <<endl ;
  53.  
  54.  
  55.     return 0;
  56.  
  57. }
  58.  
  59.  
The problem is at this block :
Expand|Select|Wrap|Line Numbers
  1. {
  2.            OpenFile.seekg(-3);                 //go 3 characters back
  3.            OpenFile >> str ;                     //and put the 3 characters in str
  4.            array[ctr] = atoi(str);                //convert str to int
  5.            OpenFile.seekg(4);                 //go 4 characters front so the inside -
  6.            ctr++ ;                                   //pointers shows on the next character 
  7. }
  8.  
  9.  
Can you find what i do wrong ??????
Dec 30 '06 #2

P: 9
Also inside the lala.txt there is :

-50#-49#-48#-47#-46#-45#-44#-43#-42#-41#-40#-39#-38#-37#-36#-35#-34#-33#-32#-31#-30#-29#-28#-27#-26#-25#-24#-23#-22#-21#-20#-19#-18#-17#-16#-15#-14#-13#-12#-11#-10# -9# -8# -7# -6# -5# -4# -3# -2# -1# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# 11# 12# 13# 14# 15# 16# 17# 18# 19# 20# 21# 22# 23# 24# 25# 26# 27# 28# 29# 30# 31# 32# 33# 34# 35# 36# 37# 38# 39# 40# 41# 42# 43# 44# 45# 46# 47# 48# 49# 50# 51# 52# 53# 54# 55# 56# 57# 58# 59# 60# 61# 62# 63# 64# 65# 66# 67# 68# 69# 70# 71# 72# 73# 74# 75# 76# 77# 78# 79# 80# 81# 82# 83# 84# 85# 86# 87# 88# 89# 90# 91# 92# 93# 94# 95# 96# 97# 98# 99#100#101#102#103#104#105#106#107#108#109#
Dec 30 '06 #3

Expert 100+
P: 1,510
the reading of the data is very complex - when reading you can simply read an int then a char, e.g.
Expand|Select|Wrap|Line Numbers
  1.     ifstream OpenFile("lala.txt");
  2.     if ( OpenFile)
  3.     {
  4.        while(OpenFile >> array[ctr])   // read an int
  5.           {  ctr++; OpenFile.get(); }     // read the #
  6.     }
  7.  
when you are creating the data you are appending to any existing file so it is getting longer and longer and will overrun the end of your array when reading
Dec 30 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.