catalogue
External and internal documents
File stream classes and objects
get and put file reading and writing
Binary file read and write (read and write)
There are sprintf and sscanf in C, and C + + also has its own ability to read and write strings
Classification of documents
External and internal documents
External files
Hard disk, U SB flash disk, etc. the files on the disk are external files
Internal documents
The file used in the program is the file stream object, that is, the data in memory
Document classification
text file
That is, ASCII files, an ASCII code is a byte
Binary file
It is the file that the computer stores on the disk as it is
That is, 111111 takes up 6 bytes in the text file (one byte for each of the six 1s) and 4 bytes in the binary file (an int type)
File stream classes and objects
The standard library provides standard input (istream) output (ostream) input output (iostream)
For file operations
ifstream, derived from istream, file read
ofstream, derived from ostream, is written to the file
Fsstream, derived from iostream, reads and writes files
Header file
#include <fstream>
File open close
Open file
ofstream out; // Define OFSTREAM objects
out.open("path / file name", file opening method); // Open file
ofstream out("path / file name", file opening method); // This is also possible, constructor
File opening method
ios::in Open file as write
ios::out Open the file in the form of reading. If there is no file, create it and clear the contents of the file if any
ios::app Open the file in the form of append. If there is no file, create it
ios::ate Open an existing file and point the file read pointer to the end of the file
ios:: trunc When you open a file, all the data stored inside will be emptied. When used alone, it is the same as ios::out
ios::binary Open the file in binary mode. If this mode is not specified, it will be opened in text mode
ios::in | ios::out under fstream class Open an existing file to read its contents or write data to it. When the file is first opened, the original contents remain unchanged. If the file does not exist, there is an error opening it
ios::in | ios::out | ios::trunc under fstream class Open an existing file to read its contents or write data to it. If the file does not exist, create a new file. If the file exists, empty the data
When the file fails to open, it returns 0, which can be used to detect whether the file is opened successfully
Close file
File stream object. close();
File reading and writing
#include <iostream> #include <fstream> using namespace std; int main() { //Open file writing, file does not exist, create file, file exists, empty file fstream fout("test.txt", ios::out); //Judge whether it is opened successfully if (!fout) { cerr << "fout"; exit(1); //Didn't you say that normal exit returns 0, and non-0 abnormal exit } //write file fout << "abed, I see a silver light,\n It's suspected to be frost on the ground.\n look at the bright moon,\n Bow your head and think of your hometown.\n"; //Close file fout.close(); //Open the file to read. If the file does not exist, return 0 fstream fin("test.txt", ios::in); if (!fin) { cerr << "fin"; exit(1); } string str2; //read file fin >> str2; cout << "Read file contents:" << endl; cout << str2 << endl; //Close file fin.close(); return 0; }
Output information
file information
You can't read space directly. Carriage return is a blank character
Because it is inherited, the standard input / output member functions (get,getline,write,put,read) also apply to files and follow their rules. The above code also does not read white space characters from files
get and put file reading and writing
You can read white space characters, line breaks, and spaces
Although the get function reads a file as a byte, it actually reads one sector at a time, and then only obtains data from memory. This can improve the speed and reduce the consumption of hard disk
This knowledge should be in the principles of computer composition, perhaps in the fundamentals of computer. I can't remember clearly
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { fstream fout("test.txt", ios::out); if (!fout) { cerr << "fin"; exit(1); } string str = "1234567\n 234567\n123456"; //Write file write one character at a time, you can write white space characters for (int i = 0; i < str.size(); ++i) { fout.put(str[i]); } fout.close(); //If the file here is not closed, the file pointer will not read the data we want at the end. It will be said later fstream fin("test.txt", ios::in); if (!fin) { cerr << "fin"; exit(1); } char ch = 0; //C says EOF (end of file). Generally, this value is - 1, but it also represents invalid characters, so it can be judged as the end while (fin.get(ch)) { cout << ch; } fin.close(); return 0; }
Output information
file information
getline read file
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { fstream fout("test.txt", ios::out); if (!fout) { cerr << "fin"; exit(1); } string str = "1234567\n 234567\n123456"; //Write file write one character at a time, you can write white space characters for (int i = 0; i < str.size(); ++i) { fout.put(str[i]); } fout.close(); //If the file here is not closed, the file pointer will not read the data we want at the end. It will be said later fstream fin("test.txt", ios::in); if (!fin) { cerr << "fin"; exit(1); } string str2; //He reads spaces and discards line breaks while (getline(fin,str2)) { cout << str2; } fin.close(); return 0; }
Output information
file information
Binary file read and write (read and write)
#include <iostream> #include <fstream> using namespace std; struct Student { char m_name[64]; int m_age; int m_score; }; int main() { Student *stu = new Student[3]; stu[0] = { "Xiaohua",11,111 }; stu[1] = { "Xiao Ming",22,222 }; stu[2] = { "Xiaolan",33,333 }; fstream fout("test.txt", ios::out | ios::binary); if (!fout) { cerr << "fout"; exit(1); } //The first parameter to write a file requires char *, so we need to force conversion for (int i = 0; i < 3; ++i) { fout.write((char*)&stu[i], sizeof(stu[i])); } delete[]stu; fout.close(); fstream fin("test.txt", ios::in | ios::binary); if (!fin) { cerr << "fin"; exit(1); } Student* stu2 = new Student[3]; //read file for (int i = 0; i < 3; ++i) { fin.read((char*)&stu2[i], sizeof(stu2[i])); } for (int i = 0; i < 3; ++i) { cout << stu2[i].m_name << " " << stu2[i].m_age << " " << stu2[i].m_score << endl; } delete[]stu2; fin.close(); return 0; }
Output information
file information
File data location
There is a file pointer in the file. Every time we read a byte, the pointer moves back one byte
File pointer member function
gcount() Returns the number of bytes last read
tellg() Returns the current position of the file pointer
Seekg (pointer position) Moves the file pointer to the specified location
Pointer position
ios::beg Default at the beginning of the file
ios::cur Current location of file pointer
ios::end End of file
Seekg (offset, pointer position) Move the file pointer to the offset position
tellp() Returns the current position of the file pointer
Seekp (pointer position) Moves the file pointer to the specified location
Seekp (offset, pointer position) Move the file pointer to the offset position
Where g is read (read pointer) and p is write (write pointer)
EOF
It is also a member function. It is generally - 1. When text is ASCII code 0 to 255, there is no - 1, so it can be judged directly
The file terminator under Windows is Ctrl + D, and the file terminator under Linux is Ctrl + D (this is the answer I got from Baidu, but I remember it seems to be CTRL+c...)
There are sprintf and sscanf in C, and C + + also has its own ability to read and write strings
istrstream strout(char* buf,int n)
ostrstream strin(char* buf,int n)
The first is the string address, and the second is how many characters to read or write
But I can't use it