Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a brief question as to how you would read a file. Specifically how could you read about 100 characters of a file at a time then write the very same characters to the same file.Like if you wanted to run an xor op on a file without using any file other than the source.

What I have tried:

This
void main ()
{
int theSize = 32;
char *data = new char[36];
fstream fileStreamIn("in",ios::in | ios::out | ios::binary);
while(fileStreamIn)
{
fileStreamIn.read(data,theSize);
size_t count = fileStreamIn.gcount();

cout << data;
fileStreamIn << data;
fileStreamIn.write(data,count);
}
fileStreamIn.close();
system("pause");
}
Posted
Updated 12-Mar-16 2:31am

1 solution

Reading is easy: you are already doing that.
And writing is easy: you are doing that as well.
All you need to is Seek the position: istream::seekg - C++ Reference[^]
It's worth noting that if you are switching from Read to Write or vice versa, it's a good idea to seek the right position anyway, as some systems do maintain independent input and output pointers.
 
Share this answer
 
Comments
Member 10657083 13-Mar-16 0:47am    
How would you do that with the code above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900