当前位置: 首页 > 知识库问答 >
问题:

在C程序中使用strtok():为什么不能检索文本文件的下一行?

严宇
2023-03-14

我正在编写C代码来读取包含多行单词的文本文件。对于文本文件中的每一行,我想标记字符串以删除空格。

例如,假设要读取的文本文件名为“testlist.txt”,内容为:

input1.txt\n    
_input2.txt_\n    
__input3.txt_\n   

其中下划线表示空格,“\n”字符不可见。程序输出应为:

input1.txt
input2.txt
input3.txt 

将输出以下代码:

Token:input1.txt.
Token:

第二次打印“Token:”后没有换行。此外,我希望getline()能够获得testlist的下一行。txt,以使进程重复,但程序已完成执行/终止,没有错误/警告。

#include <iostream> 
#include <fstream>
#include <string.h>
#include <string> 

using namespace std;

int main() {

   string str_lineOutput;
   char* char_lineOutput, *tok; //str_lineOutput will be converted to char array  

   ifstream fp("testlist.txt"); //Open the file
   if(fp.is_open()) {
      while( getline(fp, str_lineOutput) ){ //Get the next line
         //Convert str_lineOutput to char_lineOutput which is what strtok() uses
         char_lineOutput = const_cast <char *> (str_lineOutput.c_str()); 
         //Remove spaces and line feed
         tok = strtok(char_lineOutput, " \n");
         while( tok != NULL){ 
             tok = strtok(NULL, " \n"); 
             cout << "Token:" << tok << ".\n";
         }
      }
      fp.close(); 
   }

   return 0; 
}

感谢那些查看过我的代码或提出替代解决方案的人。我坚持我的实现很重要。

错误代码源于在最内部的while循环中使用strtok()。使while循环的主体能够运行的条件断言tok!=NULL,但我通过调用strtok()并将其分配给while循环体中的tok,允许tok接受NULL。

我期望tok!=while循环体内部为NULL。有趣的是,程序只是暂停了,实际上从未完全终止,这与我试图将cout设置为NULL有关。

修复很简单:如果在内部strtok()之后,tok不为NULL,则在主体内运行代码。

共有2个答案

钦侯林
2023-03-14
#include <iostream> 
#include <fstream>
#include <string.h>
#include <string> 

using namespace std;

int main() {

   string str_lineOutput;
   char* char_lineOutput, *tok; //str_lineOutput will be converted to char array  

   ifstream fp("testlist.txt"); //Open the file
   if(fp.is_open()) {
      while( !fp.eof() ){ //added this and moved getline inside
           getline(fp, str_lineOutput)//Get the next line
         //Convert str_lineOutput to char_lineOutput which is what
         //strtok() uses
         char_lineOutput = const_cast <char *> (str_lineOutput.c_str()); 
         //Remove spaces and line feed
         tok = strtok(char_lineOutput, " ");//took out new line char
         while( tok != NULL){ 
             //switch the order of these lines
             cout << "Token:" << tok << ".\n";
             tok = strtok(NULL, " "); //took out new line char here too
         }
      }
      fp.close(); 
   }

   return 0; 
}

对代码进行了一些更改,得到了以下结果:

Token:input1.txt.

Token:input2.txt.

Token:input3.txt.

没有额外的线条,我是新来的...仍在学习如何正确发布答案。

我希望这有帮助。

虞滨海
2023-03-14

如果您只需要完成工作,这里有一个似乎有效的程序:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    const char *filePath = "E:\\temp\\foo.txt";
    std::ifstream is(filePath);
    std::string word;
    while (!is.eof())
    {
        is >> word;
        std::cout << "word: " << word << std::endl;
    }
    return 0;
}

如果你努力的目的是探索strtok(),当然,我的答案对你来说不会有什么好用处。

我测试的输入文件是:

第一ord.txtord.txt第三ord.txt

输出为:

单词:第一个单词。文字:第二个字。文字:第三个字。文字:第三个字。txt文件

 类似资料:
  • 嗨,这让我感到困惑,我正在尝试搜索我的文件中的选定数字,然后用该行的其余部分显示该数字,问题是我的程序似乎在我搜索它时返回第一行内容,但当我键入搜索它们时不会返回第二行或第三行 我的 txt 文件内容 我正在寻找的输出 当我键入2或3以查找项目时,我的程序不显示任何问题,我该如何解决它?

  • 在我们的应用程序中,我们需要实现以下场景: 从客户端发送请求 服务器处理请求并生成文件 服务器返回文件作为响应 客户端浏览器显示文件下载弹出对话框,允许用户下载文件 我们的应用程序是基于ajax的应用程序,因此发送ajax请求(如使用jquery.ajax()函数)将非常简单方便。 但是在googilng之后,事实证明文件下载只有在使用非ajax POST请求时才是可能的(就像在这个流行的SO线程

  • mongoDB v2.6.2 宝石'Mongo','~>1.10.2' RuntimeError:未知选项[{“score”=>{“$meta”=>“textscore”}}]

  • 我不知道我在PhpStorm 2019.2.3中的PHP文件发生了什么,但它与PHP文件不同。我的意思是缺少上下文帮助、代码重新格式化等。。。 我更改了哪些选项以及如何恢复? 实际上只有一个文件有问题,其他的都没问题。

  • 问题内容: 我有成千上万个包含多个JSON对象的文本文件,但是不幸的是,这些对象之间没有分隔符。对象存储为字典,它们的某些字段本身就是对象。每个对象可能具有可变数量的嵌套对象。具体来说,一个对象可能看起来像这样: 并在文本文件中串联了数百个这样的对象而没有分隔符。这意味着我既不能使用也不可以。 关于如何解决此问题的任何建议。是否有已知的解析器可以执行此操作? 问题答案: 这将从字符串中解码您的JS

  • 问题内容: 当我尝试对文件进行排序并将其输出保存在自身中时,就像这样 file1的内容将被完全删除,而当我尝试使用“ tee”命令执行同样的操作时 它运行良好 [ed:仅对具有幸运时机的小文件“运行良好”,会在大文件上或在无用的进程调度中导致数据丢失] ,即它会覆盖file1本身的排序输出并在标准输出中显示。 有人可以解释为什么第一种情况不起作用吗? 问题答案: 它不起作用,因为’>’重定向意味着