Basic steps to use files:
1. Declare an ofstream/ ifstream/ftream var.
2. Open a file with it.
3. Do stuff to the file (there are couple of ways.)
4. Close it.
ifstream - open the file for input (reading)
ofstream - open the file for output (writing)
fstream - open the file for input/output/both
file. open ("test.txt", ios::in | ios::out | ios::app)
I got a bit confused here, why do I need open AND constructor? Wouldn't it be a lot easier to have just open, or just a constructor... Any way, I'll research it a bit later
flag value means
ios::in open file for reading
ios::out open file for writing
ios::app open for writing,add to end of file(append).
ios::binary binary file
ios::nocreate do not create the file,open only if it exists
ios::noreplace open and create a new file if the specified file does not exist
ios::trunc open a file and empty it.(Boom, all the data is gone,if any)
ios::ate goes to end of file instead of the beginning
Returns current position, if in the end, return length of file:
file.tellg ( );
Move courser:
seekg/seekp (pos, ios)
pos – number of bites to move
ios – move from, beg, end, cur
ios::beg beginning of the stream buffer
ios::cur current position in the stream buffer
ios::end end of the stream buffer
file.read (buffer,size);
buffer – where to put text
size – how much, counting from seekg
size = sizeof("LOOOOL");
file.write ("LOOOOL",size);
This puts text inside a "file" document.I still need some time to figure out the way it adds, it should be based on seekp position, but I'll figure it out, I just need to make a few examples, I'm planning on making some kind of "holiday calendar" using a text file tomorrow.
eof() returns true if the end of the file has been reached
Other functions for reading:
1.seekg(); //move the read pointer in bytes
2.tellg(); //returns where the read pointer is in bytes
Other functions for writing:
1.seekp(); //move the write pointer in bytes
2.tellp(); //returns where the write pointer is in bytes
Комментариев нет:
Отправить комментарий