C語言的讀寫文件處理
戻る
- 使用feof
- 讀不同輸入的程序
- Perl的讀文件處理非常簡單,C比較繁瑣。一點一點地學(xué)一下,目標是用較簡練的語句讀CSV文件。
刪除文件和改名
#include
int main(void)
{
if(remove("tst.txt") == 0 )
puts("Deleted.");
else
puts("Can't delete.");
rename("tstfile.txt","tst.txt");
return 0;
}
/*
運行結(jié)果
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;
}
/*
運行結(jié)果
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;
}
/*
運行結(jié)果
ABC
*/
1行1行地讀數(shù)據(jù)
#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;
}
運行結(jié)果:
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;
}
/*
運行結(jié)果
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); // 文件指針位置復(fù)原
putchar('/');
for (i=1; i<=6; i++)
putchar( fgetc(fp));
putchar('\n');
fclose(fp);
return 0;
}
/*
運行結(jié)果
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 の仮引數(shù)および実引數(shù)と異な
ります。
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;
}
/*
運行結(jié)果:
XXXXXXXX:22XXXXYYYY:33XXXXZZZZ:46
*/
Block read and write,通過結(jié)構(gòu)體處理
#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;
}
/*
運行結(jié)果
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;
}
/*
運行結(jié)果
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
運行結(jié)果
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;
}
/*
運行結(jié)果
ABCDEF:JH8888/JH8888
tsc.txt
AB
CDEFGHIJ8888
*/
戻る
西林县|
厦门市|
讷河市|
安西县|
宁海县|
兴宁市|
泾阳县|
清河县|
大同市|
乌鲁木齐县|
洛扎县|
望都县|
新源县|
大埔区|
清远市|
莒南县|
东光县|
乐至县|
肥东县|
从江县|
固阳县|
宜阳县|
武功县|
塘沽区|
衢州市|
汕尾市|
子长县|
白城市|
余干县|
紫阳县|
天水市|
开原市|
西华县|
潮安县|
宜章县|
兴业县|
万安县|
望江县|
道真|
丹凤县|
名山县|