using fstream, is it possible to open a file, and be able to read and write in that file, within the same session?
I can't seem to get this to work, so I'm wondering if it's even possible.
Basically, something like this:
Or am I better off closing the file each time I'm done an operation? Will this slow down the program if there are a lot of operations? If I do leave it open and get this working, what happens if the program crashes or the file is accessed by another program?
Also, why do I get garbage characters in this code?
I can't seem to get this to work, so I'm wondering if it's even possible.
Basically, something like this:
Code:
fstream fileio;
fileio.open("testfile.txt",fstream::in | fstream::out | fstream::app | fstream::binary);
fileio.write("this is a test",14);
fileio.seekg(1);
char buffer[6];
fileio.read(buffer,6);
cout<<buffer<<endl; //should read "his is"
fileio.close();
Or am I better off closing the file each time I'm done an operation? Will this slow down the program if there are a lot of operations? If I do leave it open and get this working, what happens if the program crashes or the file is accessed by another program?
Also, why do I get garbage characters in this code?
Code:
fstream fileio;
fileio.open("testfile.txt",fstream::in | fstream::binary);
/*
fileio.write("this is a test",14);
*/
fileio.seekg(10); //if I remove this it works fine
char buffer[2];
fileio.read(buffer,2);
cout<<buffer<<"[end]"<<endl; //should return "te" but returns "te" with garbage chars at the end.
fileio.close();
Last edited: