C语言的读写文件处理
戻る
- 使用feof
- 读不同输入的程序
- Perl的读文件处理非常简单,C比较繁琐。一点一点地学一下,目标是用较简练的语句读CSV文件。
删除文件和改名
#include
int main(void)
{
if(remove("tst.txt") == 0 )
puts("Deleted.");
else
puts("Can't delete.");
rename("tstfile.txt","tst.txt");
return 0;
}
/*
运行结果
Deleted.
*/
文件的Text mode 和 binary mode
#include
int main(void)
{
FILE *fp;
char c;
fp = fopen("smp.txt","r");
printf ("Text output is ");
while (( c = fgetc(fp)) != EOF)
printf ("%02X ",c);
printf ("\n");
fp = fopen("smp.txt","rb");
printf ("Binary output is ");
while (( c = fgetc(fp)) != EOF)
printf ("%02X ",c);
return 0;
}
/*
运行结果
Text output is 41 42 43 44 45 46 0A
Binary output is 41 42 43 44 45 46 0D 0A
*/
#include
int main(void)
{
FILE *fp;
char c;
fp = fopen("smp1.txt","w");
fprintf(fp,"ABC\n");
fp = fopen("smp2.txt","wb");
fprintf(fp,"ABC\n");
return 0;
}
/*
运行结果
ABC
*/
1行1行地读数据
#include
#include
int main(void)
{
FILE *fin, *fout;
char ss[256];
if ((fin=fopen("infile.txt","r")) == NULL ) {
printf("Can't open the file infile.txt.\n");
exit(1);
}
if ((fout=fopen("outfile.txt","w")) == NULL ) {
printf("Can't write to the file outfile.txt\n");
exit(1);
}
while (fgets(ss,256,fin) != NULL) {
fputs(ss,fout);
}
fclose(fin);
fclose(fout);
return 0;
}
使用feof
#include
int main(void)
{
FILE *fp;
int ch;
int n;
fp = fopen("input.txt", "r");
while (!feof(fp)) { // 一直读到文件最后
ch = fgetc(fp); // 读一文字
printf("%d ",ch); //10进制输出
}
fclose(fp);
printf("\n");
fclose(fp);
return 0;
}
输入
XYZ
输出
88 89 90 10 -1
88=>X
10=>\n
-1=>EOF
读不同输入的程序
#include
#include
int main(void)
{
FILE *fp;
int ch, dt;
char ss[88];
if ((fp=fopen("inputfile.txt","w")) == NULL ) {
printf("Can't open inputfile.txt.\n");
exit(1);
}
fprintf(fp, "%c", 'S');
fprintf(fp, "%s\n", "spiral_vector_theory");
fprintf(fp, "%d\n", 3149256);
fclose(fp);
if ((fp=fopen("inputfile.txt","r")) == NULL ) {
printf("Can't open outputfile.txt.\n");
exit(1);
}
ch = fgetc(fp); printf("ch=%c\n", ch);
fscanf(fp, "%s", ss); printf("ss=%s\n", ss);
fscanf(fp, "%d", &dt); printf("dt=%d\n", dt);
fclose(fp);
return 0;
}
运行结果:
ch=S
ss=spiral_vector_theory
dt=3149256
ferror和clearerr的例子
#include
#include // for exit()
int main(void)
{
FILE *fi;
if ((fi=fopen("tst.txt","r")) == NULL) exit(1);
fputc('A',fi);
if (ferror(fi)) puts("1:err"); else puts("1:no-err");
if (ferror(fi)) puts("2:err"); else puts("2:no-err");
clearerr(fi);
if (ferror(fi)) puts("3:err"); else puts("3:no-err");
fputc('A',fi);
if (ferror(fi)) puts("4:err"); else puts("4:no-err");
rewind(fi);
if (ferror(fi)) puts("5:err"); else puts("5:no-err");
fclose(fi);
return 0;
}
/*
以write mode打开
if ((fi=fopen("tst.txt","w")) == NULL) exit(1);
1:no-err
2:no-err
3:no-err
4:no-err
5:no-err
以read mode打开
if ((fi=fopen("tst.txt","r")) == NULL) exit(1);
1:err
2:err
3:no-err
4:err
5:no-err
*/
fflush
#include
int main(void)
{
FILE *fout;
char buf[200];
if ((fout=fopen("outfile.txt","w")) == NULL ) {
printf("Can't open the file outfile.txt.\n");
}
while (gets(buf) != NULL) {
fflush(fout);
puts(buf);
}
return 0;
}
/*
运行结果
test for fflush!
test for fflush!
但是outfile.txt没有写入该语句,WHY?
fflush的测试没有成功!
*/
fsetpos,fsetpos
#include
#include
int main(void)
{
FILE *fp;
int i;
fpos_t pos;
if ((fp=fopen("tst.txt","w")) == NULL ) exit(1);
fputs("ABCDEFJH888888",fp);
fclose(fp);
if ((fp=fopen("tst.txt","r")) == NULL ) exit(1);
for (i=1; i<=6; i++)
putchar(fgetc(fp));
putchar(':');
fgetpos(fp,&pos); // 取得文件指针位置
for (i=1; i<=6; i++)
putchar( fgetc(fp));
fsetpos(fp,&pos); // 文件指针位置复原
putchar('/');
for (i=1; i<=6; i++)
putchar( fgetc(fp));
putchar('\n');
fclose(fp);
return 0;
}
/*
运行结果
ABCDEF:JH8888/JH8888
*/
fputc
#include
#include
int main(void)
{
FILE *fp;
if ((fp=fopen("tst.txt","w")) == NULL ) exit(1);
fputc('X',fp);
fclose(fp);
return 0;
}
/*
fputc("X",fp);==>双引号不对,见下面compile的信息
test081014.c
test081014.c(10) : warning C4047: 'function' : 間接参照のレベルが 'int ' と 'char
[2]' で異なっています。
test081014.c(10) : warning C4024: 'fputc' : の型が 1 の仮引数および実引数と異な
ります。
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:test081014.exe
*/
Block read and write
#include
#include
#define SIZE 11
int main(void)
{
FILE *fp;
char buf[3][SIZE+1] = {"XXXXXXXX:22","XXXXYYYY:33","XXXXZZZZ:46"};
char ss[80];
if ((fp = fopen("tst.txt","w")) == NULL) exit(1);
fwrite(&buf[0], SIZE, 1, fp);
fwrite(&buf[1], SIZE, 1, fp);
fwrite(&buf[2], SIZE, 1, fp);
fclose(fp);
if ((fp = fopen("tst.txt","r")) == NULL) exit(1);
fread(ss, SIZE, 3, fp);
ss[SIZE*3] = '\0';
printf("%s\n", ss);
fclose(fp);
return 0;
}
/*
运行结果:
XXXXXXXX:22XXXXYYYY:33XXXXZZZZ:46
*/
Block read and write,通过结构体处理
#include
#include
typedef struct person {
char name[20];
int age;
} Person;
int main(void)
{
FILE *fp;
Person dt[3] = {{"XXXXXXXX",22},{"YYYYYYYY",33},{"ZZZZZZZZ",44}};
Person mindan[10];
int i;
if ((fp = fopen("tst.txt","w")) == NULL) exit(1);
fwrite(dt, sizeof(Person), 3, fp);
fclose(fp);
if ((fp = fopen("tst.txt","r")) == NULL) exit(1);
fread(mindan, sizeof(Person), 3, fp);
for ( i=0; i<=2; i++) {
printf("i=%d %s %d\n", i, mindan[i].name,mindan[i].age);
}
fclose(fp);
return 0;
}
/*
运行结果
i=0 XXXXXXXX 22
i=1 YYYYYYYY 33
i=2 ZZZZZZZZ 44
*/
freopen
#include
#include
int main(void)
{
printf("test string1\n");
if (freopen("tst.txt","w",stdout) == NULL ) {
printf("Can't open tst.txt\n");
exit(1);
}
printf("test string2\n");
return 0;
}
/*
运行结果
test string1
文件tst.txt,test string2
*/
fscanf,fseek
#include
int main(void)
{
FILE *fp;
int dt;
if ((fp=fopen("tst.txt","r")) == NULL ) exit(1);
fseek(fp, 10L, SEEK_SET);
fscanf(fp,"%d",&dt);
printf("d1=%d\n",dt);
fseek(fp, -5L, SEEK_END);
fscanf(fp,"%d",&dt);
printf("d2=%d\n",dt);
return 0;
}
/*
tst.txt=>88888888 2222222222
运行结果
d1=222222222
d2=22222
*/
ftell
#include
#include
int main(void)
{
FILE *fp;
int i;
long pos;
if ((fp=fopen("tst.txt","w")) == NULL ) exit(1);
fputs("AB\nCDEFGHIJ8888",fp);
fclose(fp);
if ((fp=fopen("tst.txt","r")) == NULL ) exit(1);
for (i=1; i<=4; i++)
printf("%02x ",fgetc(fp));
putchar(': ');
printf(": ");
pos = ftell(fp);
for (i=1; i<=4; i++)
printf("%02x ",fgetc(fp));
fseek(fp, 4L, SEEK_SET);
printf("/ ");
for (i=1; i<=4; i++)
printf("%02x ",fgetc(fp));
fseek(fp, pos, SEEK_SET);
printf("# ");
for (i=1; i<=4; i++)
printf("%02x ",fgetc(fp));
putchar('\n');
fclose(fp);
return 0;
}
/*
运行结果
ABCDEF:JH8888/JH8888
tsc.txt
AB
CDEFGHIJ8888
*/
戻る