Reading data into files

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 5
Reputation: Nauro is an unknown quantity at this point 
Solved Threads: 0
Nauro Nauro is offline Offline
Newbie Poster

Reading data into files

 
0
  #1
Mar 13th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,571
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Reading data into files

 
0
  #2
Mar 13th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 5
Reputation: Nauro is an unknown quantity at this point 
Solved Threads: 0
Nauro Nauro is offline Offline
Newbie Poster

Re: Reading data into files

 
0
  #3
Mar 14th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,571
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Reading data into files

 
0
  #4
Mar 14th, 2005
Consider the following pseudocode for modifying a record:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 5
Reputation: Nauro is an unknown quantity at this point 
Solved Threads: 0
Nauro Nauro is offline Offline
Newbie Poster

Re: Reading data into files

 
0
  #5
Mar 15th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,571
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Reading data into files

 
0
  #6
Mar 15th, 2005
>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:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Reading data into files

 
0
  #7
Mar 15th, 2005
Originally Posted by Narue
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,571
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Reading data into files

 
0
  #8
Mar 15th, 2005
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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