所以我正在编写一个简单的程序,只是想了解它为什么忽略空格(它将它们视为新行)以及为什么它不考虑新行。
语言:C++
平台:Kubuntu 13.04
编译器:g++
代码:
 unsigned int lines;
 string line_content;
 ifstream r_tftpd_hpa("/etc/default/tftpd-hpa"); // open file
    // test for errors
if ( r_tftpd_hpa.fail() ) {
    cerr << "Error opening file: \"/etc/default/tftpd-hpa\"" << endl;
    exit(1);
}
    // loop through file until end
while ( !r_tftpd_hpa.eof() ) {
    r_tftpd_hpa >> line_content;
    lines++;
                          // I also tried with \n
    if ( line_content[0] == ' ' ) { // my failed attempt at catching spaces
        cout << endl << "Found empty line: " << lines << endl;
    }
    cout << "Line: " << lines << " content: " << line_content << endl;
}
输出:
 Line: 1 content: #
 Line: 2 content: /etc/default/tftpd-hpa
 Line: 3 content: TFTP_USERNAME="tftp"
 Line: 4 content: TFTP_DIRECTORY="/var/lib/tftpboot"
 Line: 5 content: TFTP_ADDRESS="0.0.0.0:69"
 Line: 6 content: TFTP_OPTIONS="--secure"
 Line: 7 content: TFTP_OPTIONS="--secure"   
文件本身:
 # /etc/default/tftpd-hpa
 TFTP_USERNAME="tftp"
 TFTP_DIRECTORY="/var/lib/tftpboot"
 TFTP_ADDRESS="0.0.0.0:69"
 TFTP_OPTIONS="--secure"
该文件由 6 行组成,但它似乎认为它是 7。它将#第 1 行之后的空格计为新行,并忽略原始文件中第 2 行的空格。它也打印行6 and 7,就好像有两条相同的行,没有。
知道这里发生了什么吗?我们如何处理空格和换行符?