C++ 用delimeter从C++;

C++ 用delimeter从C++;,c++,C++,我正在试图逐行读取文件中的内容。基本上,每行都有Id号、价格、数量和书名。我知道在每次输入之后,我必须在那里放置分隔符标志以停止读取,但我不知道如何进行。我试图使用getline(),但是,getline()只能接受一个字符串,而id、价格和数量则不是这样。有人能告诉我怎么读这个文件吗?谢谢,这就是我到目前为止所拥有的 例如: 文件应包含多行,每行的格式如下: -第一个号码是身份证号码 -第二个是价格 -三是数量 -最后一个是书名 123,19.99,5,夏天很有趣 void ReadFromF

我正在试图逐行读取文件中的内容。基本上,每行都有Id号、价格、数量和书名。我知道在每次输入之后,我必须在那里放置分隔符标志以停止读取,但我不知道如何进行。我试图使用
getline()
,但是,
getline()
只能接受一个字符串,而id、价格和数量则不是这样。有人能告诉我怎么读这个文件吗?谢谢,这就是我到目前为止所拥有的

例如: 文件应包含多行,每行的格式如下: -第一个号码是身份证号码 -第二个是价格 -三是数量 -最后一个是书名

123,19.99,5,夏天很有趣

void ReadFromFile(int ids[], int prices[], int qty[], string titles[], int& count)
{
    ifstream infile("tomatoes.txt");
    if(!infile.is_open())
        cout << "File does not exists" << endl;
    else
    {
        while(!infile.eof())
        {
            infile >> ids[count];
            infile >> prices[count];
            infile >> qty[count];
            getline(infile, titles[count]);
            if(!infile.eof())
                count++;
        }
    }
}
void ReadFromFile(整数标识[]、整数价格[]、整数数量[]、字符串标题[]、整数和计数)
{
ifstream-infle(“tomatos.txt”);
如果(!infle.is_open())
cout id[计数];
填充>>价格[计数];
填充>>数量[计数];
getline(填充,标题[计数]);
如果(!infle.eof())
计数++;
}
}
}

读取整行内容,然后使用
sscanf()
提取值:

string line;
getline(infile, line);

int id, quantity;
double price;
char title[20];

sscanf(line.c_str(), "%d,%lf,%d,%[^\n]", &id, &price, &quantity, title);

cout << "id = " << id << endl;
cout << "price = " << price << endl;
cout << "quantity = " << quantity << endl;
cout << "title = " << title << endl;
字符串行;
getline(填充,行);
int id,数量;
双倍价格;
字符标题[20];
sscanf(行.c_str(),%d,%lf,%d,%[^\n],&id,&price,&quantity,title);

coutget有一个重载版本,允许您指定分隔符

解决这一问题的一种方法是:

for(int count = 0; infile.eof() == false; count++ )
{
   // for the line
   stringstream currentLine;

   // get a line at a time
   infile.get(currentLine)

   // for the element
   stringstream currentElement;

   // work on that line
   currentLine.get(currentElement,',');
   currentElement >> ids[count];
   currentElement.str();

   currentLine.get(currentElement,',');
   currentElement >> prices[count];
   currentElement.str();

   // etc...

}

那么,我们再来一次,另一个解析问题

基本读取循环。
读取文件不会使用
eof
方法来确定文件的结尾。您可以在StackOverflow中搜索“c++读取eof while”以获取一些示例

std::string textline;
while (getline(infile, textline)
{
}
上面的循环将一次读取一行文本,直到读取一行失败为止,这通常是文件的结尾

解析文本
我们可以通过将文本字符串作为输入流来解析它。这涉及到使用
std::istringstream
getline
。 有一个重载版本的
getline
,它将读取文本,直到找到指定的分隔符为止。在您的例子中,此分隔符是逗号

std::string textline;
while (getline(infile, textline)
{
   string comma_string;
   std::istringstream text_stream(textline);
   text_stream >> id;
   getline(text_stream, comma_string, ','); // Read characters after number until comma.
   text_stream >> price;
   getline(text_stream, comma_string, ','); // Read characters after number until comma.
   text_stream >> quantity;
   getline(text_stream, comma_string, ','); // Read characters after number until comma.
   // Read the remaining text, until end of line, as the title.
   getline(text_stream, title, '\n');
}
存储记录
输入循环已扩展为在字段中读取。它们应该存储在一个对象中

std::string textline;
while (getline(infile, textline)
{
   string comma_string;
   std::istringstream text_stream(textline);
   text_stream >> id;
   getline(text_stream, comma_string, ','); // Read characters after number until comma.
   text_stream >> price;
   getline(text_stream, comma_string, ','); // Read characters after number until comma.
   text_stream >> quantity;
   getline(text_stream, comma_string, ','); // Read characters after number until comma.

   // Read the remaining text, until end of line, as the title.
   getline(text_stream, title, '\n');

   // Put the file data fields into an object.
   book.id = id;
   book.price = price;
   book.quantity = quantity;
   book.title = title;
}
将数据附加到容器中

std::string textline;
std::vector<Book> catalog;
while (getline(infile, textline)
{
   string comma_string;
   std::istringstream text_stream(textline);
   text_stream >> id;
   getline(text_stream, comma_string, ','); // Read characters after number until comma.
   text_stream >> price;
   getline(text_stream, comma_string, ','); // Read characters after number until comma.
   text_stream >> quantity;
   getline(text_stream, comma_string, ','); // Read characters after number until comma.

   // Read the remaining text, until end of line, as the title.
   getline(text_stream, title, '\n');

   // Put the file data fields into an object.
   book.id = id;
   book.price = price;
   book.quantity = quantity;
   book.title = title;

   // Append the book to the catalog
   catalog.push_back(book);
}
std::字符串文本行;
矢量目录;
while(getline(内嵌、文本行)
{
字符串逗号\字符串;
std::istringstream文本\ U流(文本行);
文本流>>id;
getline(文本流,逗号字符串,,);//读取数字后面的字符,直到逗号。
文本流>>价格;
getline(文本流,逗号字符串,,);//读取数字后面的字符,直到逗号。
文本流>>数量;
getline(文本流,逗号字符串,,);//读取数字后面的字符,直到逗号。
//阅读剩余的文本,直到行尾,作为标题。
getline(文本流,标题'\n');
//将文件数据字段放入对象中。
book.id=id;
账面价格=价格;
数量=数量;
书名=书名;
//将这本书附加到目录中
目录。推回(书);
}
结论

你不应该期望别人给你想要的东西,但是你应该能够把代码片段和你的代码片段进行比较,看看你是否能让它们工作。

还有另一个文件解析问题。在Stackoverflow中搜索“c++读取文件解析”对于更多类似的例子。我一直在搜索和询问,但是,似乎还没有人给我正确的答案:(所以我认为你不能看其他类似的例子,并将你的与他们的进行比较。我只是在今天早些时候回答了一个。infle.get(*(currentLine.rdbuf())在这一行仍然有错误:currentLine.get(currentElement,“,”);这里也有相同的更改:currentLine.get(*(currentElement.rdbuf()),',')如果你使用的是<代码> STD::String ,你不应该使用<代码>:STRIGSWORSTOR> <代码>而不是<代码> SCASNF?Thomas Matthews,当然,最好是一致的,不要混合C和C++。这只是第一个出现在我脑海中的东西。谢谢你花时间来把事情解释得这么清楚。你帮了大忙。