943,972 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5000
  • C++ RSS
Mar 13th, 2005
0

Reading data into files

Expand Post »
I'm currently writing a program and I want it to read in data from a source and if needed change some or all of it and re-write it to the same source file.
I have:
where all the variables are ints in a struct called date.
ifstream ifs("data.txt");
ifs>>date.hours;
ifs>>date.minutes;
ifs>>date.month;
ifs>>date.day;
ifs>>date.year;

and to output I have:
ofstream ofs("data.txt");
ofs<<date.hours;
ofs<<date.minutes;
ofs<<date.month;
ofs<<date.day;
ofs<<date.year;

Right now I read all of the data into the strut and do what ever operations I need and then read it all back. The problem is in larger text files I still need to read it all into my program, and then read it all back out. Is there a way to read in only one entry say minutes, and change it and then write it back as the new value WITHOUT having to read and write the rest of the text file entries. So I can just change the one entry?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Nauro is offline Offline
5 posts
since Jan 2005
Mar 13th, 2005
0

Re: Reading data into files

>So I can just change the one entry?
Yes, but only if the entry or record is a fixed length. Otherwise you risk overwriting the next entry. Can you describe the format of your file? That's a huge factor in how you read from and write to it.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 14th, 2005
0

Re: Reading data into files

The format looks something like this:
21 10 3 13 2005
for each entry which corresponds to the form: hour-minute-month-day-year
The first number in the file is the number of entries in the file. So a file with 3 sets of date looks like this:

3
21 10 3 13 2005
12 8 4 9 2004
3 6 10 31 2004

If there is an easier way to make this file i'm open to suggestions.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Nauro is offline Offline
5 posts
since Jan 2005
Mar 14th, 2005
0

Re: Reading data into files

Consider the following pseudocode for modifying a record:
C++ Syntax (Toggle Plain Text)
  1. while ( !in.eof() ) {
  2. pos = in.tellg();
  3. rec = read_rec ( in );
  4. if ( rec == search_rec ) {
  5. modify_field ( rec, new_entry );
  6. in.seekg ( pos );
  7. write_rec ( rec );
  8. }
  9. }
A good C++ reference should should give you the details.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 15th, 2005
0

Re: Reading data into files

Ok thanks,
correct me if I'm wrong though but that code only works as long as you know precisely how many char's it is until the entry u want to write? Or is it possible to allow it to read each entry instead of each Char. Or am I totally off base here and need to find a new C++ reference.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Nauro is offline Offline
5 posts
since Jan 2005
Mar 15th, 2005
0

Re: Reading data into files

>only works as long as you know precisely how many char's it is until the entry u want to write?
Nope, with your format you only need to know how many fields are in the record before the one you want to modify. Since each record is on a separate line, in.tellg() saves the location at the beginning of each record so that you can return to the beginning of the line and rewrite the record. Here's a sample execution with the file you gave:
C++ Syntax (Toggle Plain Text)
  1. 21 10 3 13 2005
  2. 12 8 4 9 2004
  3. 3 6 10 31 2004
Say you want to change 12 8 4 9 2004 to 12 8 4 11 2004. in.tellg() saves the position just before 21. You read the record, then test it to see if it's the one you want. The test fails, so the loop continues to the next iteration. in.tellg() saves the position just before 12 and the record is read. The test succeeds because this is the record you want to change, so modify_record is called to change 9 to 11. How it does so is irrelevant to the loop. With the modified record in hand, in.seekp ( pos ) resets the file pointer to the beginning of the line so that the new record can be rewritten over the old record.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 15th, 2005
0

Re: Reading data into files

Quote originally posted by Narue ...
C++ Syntax (Toggle Plain Text)
  1. 21 10 3 13 2005
  2. 12 8 4 9 2004
  3. 3 6 10 31 2004
Say you want to change 12 8 4 9 2004 to 12 8 4 11 2004
Wouldn't the resulting string being 1 byte longer overwrite the "3" in the next position (record)? Often when confronted with this type of data I prefer to pad values that will be less than 10 with zeros.

ie
21 10 03 13 2005
12 08 04 09 2004
03 06 10 31 2004

That makes each record a fixed length and maybe a little easier to code.
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005
Mar 15th, 2005
0

Re: Reading data into files

Ignore everything I said, I've been working with record-oriented files too much. Let's try again, this time when I'm actually thinking about the problem at hand instead of making it up as I go along.

>Is there a way to read in only one entry say minutes, and change it and
>then write it back as the new value WITHOUT having to read and write the
>rest of the text file entries.
With byte-oriented files, no, not really. There isn't a convenient and efficient way to shift bytes, so you're looking at the same issue as if you wanted to insert new data into an array. Technically, you only need to rewrite everything after and including the modified record, and a rewrite of the latter part of the file is only necessary if the new record differs in byte size from the old record. Even then, you can pad with empty space if the new record is smaller.

It would probably be better in your case to work with each line on a case by case basis, using a temporary file as your output. That way you only have to keep one record in fast memory at a time, and the temporary file can simply be renamed to replace the old file. No more copying is necessary at that point, but you still have to read and write the entire contents of the file once.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

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: C++ Reorder random numbers
Next Thread in C++ Forum Timeline: exponents in c++





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


Follow us on Twitter


© 2011 DaniWeb® LLC