繁体   English   中英

最后一个字符未打印到文件

[英]The last character is not printed to a file

我试图弄清楚为什么使用C函数strtok对我来说无法正常工作。 问题出在这里:我有一个文件,其中包含两种类型的信息:标题和文本描述。 文件中的每一行都是标题或文本描述的一部分。 标头以“>”开头。 描述文本位于标题之后,可以跨越多行。 文本末尾有一个空行,用于将描述与下一个标题分开。 我的目的是编写两个单独的文件:一个文件包含每一行的标题,另一个文件本身包含一行的相应描述。 为了用C语言实现代码,我使用fgets一次将文件读取一行到动态分配的内存中。 为了将描述文本写在一行上,我使用了`strtok来消除文本中存在的任何新行字符。

我的代码对于头文件正常工作。 但是,对于描述文件,我注意到即使将文本最后一个字符打印到标准输出,也不会将其打印到文件中

FILE *headerFile = fopen("Headers", "w"); //to write headers
FILE *desFile = fopen("Descriptions", "w"); //to write descriptions

FILE *pfile = fopen("Data","r");

if ( pfile != NULL )
{

  int numOfHeaders =0;

  char **data1 = NULL; //an array to hold a header line
  char **data2 = NULL; //an array to hold a description line 
  char line[700] ; //maximum size for the line

  while (fgets(line, sizeof line, pfile ))
  {

      if(line[0] =='>') //It is a header
      {
          data1 = realloc(data1,(numOfHeaders +1)* sizeof(*data1));
          data1[numOfHeaders]= malloc(strlen(line)+1);
          strcpy(data1[numOfHeaders],line);

          fprintf(headerFile, "%s",line);//writes the header

          if(numOfHeaders >0)
            fprintf(desFile, "\n");//writes a new line in the desc file

          numOfHeaders++;              
      }

      //it is not a header and not an empty line
      if(line[0] != '>' && strlen(line)>2)
      {
          data2 = realloc(data2,(numOfHeaders +1)* sizeof(*data2));
          data2[numOfHeaders]= malloc(strlen(line)+1);

          char *s  = strtok(line, "\n ");              
          strcpy(data2[numOfHeaders],s);

          fprintf(desFile, "%s",data2[numOfHeaders]);              
          printf(desFile, "%s",data2[numOfHeaders]);
       }

  } //end-while
  fclose(desFile);
  fclose(headerFile);
  fclose(pfile );

  printf("There are %d headers in the file.\n",numOfHeaders);

}

如评论中所述:

  fprintf(desFile, "%s",data2[numOfHeaders]);  //okay            
  printf(desFile, "%s",data2[numOfHeaders]);  //wrong  

第二行应为:

  printf("%s",data2[numOfHeaders]);  //okay

或者,您可以这样做:

  sprintf(buffer, "%s",data2[numOfHeaders]);
  fprintf(desFile, buffer);
  printf(buffer);    

其他可能的问题
没有输入文件,就不可能确定strtok()在做什么,但这是基于您所描述的猜测:

在这两行中

  data2[numOfHeaders]= malloc(strlen(line)+1);

  char *s  = strtok(line, "\n ");          

如果data2中包含的字符串具有任何嵌入的空格,则s将仅包含该空格之前的段。 并且因为您仅在刷新之前调用它一次:

while (fgets(line, sizeof line, pfile ))  

仅读取一个令牌(第一段)。

并非总是如此,但通常在循环中调用strtok()

char *s = {0};
s= strtok(stringToParse, "\n ");//make initial call before entering loop
while(s)//ALWAYS test to see if s contains new content, else NULL
{
    //do something with s
    strcpy(data2[numOfHeaders],s);
    //get next token from string
    s = strtok(NULL, "\n ");//continue to tokenize string until s is null
}

但是,正如我上面所说,在更改字符串的内容之前,您只能在该字符串上调用一次。 这样就有可能未打印的段尚未被strtok()标记。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM