944,099 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 7470
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 5th, 2007
0

Setting an array and reading in a txt file backwards

Expand Post »
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
adnichols is offline Offline
4 posts
since Apr 2007
Apr 5th, 2007
0

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

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Apr 5th, 2007
0

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

a. read the lines and insert it into a vector
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Apr 5th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
a. read the lines and insert it into a vector
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
adnichols is offline Offline
4 posts
since Apr 2007
Apr 5th, 2007
0

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

to fix the compiler error:
u need to declare the function before u
call it. add the following anywhere before
the call.
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. void backward( const vector<int>& vect ) ;

were u not trying to read strings (lines)
from the file (not integers) in the original
post?
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Apr 5th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
to fix the compiler error:
u need to declare the function before u
call it. add the following anywhere before
the call.
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
adnichols is offline Offline
4 posts
since Apr 2007
Apr 5th, 2007
0

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

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
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Apr 5th, 2007
0

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

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.
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Apr 6th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by Lerner ...
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.
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Apr 6th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
u ... u ... u ... u ...

Read this please.
Last edited by WaltP; Apr 6th, 2007 at 2:35 am.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: linker errot
Next Thread in C++ Forum Timeline: iostream





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC