Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using the seekg command for files

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi, I currently need help on the seekg command. The problem is that I do not know how to write information to a file in the same block size (I am using random access)
for example:
I want to search the 9th and 17th object in a file:
---------------------------------------------------
A
B
C
D
...
X
Y
Z
---------------------------------------------------
the 9th and 17th should be I and Q
I used the command fin.seekg(9, ios::beg);
there wasn't any errors but it outputted D instead...
the object of my program is to find the I and Q and replace them to X. If anyone of you need to look at the source code i will post it here again... thank you.
 
Keep these in mind...
1) The argument in seekg is the absolute position in the file (in bytes). This includes white spaces and all.
2) The bytes in a file are numbered starting with 0.

So to get to the 'n'th object in a file in which the field width of each object is 'w'...
fin.seekg( w*(n-1), ios::beg)
...is what needs to be done. This is for reading. For writing you need to use seekp. Ankan.

Please do correct me if I am wrong. s-)
 
Greetinx!

If your file is shown as previous example, you should take into account a line feed and carriage return symbols, which are place after of each letter. Happy programming!))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top