Home > Database > Mysql Tutorial > Windows下的换行回车与Linux的回车

Windows下的换行回车与Linux的回车

WBOY
Release: 2016-06-07 16:51:50
Original
1260 people have browsed it

Windows下的换行回车与Linux的回车的示例文本如下:fsad6546F5343646546345asfdsafsadfsda456749+8ASDFSAD现要读取第一行Windows

Windows下的换行回车与Linux的回车的示例文本如下:

fsad6546
F5343
646546345asfdsa
fsadfsda
456749+8
ASDFSAD

现要读取第一行

Windows下第一行结束是\r\n,8个字符之后是‘\r\n’,c语言fgetc读取时,第九个和第十个字符均为'\n',同时下一行的F是第11个字符

Linux下没有回车'\r',有换行,读完8个字符后是'\n',同时下一行的F是第十个字符

以下程序是读取文本文件,然后输出

(注意要先得到一行的长度)

#include
#include
#include

int main()
{

char *str,c;
str=(char*)malloc(1);

int k=0,sum=0;
FILE * pfile= fopen("test.txt","r");

while(1)
{
int i=0;
char d;
int k=ftell(pfile);
fseek(pfile,8,SEEK_SET);
c=fgetc(pfile);
while( c!='\n')
{

if(c==-1)
{
fseek(pfile,-i,SEEK_CUR);
goto first;
}
i++;
c=fgetc(pfile);
k=ftell(pfile);
}
k=ftell(pfile);
d=fgetc(pfile);
k=ftell(pfile);
fseek(pfile,-i-2,SEEK_CUR);//Linux下这要改为 fseek(pfile,-i-1,SEEK_CUR);


first:
free(str);
str=(char*)malloc(i+2);
if(fgets(str,i+2,pfile)==NULL)
{
break;
}

printf(str);

}
fclose(pfile);
return 0;
}

linux

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template