C文件操作--示例代码存放
from http://blog.csdn.net/lxf464384/archive/2007/10/27/1848714.aspx
Example/* FOPEN.C: This program opens files named "data" * and "data2".It uses fclose to close "data" and * _fcloseall to close all remaining files. */#include <stdio.h>FILE *stream, *stream2;void main( void ){ int numclosed; /* Open for read (will fail if file "data" does not exist) */ if( (stream = fopen( "data", "r" )) == NULL ) printf( "The file 'data' was not opened\n" ); else printf( "The file 'data' was opened\n" ); /* Open for write */ if( (stream2 = fopen( "data2", "w+" )) == NULL ) printf( "The file 'data2' was not opened\n" ); else printf( "The file 'data2' was opened\n" ); /* Close stream */ if( fclose( stream ) ) printf( "The file 'data' was not closed\n" ); /* All other files are closed: */ numclosed = _fcloseall( ); printf( "Number of files closed by _fcloseall: %u\n", numclosed );}OutputThe file 'data' was openedThe file 'data2' was openedNumber of files closed by _fcloseall: 1
RemarksThe fseek function moves the file pointer (if any) associated with stream to a new location that is offset bytes from origin. The next operation on the stream takes place at the new location. On a stream open for update, the next operation can be either a read or a write. The argument origin must be one of the following constants, defined in Stdio.h: SEEK_CUR Current position of file pointer SEEK_END End of file SEEK_SET Beginning of file Example/* FSEEK.C: This program opens the file FSEEK.OUT and * moves the pointer to the file's beginning. */#include <stdio.h>void main( void ){ FILE *stream; char line[81]; int result; stream = fopen( "fseek.out", "w+" ); if( stream == NULL ) printf( "The file fseek.out was not opened\n" ); else { fprintf( stream, "The fseek begins here: " "This is the file 'fseek.out'.\n" ); result = fseek( stream, 23L, SEEK_SET); if( result ) printf( "Fseek failed" ); else { printf( "File pointer is set to middle of first line.\n" ); fgets( line, 80, stream ); printf( "%s", line ); } fclose( stream ); }}OutputFile pointer is set to middle of first line.This is the file 'fseek.out'.
/**一个c语言文件操作例子代码参考**文件使用方式 意 义*“rt” 只读打开一个文本文件,只允许读数据*“wt” 只写打开或建立一个文本文件,只允许写数据*“at” 追加打开一个文本文件,并在文件末尾写数据*“rb” 只读打开一个二进制文件,只允许读数据*“wb” 只写打开或建立一个二进制文件,只允许写数据*“ab” 追加打开一个二进制文件,并在文件末尾写数据*“rt+” 读写打开一个文本文件,允许读和写*“wt+” 读写打开或建立一个文本文件,允许读写*“at+” 读写打开一个文本文件,允许读,或在文件末追加数 据*“rb+” 读写打开一个二进制文件,允许读和写*“wb+” 读写打开或建立一个二进制文件,允许读和写*“ab+” 读写打开一个二进制文件,允许读,或在文件末追加数据*·字符读写函数 :fgetc和fputc·字符串读写函数:fgets和fputs·数据块读写函数:freed和fwrite·格式化读写函数:fscanf和fprinf对于文件使用方式有以下几点说明:1. 文件使用方式由r,w,a,t,b,+六个字符拼成,各字符的含义是:r(read): 读w(write): 写a(append): 追加t(text): 文本文件,可省略不写b(banary): 二进制文件*/int main(){FILE *fp;char ch;if((fp=fopen("e10_1.c","rt"))==NULL){printf("Cannot open file strike any key exit!");exit(1);}ch=fgetc(fp);while (ch!=EOF){putchar(ch);ch=fgetc(fp);}fclose(fp);return 0;}
/**二、写字符函数fputc fputc函数的功能是把一个字符写入指定的文件中,函数调用的 形式为: fputc(字符量,文件指针); 其中,待写入的字符量可以是字符常量或变量, 例如:fputc('a',fp);其意义是把字符a写入fp所指向的文件中。 对于fputc函数的使用也要说明几点:1. 被写入的文件可以用、写、读写,追加方式打开, 用写或读写方式打开一个已存在的文件时将清除原有的文件内容,写入字符从文件首开始。如需保留原有文件内容,希望写入的字符以文件末开始存放,必须以追加方式打开文件。被写入的文件若不存在,则创建该文件。2. 每写入一个字符,文件内部位置指针向后移动一个字节。3. fputc函数有一个返回值,如写入成功则返回写入的字符, 否则返回一个EOF。可用此来判断写入是否成功。[例10.2]从键盘输入一行字符,写入一个文件, 再把该文件内容读出显示在屏幕上。*/int main(){FILE *fp;char ch;if((fp=fopen("e10_1.c","wt+"))==NULL){printf("Cannot open file strike any key exit!");exit(1);}printf("input a string:\n");ch=getchar();while (ch!='\n'){fputc(ch,fp);ch=getchar();}rewind(fp);ch=fgetc(fp);while(ch!=EOF){putchar(ch);ch=fgetc(fp);}printf("\n");fclose(fp);return 0;}