北京大学计算概论DataFilesSK2007B.ppt_第1页
北京大学计算概论DataFilesSK2007B.ppt_第2页
北京大学计算概论DataFilesSK2007B.ppt_第3页
北京大学计算概论DataFilesSK2007B.ppt_第4页
北京大学计算概论DataFilesSK2007B.ppt_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Lesson 17 Data Files 2,Hu Junfeng 2007/12/05,2,The Arrangements:,Dynamic Data Structures C+ introduction (part1) C+ introduction (part2) Database introduction (?) Review January 7, the happy ending.,3,Todays objectives,Advanced string operations Random File Access Passing and Returning File Pointers

2、 Binary Files Dynamic Memory Allocation Linked List Basic,4,Reading from Text Files EOF,C appends the low-value hexadecimal byte 0 x00 as the end-of-file (EOF) sentinel when the file is closed EOF sentinel is never counted as part of the file,5,Reading from a Text File (exp.),6,Reading from and Writ

3、ing to Text Files(cont.),Prototypes in stdio.h Examples Examples fgetc(inFile); fgets(message,10,inFile); fscanf(inFile,%lf, fgetc() and fscanf() return EOF when the end-of-file marker is detected fgets() returns a NULL instead,7,Standard Device Files,stdin data stream (read) from keyboard stdout da

4、ta stream (write) from monitor stderr data stream (write) for error message,8,Can be replaced by getchar();,9,Get a line from stream,There are two line-oriented I/O functions, i.e. fgets() and fputs(), which allow us read and write lines instead of chracters. The function prototypes are:char *fgets(

5、char *s, int n, FILE *stream) ;int *fputs(const char *s, FILE *stream);The second argument, n, of fgets() specifies the size of the array in which the string is stored.,10,Prog11.4,11,char * strstr(char *s1, const char *s2);,strstr returns a pointer to the beginning of the sub-string or NULL if not

6、found. Exp: char string1 = “paper tiger; char string2=“tiger; void *pointer; pointer = strstr(string1, string2); ,12,13,char *strtok( char *str1, const char *str2 );,The strtok() function returns a pointer to the next token in str1, where str2 contains the delimiters that determine the token. strtok

7、() returns NULL if no token is found. In order to convert a string to tokens, the first call to strtok() should have str1 point to the string to be tokenized. All calls after this should have str1 be NULL.,14,char *strtok( char *str1, const char *str2 );,15,16,Text to HTML ,17,18,ASP and B/S Archite

8、cture,request,Active server pages,respond,19,Random File Access,fseek(fileName, offset, origin),file stream,File* fp;,program,20,Random File Access,rewind() resets the current position to the start of the file rewind(inFile) fseek() allows the programmer to move to any position in the file fseek(fil

9、eName, offset, origin) Origin: SEEK_SET, SEEK_CUR, and SEEK_END ftell() returns the offset value of the next character that will be read or written ftell(inFile);,21,Random File Access (continued),Examples of fseek() are fseek(inFile,4L,SEEK_SET); fseek(inFile,4L,SEEK_CUR); fseek(inFile,-4L,SEEK_CUR

10、); fseek(inFile,0L,SEEK_SET); fseek(inFile,0L,SEEK_END); fseek(inFile,-10L,SEEK_END);,22,Random File Access (exp),23,Passing and returning file pointer,#include #include FILE *getOpen(); /* function prototype */ void inOut(FILE *); /* function prototype */ int main() FILE *outFile; outFile = getOpen

11、(); inOut(outFile); fclose(outFile); return 0; ,FILE *getOpen() FILE *fpointer; char name13; printf(nEnter a file name: ); gets(name); fpointer = fopen(name,w); if(fname = NULL) printf(nFailed to open the file %s.n, name); exit(1); return(fpointer); ,24,第三次附加作业(可选) 唐诗鉴赏网站的自动生成,要求: 实现方便的诗歌浏览功能。 设计(部分实现)诗歌浏览页面的相关链接

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论