博文
关于文件操作的一点细节(引用别人的)(2007-03-06 09:11:00)
摘要:
/* ---------------------------------------------
** Test File Function
** author: smileonce 2005-06-16
*/
#include <stdio.h>
int freadline(FILE * f, char * buf);
int main(int argc, char* argv[])
{
FILE * f;
int i;
char buf[255];
// write some number to a file for testing.
f = fopen("F:\\tmp\\FileSy\\Debug\\testfile.txt", "w");
for (i=0; i<10; i++)
fprintf(f, "origin : %d\n", i);
fclose(f);
// test ...
f = fopen("F:\\tmp\\FileSy\\Debug\\testfile.txt", "r+");
for (i=0; i<5; i++)
{
freadline(f, buf);
printf("%s\n", buf);
}
fseek(f, 0L, SEEK_CUR); //use this to refresh the file,
//same function as fflush(), fgetpos(), fsetpos().
for (i=5; i<8; i++)
fprintf(f, "replace: %d\n", i);
fclose(f);
return 0;
}
// read a line from file stream
int freadline(FILE * f, char * buf)
{
int count=0, ch;
while((ch=fgetc(f)) != EOF)
if (ch != '\n')
buf[count++] = (char)ch;
else break;
buf[count++] = '\0......