Setting an array and reading in a txt file backwards

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2007
Posts: 4
Reputation: adnichols is an unknown quantity at this point 
Solved Threads: 0
adnichols adnichols is offline Offline
Newbie Poster

Setting an array and reading in a txt file backwards

 
0
  #1
Apr 5th, 2007
I have been having difficulty trying to figure out how to read in data backwards. I get the idea, but have no idea how to properly code it since it is string data from a txt file. I was given the basic file to start with, but need to add in an array that will not assume more than 100 records.
  1. void main()
  2. {
  3. fstream custFile, custFileCopy;
  4. string custRecord;
  5.  
  6. custFile.open("customer.txt", ios::in);
  7. if(custFile.fail())
  8. { cout << "Master input file can't be opened. Program stopping." << endl;
  9. return;
  10. }
  11.  
  12. custFileCopy.open("custFileCopy.txt", ios::out);
  13. if(custFileCopy.fail())
  14. { cout << "Input file can't be opened. Program stopping." << endl;
  15. return;
  16. }
  17.  
  18. getline(custFile, custRecord);
  19.  
  20. char c;
  21.  
  22. while (!custFile.fail() )
  23. { int i = 0;
  24. while(!custRecord[i] == 0)
  25. { c = custRecord[i];
  26. cout << c;
  27. i++;
  28. }
  29.  
  30. cout << endl;
  31.  
  32. custFileCopy << custRecord << endl;// write an output record
  33. getline(custFile, custRecord); // read another input record
  34.  
  35. }
  36. custFile.close();
  37. custFileCopy.close();
  38. }
Last edited by WaltP; Apr 5th, 2007 at 2:46 pm. Reason: Added CODE tags -- read the words you typed over input text box...
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Setting an array and reading in a txt file backwards

 
0
  #2
Apr 5th, 2007
Welcome to Daniweb C/C++.

Please read the announcement at the top of the page regarding use of BB tags or the watermark in the Message box regarding the use of code tags specifically. Using these tags will keep the formatting of your code as you post it. If you don't indent, etc, please do.

For your particular problem I recommend you write out the steps first, then try to write code. Here's a sample of how you might do it

//I'll need
a file to read from
a second file to write to
an array to hold all the strings I read from file 1 and will write to file 2

//order of program
open file 1
check file 1 is open

open file 2
check file 2 is open

use a loop to read each string from file 1 one by one
place each string into the array as it's read
keep track of how many strings have been read in

use a loop to write the array of strings to file 2 starting from the last string read in to the first string read in

close both files
Last edited by Lerner; Apr 5th, 2007 at 1:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Setting an array and reading in a txt file backwards

 
0
  #3
Apr 5th, 2007
a. read the lines and insert it into a vector
  1. ifstream file("customer.txt") ;
  2. string rec ;
  3. vector<string> records ;
  4. while( getline(file,rec) )
  5. records.push_back(rec) ;

b. iterate over the vector backwards to
access the redords in reverse order
  1. ofstream outfile("custFileCopy.txt") ;
  2. std::copy( records.rbegin(), records.rend(),
  3. std::ostream_iterator<string>(outfile) );
you can do this only if the file is small enough;
for very large files memory may not be sufficient.
u could however read the file in parts (say 1000 records),
create intermediate output files (as above) and then
merge them in reverse order of creation.

*** note: i had not seen Lerner's post; it came in while i was
typing in the reply. so this is just a repetition of what was
suggested in that post
Last edited by vijayan121; Apr 5th, 2007 at 1:37 pm. Reason: correct syntax error
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: adnichols is an unknown quantity at this point 
Solved Threads: 0
adnichols adnichols is offline Offline
Newbie Poster

Re: Setting an array and reading in a txt file backwards

 
0
  #4
Apr 5th, 2007
Originally Posted by vijayan121 View Post
a. read the lines and insert it into a vector
  1. ifstream file("customer.txt") ;
  2. string rec ;
  3. vector<string> records ;
  4. while( getline(file,rec) )
  5. records.push_back(rec) ;

b. iterate over the vector backwards to
access the redords in reverse order
  1. ofstream outfile("custFileCopy.txt") ;
  2. std::copy( records.rbegin(), records.rend(),
  3. std::ostream_iterator<string>(outfile) );
you can do this only if the file is small enough;
for very large files memory may not be sufficient.
u could however read the file in parts (say 1000 records),
create intermediate output files (as above) and then
merge them in reverse order of creation.

*** note: i had not seen Lerner's post; it came in while i was
typing in the reply. so this is just a repetition of what was
suggested in that post
*******************
Thanks for your help. I am quite the newbie to this board! I am figuring out the in's and out's quickly though!

I started looking into the vector and think that is a much easier route. Here is what I came up with, however my compiler is still giving me two errors. It does not recognize my "backward" identifier from the function. Any suggestions on how to make the function work correctly?

