| Guzfraba (25) | |||||
|
Hello ! I read about seekg and seekp,but i didn't understood what they do.... I've tryed this both codes:
Both work(no error),and the result are exact the same :| In file.txt is written "abcdefgh12345678",and after compiling,is written: [NUL][NUL][NUL][NUL][NUL]write.I would be happy for any response :) Thank you,and Merry Christmas ! | |||||
|
Last edited on
|
|||||
| lesshardtofind (5) | |||||
I believe
Creates an output file that is blank so you will always create a blank file and then move forward 5 and write on the sixth space. Since you never created values for the data positions before where you placed the cursor the [nul] is not at all unexpected. If you were wanted to write to the file but not create a blank document I think you need to use
This shouldn't call the Truncation | |||||
|
|
|||||
| Guzfraba (25) | |
| thank you,it worked with ios in and ios out. | |
|
|
|
| Cubbi (1927) | |||
This is important to know:
This writes at the 6th positions, because file streams have only ONE position. Both reading and writing happen there. That's a limitation of the C file I/O, which C++ file I/O is implemented in terms of. Other C++ streams (stringstream for example) have two independent positions: one for reading, one for writing. If f is a stringstream, your f<<"write" will write starting at the first position, where the put pointer is.
| |||
|
Last edited on
|
|||
| Guzfraba (25) | |||
|
ok,i understood: But here is another problem. I realized that seekg has influence on seekp and viceversa.Here is my example:
The output is 6,6.If i use seekg(6) before seekp(2),the output will be 2,2 . Is this normally ? :| | |||
|
|
|||
| Cubbi (1927) | |||
|
Yes, this is what file streams do. They have only one position, both seekp() and seekg() move it. Compare to:
this prints 2,6 | |||
|
|
|||