Ugu*_*AYA 2 c console fgets carriage-return linefeed
我有ac代码,只需从txt文件中读取一行.该文件只有一行,如下所示:
读取此行的代码段是:
int** readFile(char* filename){
int col=0, row =0;
int i=0;
int* numList[2048];
for(int i = 0; i<2048; i++)
numList[i] = (int*) malloc(6*sizeof(int));
if(NULL == numList){
printf("Memory error!");
}
char * token = NULL;
char currentLine[25] = {'\0'};
FILE* file = fopen(filename, "r");
if(NULL != file){
printf("File is opened successfully\n");
if( NULL != fgets (currentLine, 60, file) )
{
int i = 0;
while (NULL != currentLine[i]){
printf("%d ", currentLine[i]);
i++;
}
}
}
else
{
printf("File I/O Error");
return NULL;
}
fclose(file);
return numList;
}
Run Code Online (Sandbox Code Playgroud)
当这段代码运行时,我得到以下输出:
我观察到一些可疑的东西,正如你在第一个截图(txt文件的内容)中看到的那样,Notepad ++在行尾给出了CR LF.但是在输出中,我看到10是LF的最后一个字符.
可能我错过了一个非常原始的观点但是,我无法理解为什么CR角色不存在.
不用说,平台是Windows,这是一个控制台程序.
感谢和问候.
小智 8
您正在以文本模式打开文件.此模式可确保您可以在任何平台上处理相同的文本文件.
C指定'\n'为行尾.在Windows中,行尾是序列"\r\n".C(在这种情况下,标准库实现stdio)将自动为您翻译.在文本模式下从Windows上的文件读取将为您\n提供\r\n.
如果要查看文件的字节内容,则必须以二进制模式打开它:
FILE* file = fopen(filename, "rb");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
364 次 |
| 最近记录: |