gpt4 book ai didi

c++ - 如何通过strtok忽略一个字符?

转载 作者:搜寻专家 更新时间:2023-10-31 01:53:48 26 4
gpt4 key购买 nike

在下面的代码中我也想忽略字符”。但是在添加它之后,我仍然得到“Mr_Bishop”作为我的输出。

我有以下代码:

    ifstream getfile;
getfile.open(file,ios::in);
char data[256];
char *line;
//loop till end of file
while(!getfile.eof())
{
//get data and store to variable data
getfile.getline(data,256,'\n');

line = strtok(data," ”");
while(line != NULL)
{
cout << line << endl;
line = strtok(NULL," ");
}
}//end of while loop

我的文件内容:

hello 7 “Mr_Bishop”
hello 10 “0913823”

基本上我希望我的输出是:

hello
7
Mr_Bishop
hello
10
0913823

使用这段代码我只能得到:

hello
7
"Mr_Bishop"
hello
10
"0913823"

提前致谢! :)

我意识到我在内循环中犯了一个错误,漏掉了引号。但现在我收到以下输出:

hello
7
Mr_Bishop

hello
10
0913823

有什么帮助吗?谢谢! :)

最佳答案

看起来您使用写字板或其他工具来生成文件。您应该在 Windows 上使用记事本或 Notepad++,或在 Linux 上创建 ASCII 编码的类似工具。现在您正在使用类似于 UTF-8 的编码。

此外,"的正确转义序列是\"。例如

line = strtok(data," \"");

将文件修复为 ASCII 编码后,您会发现循环中漏掉了一些东西。

while(!getfile.eof())
{
//get data and store to variable data
getfile.getline(data,256,'\n');

line = strtok(data," \"");
while(line != NULL)
{
std::cout << line << std::endl;
line = strtok(NULL," \""); // THIS used to be strtok(NULL," ");
}
}//end of while loop

你在那里漏掉了一组引语。更正文件,这个错误会产生正确的输出。

关于c++ - 如何通过strtok忽略一个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10607743/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com