Selecting where to start from when reading or writing text to a file.

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

Join Date: Dec 2007
Posts: 65
Reputation: CodeBoy101 is an unknown quantity at this point 
Solved Threads: 0
CodeBoy101's Avatar
CodeBoy101 CodeBoy101 is offline Offline
Junior Poster in Training

Selecting where to start from when reading or writing text to a file.

 
0
  #1
May 1st, 2008
Is there a way to chose where a program outputs text to a file ?(or read text from a file). As in selecting which line to start from.? Any help is welcome.
Last edited by CodeBoy101; May 1st, 2008 at 3:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
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: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Selecting where to start from when reading or writing text to a file.

 
0
  #2
May 1st, 2008
Only if the file is highly structured. seekg and seekp will position the file pointer at a specified number of bytes from the beginning of the file, but unless each line has exactly the same number of bytes, trying to place the pointer at the start of the desired line is almost impossible. A common alternative when files aren't perfectly predictable is to read the lines into a container which then makes it easier to find the desired line.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,627
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: 717
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Selecting where to start from when reading or writing text to a file.

 
2
  #3
May 1st, 2008
>As in selecting which line to start from.?
Sure, but unless the file is structured enough to seek, you'll have to actually read your way to the correct line. Writing is harder if you mean to insert rather than overwrite. In that case you need to save the subsequent lines and shift them over like you would insert into an array.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 65
Reputation: CodeBoy101 is an unknown quantity at this point 
Solved Threads: 0
CodeBoy101's Avatar
CodeBoy101 CodeBoy101 is offline Offline
Junior Poster in Training

Re: Selecting where to start from when reading or writing text to a file.

 
0
  #4
May 1st, 2008
I mean to overwrite, but how does seekg and seekp work?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 65
Reputation: CodeBoy101 is an unknown quantity at this point 
Solved Threads: 0
CodeBoy101's Avatar
CodeBoy101 CodeBoy101 is offline Offline
Junior Poster in Training

Re: Selecting where to start from when reading or writing text to a file.

 
0
  #5
May 1st, 2008
What if I want to read:

a character.
a string.

In that order.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: Selecting where to start from when reading or writing text to a file.

 
1
  #6
May 1st, 2008
Does the infile consist of strings and characters jumbled?
I shot the sheriff....but I didn't shoot the deputy
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 65
Reputation: CodeBoy101 is an unknown quantity at this point 
Solved Threads: 0
CodeBoy101's Avatar
CodeBoy101 CodeBoy101 is offline Offline
Junior Poster in Training

Re: Selecting where to start from when reading or writing text to a file.

 
0
  #7
May 7th, 2008
Originally Posted by Narue View Post
>As in selecting which line to start from.?
Sure, but unless the file is structured enough to seek, you'll have to actually read your way to the correct line. Writing is harder if you mean to insert rather than overwrite. In that case you need to save the subsequent lines and shift them over like you would insert into an array.
Thanks for your asuggestion I investigated the seekg and seekp functions and found them to be very useful, b ut it turns out that I didn't need them after all.
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