C++ 按分隔符拆分行以分析文本文件

C++ 按分隔符拆分行以分析文本文件,c++,csv,C++,Csv,我正在编写一个程序,它接受一个txt文件,如下所示: while (not end of file) { while (not end of line) { while (next char isn't /) { temporary string x += char } foo.push_back(string) //ID while (next char isn

我正在编写一个程序,它接受一个txt文件,如下所示:

while (not end of file)
{
    while (not end of line)
    {
        while (next char isn't /)
        {
            temporary string x += char
        }
        foo.push_back(string)       //ID

        while (next char isn't /)
        {
            string bar += char      //value
        }
    }
}
foo.txt:

Aaaa/NGACG/NGAA//
Aaab/AGGGC//
Aaac/CTN/AGGC/NNA//
在每一行中,它将ID(Aaa..)存储到一个向量中(每个值一次),并将由/分隔的值存储到字符串中

因此,第一行是:

    foo.push_back("Aaaa");
    string bar = NGACG;
    foo.push_back("Aaaa");
    string bar2 = NGAA;
伪代码如下所示:

while (not end of file)
{
    while (not end of line)
    {
        while (next char isn't /)
        {
            temporary string x += char
        }
        foo.push_back(string)       //ID

        while (next char isn't /)
        {
            string bar += char      //value
        }
    }
}
我的伪代码显然有缺陷,但这正是我想做的。我已经查阅了关于如何解析的指南,但没有任何东西真正符合我的目的。我如何才能做到这一点而不完全低效?我想不出如何在不使用任意数量的while循环的情况下编写这篇文章

您可以使用
fgets
strtok
使用传统的C语言编写:

char line[SIZE];   // make sure SIZE is greater than your longest input line ...
char *ix;
while(fgets(line), SIZE, fdin) {
    line[strcspn("\n")] = '\0'; // remove EOL
    ix = strtok(line, "/");
    foo.push_back(string(ix));
    string bar = string(strtok(NULL, "/"));
    string bar2 = string(strtok(NULL, "/"));
    ...
}
或者您可以使用带有分隔符参数的
stringstream
std::getline

string line;
while(getline(fdin, line)) {
    string temp, bar, bar2;
    istringstream is(line);
    getline(is, temp, '/');
    foo.push_back(temp);
    getline(is, bar);
    getline(is, bar2);
    ...
}

当然,您应该添加错误条件的测试…

您可以将行(或整个文件)读入简单的字符数组,并直接使用您已经阅读的行:只需将
/
替换为
'\0'
并将指向下一个字符的字符指针指向每一行,而无需复制任何内容。空字节终止原始数据中斜杠之间的“值”,在所有目的和目的上都是C字符串。

您是否想过使用类似?或scanf的东西,它接受一些基本模式作为转换;在循环中,读取任何不是斜杠的内容,然后读取斜杠。类似这样的内容(但使用EOF检查):
if(scanf(“%[^/]”,buf)==1){strcpy(val,buf);}getchar();//“/”}
的可能重复项