已阅读5页,还剩25页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
LOGOChapter Seven FileContentsIntroduction1Open/close file2Read/write file 3Check the state of the opening file4The location of the file pointer5How to get the data while run a program?Where will the result in ?How can input the result of a program to another program ?IntroductionThe procedure of C language Edit LinkCreate the program textCompileCompile it successfullyLink it to the C function library Introductionprogramformat outputcharacter outputstring outputinitializationassignment input from keyboardmemorymemoryformat inputcharacter inputstring input Read data from a file Write data to a fileIntroductionFile is the gather of related information saved in memory device (assistant memory )OS manages system in filesProcessing files means file input/out processing.Procedure 1 open file2 file read/write 3 close file.Interface input/out devices are treated as files in C .File accessing methods:1) Buffered I/O (or stream I/O)2) Unbuffered I/O (or low-level I/O)IntroductionBuffered (temporary storage location)Well discuss Buffered I/O in our lectureIn buffered I/O,-open a communication area in RAMDISkPROGRAMOutput file buffer Input file bufferThere are two ways to deal with file:Binary fileASCII file IntroductionStream is the abstract of file used to deal with file. There are two kinds of streams in C.The character is ASCII code .one ASCII code occupies one byte , new line n is end of line .After read /write a character from /to ASCII file , the fp increases automatically to guarantee the read /write sequences.Text stream Binary streamStoring data to disk in the form the same as in RAM .Example : 10000 In binary file , use 2 bytes In ASCII file , use 5 bytesOpen/close fileHow to magange a file in C ? PointerOperations open file( load file ) operate file using functions close file ( remove file from memory)BDFACEA file systemcreat a new filedelete an old one read/write access a file by its namemanage the memory file usedlimit the users rightOpen/close fileFile pointer,points to a structure that contains information about the file, such as the location of a buffer , the current character position in the buffer, whether the file is being read or written, and whether errors or end of file have occurred. Users dont need to know the details ,because the definitions obtained from include a structure declaration called FILE. I am a student .2 The function of opening a file :FILE *fp; uppercase letters ,otherwise ,cant be recognized, defined to standard structure , key-word # include is necessaryfopen ( filename ,*mode );stringchar arraychar pointer paths are allowed1 files pointer :opening a file use function fopen ( ) readwriteappendOpen/close fileOpen/close filefp=fopen (“ file.txt ” , “r ” ); open file.txt , read in ASCIIOpening a file means.returns a file pointerspecify the operation to be donedefining a file name firstpoints to the first location of file buffer.r-readfile.txtwhich The returned value is assigned to a file pointer. If process is successful , the returned value is a file pointer. (address ) FLIE *fp; Otherwise, return NULL pointer.if ( ( fp = fopen ( “file.txt” “r ” ) = = NULL ) printf ( “ cant open file n”); exist (1 ) ; Open/close fileSymbol Explanationr readw writea appendt Text streamb Binary stream+ Permit read/write If fopen fails , return value is zero ( NULL pointer) when:1)fopen wants to open a non existed file for reading .2) read a file not permitted read .3) create a file but disk is full .4) disk sector is bad .3 Close a fileClose a file opened ,argument Open/close fileOpened file must be closed before ending a program, otherwise, data may be lost .Writes string into file pointed by streamFile read/writefgets(string,n,stream)Reads a string of up to n-1 characters from stream and places them into stringfputs(string, stream)1 read/write file functionsfgetc (fp), fputc(fp) read/write a char from /to file read : c = fgetc(fp); read a char from named file.write : fputc (c,fp ); write c to named file.Prototype in stdio.h return EOF (-1) if a) at the end of file; b) detecting an error;Using function fgets() and fputs() can realize string input/ouput in files.main ( int argc, char *argv ) int c ; FILE *fpr, *fpd;if ( argc!=3) putchar(007);puts(“ Usage: copy file1, file 2“);exit( ); if ( (fpr=fopen (argv1 ,“r“) = NULL ) printf(“%c“,007);printf(“file %s cant open n“, argv 1);exit( ); if ( fpd = fopen ( argv2 ,“w“ )= NULL) printf ( “ files %s cant openn “ , argv2);exit ( ); while( c=fgetc(fpr)!= EOF)fputc(c, fpd);fclose (fpr);fclose (fpd);Input three strings( file name)Source file name object file name The function of this program is to copy file1 to file2#includemain() FILE *fp; char c1;fp=fopen(“file.dat”,”r”);c1=fgetc(fp);putchar(c1); fclose(fp);#includemain() FILE *fp; char ch=a;fp=fopen(“file.dat”,”w”);fputc(ch,fp);fclose(fp); Read the character from file.dat write character a to file.dat . File read/writefscanf ( stream, format , arg,.)Reads data from stream using format conversion specifier into address specifierd by arg .fprintf (stream,format, arg,.)Writes arg into stream using format conversion specifier.main( ) FILE *fp; char *c1=“Turbo C”; char c210;fp=fopen(“file2.dat”,”w”);fputs(c1,fp); fclose(fp);fp=fopen(“file2.dat”,”r”);fgets(c2,8,fp);printf(“%sn”,c2); fclose(fp); File read/write read a string from determined file (fp) , have read 7 chars;Returned value: normal the 1st location of the string; error or end of file NULLwrite a string to determined file (fp)Returned value: normal: the number of chars; error : NULLmain() FILE *fp;char *c1=“Turbo C”;char c210;fp=fopen(“file3.dat”,”w”);fprintf(fp,”%s”,c1); fclose(fp);fp=fopen(“file3.dat”,”r”);fscanf(fp,”%s”,c2);printf(“%sn”,c2); fclose(fp); File read/writeIt is similar to printf , add fp file pointerIt is similar to scanf, add fp file pointermain( ) FILE *fp; char str81, filename80;gets(filename);if(fp=fopen(filename,”w”)=NULL) printf(“can not open this filen”); exit(0); while(strlen(gets(str)0 ) fputs(str,fp);fputs(“n”,fp); fclose(fp); Input several strings , then save them to diskCan you use “filename” here ?How many strings should you input ?File read/write fread reads n items of size length from stream into buffer. File read/writefwrite( ) / fread( ) * Two standard library functions are available to facilitate the storage of data arrays in binary forms. These are fwrite( ) and fread( ).fwrite( buffer, size, n, stream); fread( buffer, size, n, stream); buffer :a pointer to the start of the buffer to be written into stream. n: items in the buffer of length size in bytes.when computer are started, 3 standard files are opened automatically, and 3 associated file pointers are provided 。 User neednt write statements to open standard files and after program are executed,standard files automatically closed.2 Interface input/out devices stdin file inputstdout standard output pointerstderr errorexit(1) : return to os . Innormal existexit(0) : - normal exist .3 Exit functions program will stop right away.File read/writeThe files state is necessary when using input/out functions ,especially testing the end of a file. There are one couple of ways to test the end of file1 EOFWhen reading /writing ASCII file, EOF is used to test if the file pointer points to the end of file, if so, the value of EOF is -1 Cant suit for binary file 2 function feof ( ) feof(file pointer name )The return value is nonzero at the end of file ,Otherwise , it is not the end of file .Check the state of the opening file while(feof(fp)=0)c=fgetc(fp);Check the state of the opening file while(c=fgetc(fp)!=EOF) putchar( c ); Example : read characters from a file The location of he file pointer1 rewindrewind( file pointer name ) Sets file pointer to stream to beginning of file2 fseek Locates file pointer for stream at offset bytes from origins fseek(file pointer name , offsets, origin ) No return value Generally it is Used for binary file0:the begining1:current position 2:the end The number of bytes + move backwards- move forwards return: a zero if the pointer was reset without error.3、 The current position ftell( file pointer name ) Returns current position of file pointed from stream Return 1L means there is something wrong The location o f the file pointerWhere is the current position of the file pointer?0 Beginning of file1 Current position of file pointer2 End of filefseek(file pointer name , distance, origin )main ( int argc, char *argv ) char s80; int a;FILE *fp;if ( (fp=fopen (“test“ ,“w“) = NULL ) printf(“cant open file“ );exit( ); fscanf(stdin, “%s%d“, s, /*read from keyboard*/fprintf(fp, “%s %d“, s, a); /* write to file*/fclose(fp);if ( (fp=fopen (“test“ ,“r“) = NULL ) printf(“cant open file“
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2026学年1和许多数学说课稿
- 2025-2026学年4和5的点数说课稿
- 船体冷加工安全培训效果考核试卷含答案
- 2025-2026学年大班拍网球说课稿
- 手风琴装配工安全技能能力考核试卷含答案
- 纺织品裁剪工安全生产规范知识考核试卷含答案
- 2025-2026学年二年级写动物说课稿反思
- 2025-2026学年七年级跳山羊教学说课稿
- 印染成品定等工创新方法知识考核试卷含答案
- 硅晶片抛光工诚信品质水平考核试卷含答案
- 3.2.1 分数除以整数 教学课件2026-2027学年人教版数学六年级上册
- 译林版七年级英语上册知识清单
- 2026秋冀少版新教材七年级上册生物学每课知识点清单
- 中国重症患者液体管理专家共识(2026版)
- 2026年全国翻译专业资格考试(CATTI)三级口译英语口译综合能力真题
- 2026国睿防务公司反无人机雷达总体工程师岗位招聘笔试历年参考题库附带答案详解
- 雨课堂学堂在线学堂云《声纳技术(中国人民解放军海军航空)》单元测试考核答案
- 重症医学临床路径标准化
- 稻盛和夫心法课件
- 雨课堂学堂在线学堂云《临床伦理与科研道德(山东大学)》单元测试考核答案
- 2025年住院医师规范化师资培训考试题附答案
评论
0/150
提交评论