比较 strtok 之后的字符串
Comparing strings after strtok
我想在 strtok 之后比较字符串,但我似乎得到了假而不是真
如果我使用 strtok,我还需要做什么吗?
char file[] = "temp.txt";
ifstream getfile;
getfile.open(file,ios::in);
if(getfile.is_open())
{
char data[256];
char *line;
const char * test = "init";
//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 << "Comparing " << line << " with " << test <<endl;
//This is suppose to print but it dosent
if(line == test)
cout << line << endl;
line = strtok(NULL," ");
}
}
}
输出:
comparing init with init
想要的输出:
comparing init with init
init
谢谢!:D
====
============================更改为以下内容,它奏效了! :)
if(strcmp(line,test)==0)
您正在比较指针而不是内容。查看 strcmp 或将 C 字符串包装在 std::string 中。
您正在比较指针(字符串的地址)。 他们永远是不同的。 使用 strcmp()
比较字符串本身。
相关文章:
- 在循环C++中指定字符串之后,不会打印该字符串
- 我在 C++ 中创建了一个函数来递归反转字符串,但是之后如何使功能打印一个 endl?
- 比较字符串的 GetLine 工作一次,然后比较之后不起作用
- 打印C++中字符出现之间/之后的所有子字符串
- C++代码,用于在用户插入的字符串的每个元音之后插入一对字符
- 在字符串的每个元素(不使用弦乐)的每个元素之后插入字符
- 在C 字符串中,为什么在最后一个字符之后,通过索引和()访问索引时行为是不同的
- 在getline()之后获取字符串
- 在每个特定字符串之后插入下一行
- 在字符串中的角色之后,我如何忽略所有内容
- C :在字符串中找到一个char,然后在先前的char之后找到另一个字符
- 在 ss.clear() 之后使用 ss.str( " ") 用于新定义的字符串流
- 字符串操作 C++ 在字符串之后存储整数
- 为什么即使在启用 C++11 并且我包含字符串之后,'stod'仍然没有在此范围内声明?
- 正则表达式:在字符串和冒号之后提取 int
- 嵌入式零件终端UB之后,正在访问字符串的部分
- 为什么在realloc之后字符串在内存中变得无效
- C 如何在文件中的特定字符串之后获取数字
- 即使在将新字符串复制到现有字符串之后,旧包含也可用(在某些索引中)
- 如何获取从sstream变量中提取的字符串之后的剩余字符串