【问题标题】:C code to read a text file does not find EOF (end of file)读取文本文件的 C 代码未找到 EOF(文件结尾)
【发布时间】:2015-01-27 14:16:35
【问题描述】:

我编写了这个短代码来读取一个文本文件并将其信息复制到一个新的 txt 文件中,但在此过程中进行了一些字符替换。

我的问题是,代码完成了它应该做的所有工作,但它并没有结束。它无法在文件末尾 (arq1) 找到 EOF 特殊字符来告诉它完成处理。

有什么问题?

#include<stdio.h>
#include<stdlib.h>

typedef enum {false, true} Boolean;

int main(){

  FILE *arq1, *arq2;
  char filename[30];
  char first = '*' , second = '*';
  Boolean f_use = false, s_use = false;
  char aux;

  printf("Filename: ");
  scanf("%s", filename);

  arq1 = fopen(filename, "r");
  arq2 = fopen("Codes_out.txt", "w");

  while(fscanf(arq1, "%c", &aux) != EOF && aux != '\n')
    fprintf(arq2, "%c", aux); /*Copy first line*/

  fprintf(arq2, "\n");

  while(aux != EOF){ //#

    printf("Processing new line\n"); // TEST
    f_use = false;
    s_use = false; 

    while(aux != '\t' && aux != ' ' && aux != EOF){ /* Copy locus ID*/
      printf("%c", aux);//TEST
      fscanf(arq1, "%c", &aux);
      fprintf(arq2, "%c", aux);
      printf("Copying ID\n");//TEST
    }
    printf("ID copied\n");//TEST

  while(fscanf(arq1, "%c", &aux) != EOF && aux != '\n'){ //##

      /*If a code for nitrogen base is found, identify
    as first or second state and substitute 
    for a numeric code (1 or 2)*/
      if(aux == 'C' || aux == 'G' || aux == 'A' || aux == 'T' ||
     aux == 'c' || aux == 'g' || aux == 'a' || aux == 't'){

    if(f_use == false){ 
      /*First base not yet identified*/
      first = aux;
      f_use = true;
      printf("OK 6a\n\n"); //TEST
      printf("first = %c\n", first); //TEST
    }
    else if(s_use == false && aux != first){  
      /*second base not yet identified
        and aux different from first base*/
      second = aux;
      s_use = true;
      printf("OK 6b\n\n"); //TEST
      printf("second = %c\n", second); //TEST
    }

    if(aux == first){
      fprintf(arq2, "1");
      printf("OK 5a\n\n"); //TEST
    }
    else if(aux == second){
      fprintf(arq2, "2");
      printf("OK 5b\n\n"); //TEST
    }
  }

  else if(aux == ' ')
    fprintf(arq2, "%c", aux);

  else if(aux == 'N' || aux == 'n')
    fprintf(arq2, "%c", aux);

  else
    fprintf(arq2, "3");

  } //##
  printf("%c ", aux);
  fprintf(arq2, "\n"); /*add line break*/
  printf("OK 7\n\n"); // //TEST
} //#

printf("Processing finished\n"); //Control
fclose(arq1);
fclose(arq2);

return 0;
}

Here is the link for the input file

【问题讨论】:

  • printf 标记为 whit //TEST 是临时用于调试的
  • @Kaiser 您的代码实际上需要大量改进,但主要问题已在我的回答中得到解决,我建议您让您的代码以一种干净的方式遵循它自己的逻辑,我什至不想要尝试猜测它的作用。

标签: c text-files infinite-loop eof


【解决方案1】:

EOF 不是字符,你不能尝试用fscanf(arq1, "%c", &amp;eof); 读取它,你应该检查fscanf() 的返回值,它可以是EOF 或匹配参数的数量。

试试这样的

int status;

while ((status = fscanf(arq1, "%c", &aux)) != EOF)
{
 .
 .
if (status == 1)
    fprintf(arq2, "%c", aux);
 .
 .
}

还有很长的时间if 我会推荐这个

switch (aux)
{
case 'C':
case 'G':
case 'A':
case 'T':
case 'c':
case 'g':
case 'a':
case 't':
    /* code here */
    break;
}

【讨论】:

    【解决方案2】:

    使用feof( arqN ) 函数检查文件结尾。 EOF 代码 (0x1A) 并不存在于所有文本文件中。

    【讨论】:

    • feof() 几乎没有用处。
    • 有这样的代码,但你不知道。 0x1A 代码 (Ctrl-Z)。过去在 DOS 中使用。
    • 是的,你在 DOS 中说,EOF 是一个宏,因为我想它可能有不同的值,例如在 linux 上它是 -1
    • 文件中是真正的 0x1A 字节。如果在 Prompt 中输入 type myfile.txt 并到达 Ctrl-Z 代码,则文件被关闭。
    • ...当您使用fopen 打开文件时,必须对二进制文件使用“rb”模式。如果你用“rt”或“r”打开它,0x1A 代码将被解释为 EOF 并且你不能向前阅读。 (其他区别是0x0A代码替换为0x0D 0x0A)。
    猜你喜欢
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 2013-12-25
    • 1970-01-01
    相关资源
    最近更新 更多