I inserted this after reading from file:

  1. {
  2. const int records = 200;
  3. vector<int> customer(records);
  4. string rec;
  5.  
  6. for (int count = 0; count < records; count++)
  7. customer.push_back(count--);
  8. backward(customer);
  9. }
  10.  
  11. // Closing the files
  12. cout << "\n\n\nClosing files.\n\n\n";
  13. custFile.close();
  14. custFileCopy.close();
  15. cout << "Program has ended.\n\n\n";
  16. }
  17.  
  18. void backward(vector <int> vect)
  19. {
  20. for (int count = 0; count < 200; count++)
  21. cout << vect[count] << " ";
  22. cout << endl;
  23. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Setting an array and reading in a txt file backwards

 
0
  #5
Apr 5th, 2007
to fix the compiler error:
u need to declare the function before u
call it. add the following anywhere before
the call.
  1. void backward(vector <int> vect) ;
a vector is really a dynamically resizeable
array; paasing it by value is not a good
idea at all.
(u are copying a large collection!)
in general, for any user defined type,
prefer passing by const referance instead
of by value. so i would also suggest u
change the signature of backward to
  1. void backward( const vector<int>& vect ) ;

were u not trying to read strings (lines)
from the file (not integers) in the original
post?
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: adnichols is an unknown quantity at this point 
Solved Threads: 0
adnichols adnichols is offline Offline
Newbie Poster

Re: Setting an array and reading in a txt file backwards

 
0
  #6
Apr 5th, 2007
Originally Posted by vijayan121 View Post
to fix the compiler error:
u need to declare the function before u
call it. add the following anywhere before
the call.
  1. void backward(vector <int> vect) ;
a vector is really a dynamically resizeable
array; paasing it by value is not a good
idea at all.
(u are copying a large collection!)
in general, for any user defined type,
prefer passing by const referance instead
of by value. so i would also suggest u
change the signature of backward to
  1. void backward( const vector<int>& vect ) ;

were u not trying to read strings (lines)
from the file (not integers) in the original
post?
Ok, that did fix the compiler error. Yes I am trying to read in complete lines from a txt file, which includes for example customer names and addresses. But I had to declare a maximum of 100 to 200 records total. Now I just need to read in each line instead of all 0's. That was my error in trying to do this too quickly!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Setting an array and reading in a txt file backwards

 
0
  #7
Apr 5th, 2007
u need to use a vector<string>, not
vector<int>.

and u need to use getline to read in
eah line till eof. the file is read from
begin to end (forward). after inserting
the lines into the vector, iterate
backwards in the vector (from the
last element to the first).

do not worry about the number of
lines (as long as it is not impossibly
large); when u call push_back the
vector resizes itself. start with an
empty vector; after adding all the lines
(push_back), the size() method of the
vector will tell u how many strings have
been added. this is useful if u want to
iterate over it like a c style array; start
with pos size()-1 and end at pos zero
to iterate backwards.
Last edited by vijayan121; Apr 5th, 2007 at 4:21 pm. Reason: spelling
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Setting an array and reading in a txt file backwards

 
0
  #8
Apr 5th, 2007
IMO, sometime STL classes are more hassle than they are worth. One such occassion is during a class when the instructions are to use an array. Another is when the upper bounds of the array are given. And another is when the user has no idea what a template or an iterator is.
  1. ifstream fin("inputFile.txt"); //to read from file
  2. ofstream fout("outputFile.txt"); //to write back to file
  3. string input[100]; //to hold input
  4. int i = 0; //to keep track of how many strings actually read in, may be less than max possible.
  5.  
  6. //validate files are open here
  7.  
  8. //read input file one string at a time, adding it to the array in the appropriate index
  9. while(getline(fin, input[i]))
  10. i++;
  11.  
  12. //write to output file (frontwards for this demonstration) one string per line
  13. for(int x = 0; x < i; ++x)
  14. fout << input[i] << endl;
  15.  
  16. //close files here
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Setting an array and reading in a txt file backwards

 
0
  #9
Apr 6th, 2007
Originally Posted by Lerner View Post
IMO, sometime STL classes are more hassle than they are worth. One such occassion is during a class when the instructions are to use an array. Another is when the upper bounds of the array are given. And another is when the user has no idea what a template or an iterator is.
  1. ifstream fin("inputFile.txt"); //to read from file
  2. ofstream fout("outputFile.txt"); //to write back to file
  3. string input[100]; //to hold input
  4. int i = 0; //to keep track of how many strings actually read in, may be less than max possible.
  5.  
  6. //validate files are open here
  7.  
  8. //read input file one string at a time, adding it to the array in the appropriate index
  9. while(getline(fin, input[i]))
  10. i++;
  11.  
  12. //write to output file (frontwards for this demonstration) one string per line
  13. for(int x = 0; x < i; ++x)
  14. fout << input[i] << endl;
  15.  
  16. //close files here
prefer an array to a vector to bring the all the lines of a text file into memory?
well, i suppose everyone has a right to their opinion.
Last edited by vijayan121; Apr 6th, 2007 at 1:19 am. Reason: correct typo
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Setting an array and reading in a txt file backwards

 
0
  #10
Apr 6th, 2007
Originally Posted by vijayan121 View Post
u ... u ... u ... u ...

Read this please.
Last edited by WaltP; Apr 6th, 2007 at 2:35 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC