有关fgets读取文件时遇到换行符的问题,急!!!!!!!!!
jenf 2007-05-22 05:36:14 各位大侠,小弟的代码想写入数据至文件,并且每次写入都从新的一行开始。但是现在的问题是fgets的时候遇到换行符就结束了,只能读取第一行,请各位大侠帮忙看看怎么读取全部的数据出来?多谢了!
代码如下:
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "string.h"
void iwritefile();
void ireadfile();
main()
{
iwritefile();
ireadfile();
return 0;
}
void iwritefile()
{
int a[7]={12,24,56,78,32,67,55};
char cha[7][2];
char ch = ' ';
char chrturn = '\n';
FILE *fpw;
for(int i=0; i<7; i++)
{
itoa(a[i],cha[i],10);
}
if((fpw=fopen("test1.txt","a"))==NULL)
{
printf("cann't open file test1.txt");
exit(0);
}
fseek(fpw,0L,SEEK_END);
if (0 != ftell(fpw))
{
fwrite(&chrturn,1,1,fpw);
}
for(i=0;i<7;++i)
{
fwrite((cha+i),sizeof(cha[i]),1,fpw);
fwrite(&ch,1,1,fpw);
}
fclose(fpw);
printf("the originality data is:\n");
for(i=0;i<7;i++)
{
printf("%d ",a[i]);
}
printf("\n\n");
}
void ireadfile()
{
FILE *fpr;
if(NULL == (fpr = fopen("test1.txt","r"))) //ÒÔÖ»¶Á·½Ê½´ò¿ªÎļþ
{
printf("error");
exit(1);
}
fseek(fpr,0L,SEEK_END);
long flen = ftell(fpr)+1;
char *pflen = (char*)malloc(flen*sizeof(char));
rewind(fpr);
fgets(pflen,flen,fpr);
fclose(fpr);
printf("%s\n",pflen);
free(pflen);
}