C++ 在尝试读入文件时,如何处理回车换行符

C++ 在尝试读入文件时,如何处理回车换行符,c++,string,vector,getline,C++,String,Vector,Getline,因此,我正在处理一个需要读取的文件,其中包含分隔单词的逗号和每行末尾的回车换行符,我无法找到处理它的方法。我试图读入逗号前的每个单词,并将其放入a向量中,直到它到达回车换行符,但我遇到了问题 这是我的文本文件(如记事本++上所示,您可以看到符号。在实际文本中,[]中的内容不会显示) 我尝试了多种解决方案,但似乎都不管用。以下是我到目前为止所做的尝试。但它显然不起作用。我看到一些用户建议使用substring以某种方式读出逗号,并读入单词,但我不确定如何做到这一点。我找不到一个好的教程或例子。在我

因此,我正在处理一个需要读取的文件,其中包含分隔单词的逗号和每行末尾的回车换行符,我无法找到处理它的方法。我试图读入逗号前的每个单词,并将其放入a向量中,直到它到达回车换行符,但我遇到了问题

这是我的文本文件(如记事本++上所示,您可以看到符号。在实际文本中,[]中的内容不会显示)

我尝试了多种解决方案,但似乎都不管用。以下是我到目前为止所做的尝试。但它显然不起作用。我看到一些用户建议使用substring以某种方式读出逗号,并读入单词,但我不确定如何做到这一点。我找不到一个好的教程或例子。在我的头脑中,我有算法(或者至少,关于如何去做的步骤),但我不确定如何去实现它

Import file (istream)

Read until comma, take string and place it in vector1 (getline, input, ,), vector.push_back(input)
Repeat previous step until you reach \cr\lf stop reading. (getline(input, '/r'))

move on to the next line
Read until comma, take string and place it in vector2
Repeat
Read the line until /cr/lf
下面是我使用上述步骤的一部分进行实践的代码

string input;

    vector<string> v1;
    vector<string> v2;


    ifstream infile;

    infile.open("example.txt");

    while(getline(infile, input)) //read until end of line
    {
        while(getline(infile, input, '\r')) //read until it reaches a carriage return
        {
            while(getline(infile, input, ',')) // read until it reaches a comma
            {
                v1.push_back(input);  //take the word and put in vector.

            } 

        }

    }

    infile.close();
字符串输入;
向量v1;
矢量v2;
河流充填;
open(“example.txt”);
while(getline(infle,input))//一直读到行尾
{
while(getline(infle,输入'\r'))//读取,直到到达回车符
{
while(getline(infle,输入“,”)//一直读到逗号
{
v1.向后推(输入);//取单词并输入向量。
} 
}
}
infle.close();
任何帮助都将不胜感激


编辑:我忘了提。当我使用此代码时,它似乎没有将任何内容导入向量。我确信所有的单词都在getline函数的某个地方丢失了,但我不知道如何在不使用它的情况下读取逗号和回车换行符。

您应该先使用
getline()
来获取整行。它应该为您办理运费返还。然后,将结果放入
stringstream
中,并在其上使用
getline()
分隔逗号处的行

将输入读入向量向量的代码:

#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>

int main()
{
    std::ifstream fin("input.txt");
    std::vector<std::vector<std::string>> result;
    for(std::string line; std::getline(fin, line);)
    {
        result.emplace_back();
        std::stringstream ss(line);
        for(std::string word; std::getline(ss, word, ',');)
        {
            result.back().push_back(word);
        }
    }
    for(const auto &i : result)
    {
        for(const auto &j : i)
        {
            std::cout << j << ' ';
        }
        std::cout << '\n';
    }
}
#包括
#包括
#包括
#包括
int main()
{
std::ifstream-fin(“input.txt”);
std::向量结果;
for(std::string行;std::getline(fin,line);)
{
结果:放置_back();
std::stringstream ss(线路);
for(std::string word;std::getline(ss,word,,');)
{
result.back().推回(word);
}
}
用于(常数自动和i:结果)
{
用于(康斯特汽车与j:i)
{

std::难道你不需要担心
\cr\lf
对,因为
Windows
应该自动将它们转换成单个
\r
。因此
std::getline
应该做正确的事情。只关心行和逗号。@Galik-对,但是
'\r'
是一个打字错误。应该是
'\n',即换行符。@PeteBecker它与代码中的注释相匹配:)\r是回车符,\n是换行符。我同意,但公平地说,当人们在Windows上编写文本文件并试图在UNIX系统上读取时,有时会遇到问题。如果只是简单地复制,而没有适当的格式转换,则在这种情况下,它们最终会产生错误的CR值,并将这些值误解为
'\r'
(因为
'\r
'通常与CR值相同,以方便起见)。
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>

int main()
{
    std::ifstream fin("input.txt");
    std::vector<std::vector<std::string>> result;
    for(std::string line; std::getline(fin, line);)
    {
        result.emplace_back();
        std::stringstream ss(line);
        for(std::string word; std::getline(ss, word, ',');)
        {
            result.back().push_back(word);
        }
    }
    for(const auto &i : result)
    {
        for(const auto &j : i)
        {
            std::cout << j << ' ';
        }
        std::cout << '\n';
    }
}