Reference article:
C + + rookie tutorial notes 1
C + + rookie tutorial notes 5
What are the standard pan space characters in C/C + +?
C + + read file input stream read space line feed
C + + file stream stop about reading spaces
Detailed explanation of the use of C + + get() function
Difference between newline endl and \ n in C + +
Input and output processing in c/c + + is read by separator (line feed, comma, space, etc.)
!!! If you don't write well, please ask the boss for advice
#1. A blank line break is encountered
#include <iostream> #include <fstream> #include <string.h> int main(void) { std::fstream fs; fs.open("E:\\CodeField\\Code_Cpp\\C_Single\\csdn\\20211010\\src\\afile.dat", std::ios::trunc | std::ios::out); if (!fs) { std::cout << "open afile.dat err!" << std::endl; exit(-1); } char str[100]; std::cout << "enter sentence 1 : "; std::cin >> str; fs << str; std::cout << "enter sentence 2 : "; std::cin >> str; // fs << str << std::endl; // => '\n' + end // fs << str << '\n'; fs << str << '\t'; // fs << str << '\v'; // fs << str << '\f'; // fs << str << '\r'; // fs << str << ' '; std::cout << "enter sentence 3 : "; std::cin >> str; fs << str; fs.close(); // Read the file a and copy it to b std::ifstream ifs; ifs.open("E:\\CodeField\\Code_Cpp\\C_Single\\csdn\\20211010\\src\\afile.dat"); if (!ifs) { std::cout << "open afile.dat err!" << std::endl; exit(-1); } std::ofstream ofs; ofs.open("E:\\CodeField\\Code_Cpp\\C_Single\\csdn\\20211010\\src\\bfile.dat", std::ios::trunc); if (!ofs) { std::cout << "open bfile.dat err!" << std::endl; exit(-1); } int i = 0; while (ifs >> str) { std::cout << "chunk" << ++i << " : " << str << std::endl; ofs << str; } ifs.close(); ofs.close(); printf("--------------end--------------\n"); return 0; }
Output results:
enter sentence 1 : 12
enter sentence 2 : 23
enter sentence 3 : 34
chunk1 : 1223
chunk2 : 34
--------------end--------------
The file input stream was found to be stopped by a blank character ('\ t') and skipped. I don't want to skip
Contents of file afile.dat
1122 33
Contents of file bfile.dat
112233
The generated copied file (bfile.dat) is different, and the tab is missing
#2. Cancel the default ignore whitespace character of the output stream
The blank character is ignored by default. IFS > > str my understanding is to extract the file stream in front of the blank character into the variable str, and then locate the file position pointer to the blank character by default, so as to realize the function of skipping the blank character, and there will be self increment in the while loop statement.
This method can cancel the default ignore whitespace character of the output stream
void std::ios_base::unsetf(std::ios_base::fmtflags __mask)
int i = 0; ifs.unsetf(std::ios::skipws); // Cancel default ignore whitespace while (ifs >> str) { std::cout << "chunk" << ++i << " : " << str << std::endl; ofs << str; }
Output results:
enter sentence 1 : 12
enter sentence 2 : 23
enter sentence 3 : 34
chunk1 : 1223
--------------end--------------
When chunk1 was found, the cycle stopped
#3. get() method takes characters
If it is cancelled, the self increment of the while loop statement will disappear. Find an alternative, get method.
int std::istream::get()
std::istream &std::istream::get(char &__c)
Three parameters are not listed
Next, use the get() method
int i = 0; ifs.unsetf(std::ios::skipws); // Cancel default ignore whitespace while (ifs >> str) { ifs.get();// Read a character from the ifs file input stream std::cout << "chunk" << ++i << " : " << str << std::endl; ofs << str; }
Execution results:
enter sentence 1 : 12
enter sentence 2 : 23
enter sentence 3 : 34
chunk1 : 1223
chunk2 : 34
--------------end--------------
#4. Fetch and save blank character
While (IFS > > STR) is a loop condition. I don't understand it very well. I put it in the loop body
int i = 0; char ch = '\0'; ifs.unsetf(std::ios::skipws); // Cancel default ignore whitespace while (1) { ch = ifs.get(); // This function changes the file position pointer?? if (ch == EOF) { std::cout << "read file is over!" << std::endl; break; } else if (ch == '\n' || ch == '\t' || ch == '\v' || ch == '\f' || ch == 'r' || ch == ' ') { std::cout << "it is a white-space character !!!" << std::endl; std::cout << "chunk" << ++i << " : " << ch << std::endl; ofs << ch;// Save white space characters } else { ifs.seekg(-1, std::ios::cur); // Ordinary characters are returned. There will be problems with Chinese characters std::cout << "ch : " << ch << std::endl; ifs >> str;// Put this inside!!! std::cout << "chunk" << ++i << " : " << str << std::endl; ofs << str; } }
Operation results:
enter sentence 1 : 12
enter sentence 2 : 23
enter sentence 3 : 34
ch : 1
chunk1 : 1223
it is a white-space character !!!
chunk2 :
ch : 3
chunk3 : 34
read file is over!
--------------end--------------
afile.dat
1223 34
bfile.dat
1223 34
Tab copy found
doubt
- While (IFS > > STR), IFS > > STR what does this condition mean# Why does the loop stop when it is executed once in place 2
- Does ifs.get() fetch a character from the stream change the file position pointer?
stay
- std::fstream to be tested
- getline use
- Chinese text file reading and writing
- Regular white space character matching