简体   繁体   English

使用C ++读取文件时省略换行符

[英]Omit the newline character in reading file with c++

I have this code: 我有以下代码:

#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::ifstream path("test");
    std::string separator(" ");
    std::string line;
    while (getline(path, line, *separator.c_str())) {
        if (!line.empty() && *line.c_str() != '\n') {
            std::cout << line << std::endl;
        }

        line.clear();
    }

    return 0;
}

The file "test" is populated with numbers, separated by various number of spaces. 文件“ test”填充有数字,并用各种空格分隔。 I need to read only the numbers, one by one and omit spaces and newlines. 我只需要一一阅读数字,并省略空格和换行符。 This code omits the blank spaces but not the newline character. 该代码省略了空格,但没有换行符。

These are few lines from the input file "test": 这些是输入文件“ test”中的几行:

     3        19        68        29        29        54        83        53
    14        53       134       124        66        61       133        49
    96       188       243       133        46       -81      -156       -85

I think the problem is that this *line.c_str() != '\\n' is not the proper way to determine if the string line hit newline character and the program keeps printing the newlines! 我认为问题是,这种*line.c_str() != '\\n'是不是确定字串有道line打换行符和程序保持打印新行!

This one works great: 这个效果很好:

#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::ifstream path("test");
    std::string separator(" ");
    std::string line;
    while (getline(path, line, *separator.c_str())) {
        std::string number;
        path >> number;
        std::cout << number << std::endl;
    }

    return 0;
}

使用C ++内置的isdigit函数。

Use the stream operator >> to read integers: 使用流运算符>>读取整数:

std::ifstream path("test");
int number;
while(path >> number)
    std::cout << number << ", ";
std::cout << "END\n";
return 0;

This will list all the integers in your file, assuming they are separated with space. 假定它们之间用空格分隔,它将列出文件中的所有整数。

The correct usage for getline is getline(path, line) or getline(path, line, ' ') where the last argument can be any character. getline的正确用法是getline(path, line)getline(path, line, ' ') ,其中最后一个参数可以是任何字符。

*separator.c_str() in this case converts to ' ' . *separator.c_str()在这种情况下转换为' ' This usage is not recommended. 不建议使用此用法。

Likewise *line.c_str() points to the first character in line . 同样, *line.c_str()指向line的第一个字符。 To find the last character use 查找最后一个字符

if (line.size())
    cout << line[size()-1] << "\n";

When using getline(path, line) , line will not including the last \\n character. 使用getline(path, line)line将不包含最后一个\\n字符。

Here is another example with getline . 这是getline另一个示例。 We read the file line by line, then convert each line to stringstream , and then read the integers from each line: 我们逐行读取文件,然后将每一行转换为stringstream ,然后从每一行读取整数:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

int main()
{
    std::ifstream path("test");
    std::string line;
    while(getline(path, line))
    {
        std::stringstream ss(line);
        int number;
        while(ss >> number)
            std::cout << number << ", ";
        std::cout << "End of line\n";
    }
    std::cout << "\n";
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM