用char类型的变量接收fgetc有时会导致错误的EOF
今天用fgetc读取文件里的字符发现总是会卡在某个特定的位置,调试时发现在该位置fgetc本应读取一个“A”时却错误返回了EOF。作为一个合格的面向百度的编程学习者,我很快便学习了事情的真相。详情可见:https://blog.
不过和这位老师所说的不同,将接收值的变量改成unsigned char并没能解决问题,我最终是将它变成int之后进行强制类型转换才能够正常完成读取,我用的IDE是vs2017.
不过我还有一点不明白,根据这篇博客的说法只有当读取值为0xFF的时候才有可能出现问题,而我要读取的明明是A,更何况A这个字符前面已经出现无数次了,为什么偏偏在某个特定位置卡住呢?
请大佬明示。报错的代码如下,素材见附件。将代码中_ctr改为int型即解决问题。
程序代码:
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<cstring> #include "structure_use.h" /*struct Fas { char name[30]; char *seq; Fas *lnk; }; struct S_e */ /*从main中得到文件指针 剥离出序列 */ Fas *autosearch(FILE*in, FILE*out,int*seq_count) { char _ctr = '\0'; int l_c = 0; //loop controler Fas *head=NULL,*n1=NULL,*n2=NULL; char*tmp = (char*)malloc(10000000), *tmp_ing = tmp; while (_ctr = fgetc(in)) { if (_ctr == '>') { if (feof(in)) { break; } n1 = (Fas*)malloc(sizeof(Fas)); fgets((*n1).name, 27, in); if ((*n1).name[strlen((*n1).name)-1] != '\n') { (*n1).name[strlen((*n1).name)] = '\r'; (*n1).name[strlen((*n1).name) + 1] = '\n'; (*n1).name[strlen((*n1).name) + 2] = '\0'; while ((_ctr = fgetc(in)) != '\n'); } for (l_c = 0; l_c < 10000000; l_c++) { if ((_ctr = fgetc(in)) == 'a' || _ctr == 't' || _ctr == 'c' || _ctr == 'g' || _ctr == 'A' || _ctr == 'T' || _ctr == 'C' || _ctr == 'G') { *tmp_ing = _ctr; tmp_ing++; } else if (_ctr == '\r' || _ctr == '\n') //要求序列中不要出现换行 { NULL; } else if (_ctr=='>'||_ctr == EOF) { *tmp_ing = '\0'; break; } else { printf("unexpected character happened on %s at %d as %c\n", (*n1).name, l_c, _ctr); } } _strupr(tmp);//全部变成大写 (*seq_count)++; (*n1).seq = (char*)malloc(strlen(tmp) + 1); strcpy((*n1).seq, tmp); if (*seq_count == 1) //这个seq_count计数是从1开始 { head = n1; n2 = n1; } else { (*n2).lnk = n1; n2 = n1; } } else { printf("dropped char as %c\n", _ctr); } if (feof(in)) { break; } }
a.txt
(4.72 MB)