epub 33-8

Similar documents
C/C++ - 文件IO

C

实际问题 : 1 职工信息管理系统 2 学生选课系统 3 飞机订票系统 4 图书信息管理系统 5 图象处理

新版 明解C言語入門編

新・明解C言語入門編『索引』

プログラムの設計と実現II

C/C++ - 函数

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

附錄C.doc

untitled

新・解きながら学ぶC言語

untitled

C 1

攝 心 為 戒, 因 戒 而 生 定, 由 定 而 生 慧

C/C++ - 字符串与字符串函数

CC213

C/C++ 语言 - 循环

PowerPoint 演示文稿

C/C++ - 字符输入输出和字符确认

C++ 程式設計

untitled

nooog

static struct file_operations gpio_ctl_fops={ ioctl: gpio_ctl_ioctl, open : gpio_open, release: gpio_release, ; #defineled1_on() (GPBDAT &= ~0x1) #def

3.1 num = 3 ch = 'C' 2


chap12.ppt

C/C++程序设计 - 字符串与格式化输入/输出

Microsoft Word - 实用案例.doc

Microsoft Word - chap13.doc

第3章.doc

C语言的应用.PDF

2015年计算机二级(C语言)模拟试题及答案(四)

华恒家庭网关方案

C

ebook15-C

第7章 数组

untitled

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

,,,,,,,,,, ( http: \ \ www. ncre. cn,, ) 30,,,,,,,, C : C : : 19 : : : /16 : : 96 : : : ISBN 7

FY.DOC

_汪_文前新ok[3.1].doc

第一章

IO

文件

C/C++语言 - 运算符、表达式和语句

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

ebook8-30

新版 明解C++入門編

ebook15-2

11. 下列关于文件的结论中正确的是 ( ) A. 对文件操作必须先关闭文件 B. 对文件操作必须先打开文件 C. 对文件的操作顺序没有统一规定 D. 以上三种答案全是错误的 12. 若以 a+ 方式打开一个已存在的文件, 则以下叙述正确的是 ( ) A. 文件打开时, 原有文件内容不被删除, 位置

C/C++语言 - C/C++数据

C C

2013_423WBD_HKPanel_JSC_01

untitled

<4D F736F F F696E74202D20312EB9FEB6FBB1F5B9A4D2B5B4F3D1A7D5E7C1BCA3BAC3E6CFF2D1D0BEBFC9FAB8B4CAD4B5C4BDE1B9B9BBAFC3E6CAD4BFBCBACBCCBDCBF7D3EBCAB5BCF92E BBCE6C8DDC4A3CABD5D>

C/C++语言 - 分支结构

中北大学常规事项财务报销操作指南

CC213

Generated by Unregistered Batch DOC TO PDF Converter , please register! 浙江大学 C 程序设计及实验 试题卷 学年春季学期考试时间 : 2003 年 6 月 20 日上午 8:3

[改訂新版]C言語による標準アルゴリズム事典

C/C++ - 结构体、共用体、枚举体

untitled

第3章 类型、运算符与表达式

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

Microsoft PowerPoint - C_Structure.ppt

untitled

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

C_Ch12

P9 陈 麻 婆 传 奇 目 录 卷 首 语 文 学 地 标 04 黄 昏 的 戏 剧 ( 十 首 ) 雷 平 阳 小 说 看 台 09 陈 麻 婆 传 奇 ( 连 载 ) 陈 显 寰 37 一 个 叫 吴 伟 强 的 少 年 死 者 陈 湘 涛 散 文 天 地 46 我 背 着 我 的 死 吉 狄

净 利 润 和 扣 除 非 经 常 性 损 益 后 归 属 于 母 公 司 股 东 的 净 利 润 分 别 为 亿 元 和 亿 元 ; 3 假 设 本 公 司 2016 年 扣 除 非 经 常 性 损 益 前 归 属 于 母 公 司 股 东 的 净 利 润 分 别 为 6

游戏攻略大全(五十六).doc

牧 者 心 聲 要 因 心 懷 平 而 作 惡 要 謹 慎 言 行 免 得 舌 頭 犯 罪 ; 惡 人 時 候 要 用 嚼 環 勒 住 口 ( 詩 三 十 九 1) 今 天 社 會 和 教 會 裏 極 其 渴 望 人 能 以 具 體 行 動 勉 勵 走 善 良 正 直 路 作 好 榜 樣 ; 可 惜

2008年4月全国计算机等级考试二级C语言真题(含答案)

untitled

C PICC C++ C++ C C #include<pic.h> C static volatile unsigned char 0x01; static volatile unsigned char 0x02; static volatile unsigned cha

C/C++ - 数组与指针

硕士论文正文

C

c_cpp

ebook35-21

Microsoft PowerPoint - 07_IO.ppt

入 学 考 试 重 点 考 查 学 生 的 基 础 专 业 知 识 基 本 实 验 操 作 技 能 独 立 思 考 和 动 手 能 力 笔 试 和 面 试 的 试 题 都 有 足 够 的 难 度, 以 利 择 优 录 取 新 录 取 的 研 究 生 第 一 次 见 面, 池 先 生 会 作 一 次

untitled

2_ Bridegroom fast 1 - v1

学习MSP430单片机推荐参考书

<4D F736F F D20B5DA31D5C220C7B6C8EBCABDD3A6D3C3B3CCD0F2C9E8BCC6D7DBBACFBDCCB3CC2E646F63>

Go构建日请求千亿微服务最佳实践的副本

C6_ppt.PDF


Microsoft Word - Index.doc

(Guangzhou) AIT Co, Ltd V 110V [ ]! 2

epub 9-11

Microsoft PowerPoint - ds-1.ppt [兼容模式]

全国计算机技术与软件专业技术资格(水平)考试


第11章 可调内核参数

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc

6 C51 ANSI C Turbo C C51 Turbo C C51 C51 C51 C51 C51 C51 C51 C51 C C C51 C51 ANSI C MCS-51 C51 ANSI C C C51 bit Byte bit sbit

Transcription:

8 1) 2) 3) A S C I I 4 C I / O I / 8.1 8.1.1 1. ANSI C F I L E s t d i o. h typedef struct i n t _ f d ; i n t _ c l e f t ; i n t _ m o d e ; c h a r *_ n e x t ; char *_buff; /* /* /* /* /*

1 5 4 C FILE FILE *fp1,fp2; f p 1 f p 2 2. ANSI C FILE *fopen(char *fname,char *mode) s t d i o. h f o p e n () f n a m e f n a m e m o d e m o d e 8-1 m o d e 8-1 mode Mode r w a r b w b a b r + w + a + r b + w b + a b / / / / / / 8-1 FILE *fp; If ((fp=fopen("test.txt","w"))==null) /* printf("cannot open file \n"); N U L L N U L L s t d i o. h 0

8 155 3. ANSI C int fclose(file *stream) f c l o s e () s t r e a m f c l o s e () s t r e a m f c l o s e () 0 [ 8-1] m a i n ( ) FILE *fp; If ((fp=fopen("test.dat","rb"))==null) printf("cannot open file\n"); e x i t ( 0 ) ; / * * / if (fclose(fp)) printf("file close error!\n"); 8.1.2 1. C f g e t c f p u t c s t d i o. h int fgetc(file *stream) f g e t c ( ) E O F int fputc(int ch,file *stream) f p u t c c h fputc () E O F [ 8-2] m a i n () main( argc,argv) int argc;

1 5 6 C char *argv[]; char ch; FILE *fp; int i; i f (( f p = f o p e n ( a r g v [ 1 ]," r ")) = = N U L L ) printf("not open"); while ((ch=fgetc(fp))!=eof) p u t c h a r ( c h ); f c l o s e ( f p ); /* a r g v [ 1 ] /* m a i n () a rg c a rg v a rg c L 8-2. c L 8-2. e x e l 8-2. e x e c : \ t c > l8-2 L8-2.c a rg v [ 0 ] = " c :\ t c > l 8-2 " a rgv[1]=" L8-2.c " a rg c = 2 L8-2.c f g e t c () C E O F c : \ t c > l8-2 L8-2.c main( argc,argv) int argc; char *argv[]; char ch; FILE *fp; int i; i f (( f p = f o p e n ( a r g v [ 1 ]," r ")) = = N U L L ) printf("not open"); while ((ch=fgetc(fp))!=eof) p u t c h a r ( c h ); f c l o s e ( f p ); /* a r g v [ 1 ] /* [ 8-3] t e s t. t x t m a i n ( )

FILE fp; char ch; i f (( f p = f o p e n (" t e s t. t x t "," w ")) = = N U L L ) printf("cannot open file!\n"); while ((ch=fgetchar())!='\n') f p u t c ( c h, f p ) f c l o s e ( f p ); 8 157 /* /* / / t e s t. t x t D O S t y p e R U N I love china! D O S type t e s t. t x t c : \ t c > type test.txt I love china! 2. C s t d i o. h Char *fgets(char *str,int num,file *stream) fgets() s t r e a m n u m - 1 s t r E O F int fputs(char *str,file *stream) f p u t s () s t r 0 [ 8-4] t e s t. t x t #include <string.h> m a i n () FILE *fp; char str[128]; if ((fp=fopen("test.txt","w"))==null) /* printf("cannot open file!"); w h i l e (( s t r l e n ( g e t s ( s t r )))! = 0 ) /* f p u t s ( s t r, f p ); /* fputs("\n",fp); /*

1 5 8 C fclose(fp); /* 127 0 H e l l o! How do you do G o o d - b y e! d o s t y p e c : \ t c > type test.txt H e l l o! How do you do Good-bye! g e t s [ 8-5] t e s t 1. t x t t e s t 2. t x #include <string.h> m a i n () FILE *fp1,*fp2; char str[128]; if ((fp1=fopen("test1.txt","r"))==null) /* 1 printf("cannot open file\n"); if ((fp2=fopen("test2.txt","w"))==null) /* 2 printf("cannot open file\n"); while ((strlen(fgets(str,128,fp1)))>0) /* 0 fputs(str,fp2 ); /* 1 2 p r i n t f ("% s ", s t r ); /* f c l o s e ( f p 1 ); f c l o s e ( f p 2 );

8 159 3. s c a n f () p r i n t f () f f s c a n f () f p r i n t f ( ) int fscanf(file *stream,char *format,arg_list) int fprintf(file *stream,char *format,arg_list) s t r e a m s c a n f () p r i n t f () [ 8-6] m a i n () FILE *fp; int i; struct stu char name[15]; char num[6]; float score[2]; /* s t u d e n t ; /* if ((fp=fopen("test1.txt","w"))==null) /* printf("input data:\n"); for( i=0;i<2;i++) scanf("%s %s %f %f",student.name,student.num,&student.score[0], &student.score[1]); /* fprintf(fp,"%s %s %7.2f %7.2f\n",student.name,student.num, s t u d e n t. s c o r e [ 0 ], s t u d e n t. s c o r e [ 1 ]); /* f c l o s e ( f p ); /* if ((fp=fopen("test.txt","r"))==null) /* printf("output from file:\n"); while (fscanf(fp,"%s %s %f %f\n",student.name,student.num, & s t u d e n t. s c o r e [ 0 ], s t u d e n t. s c o r e [ 1 ])! = E O F /* printf("%s %s %7.2f %7.2f\n",student.name,student.num,

1 6 0 C student.score[0],student.score[1]); /* fclose(fp); /* input data: xiaowan j001 87.5 98.4 xiaoli j002 99.5 89.6 output from file: xiaowan j001 87.50 98.40 xiaoli j002 99.50 89.60 c :\ > type test.txt xiaowan j001 87.50 98.40 xiaoli j002 99.50 89.60 if ((fp=fopen("test1.txt","wb"))==null) /* 4. C int fread(void *buf,int size,int count,file *stream) int fwrite(void *buf,int size,int count,file *stream) fread stream count s i z e ( ) b u f fread f w r i t e () b u f ( ) c o u n t ( ) s t r e a m s i z e [ 8-7] #include "stdio.h" #include "stdlib.h" m a i n ( ) FILE *fp1;

8 161 int i; struct stu /* char name[15]; char num[6]; float score[2]; s t u d e n t ; if ((fp1=fopen("test.txt","wb"))==null) /* printf("input data:\n"); for( i=0;i<2;i++) scanf("%s %s %f %f",student.name,student.num, & s t u d e n t. s c o r e [ 0 ],& s t u d e n t. s c o r e [ 1 ]); /* fwrite(&student,sizeof(student),1,fp1); /* f c l o s e ( f p 1 ); if ((fp1=fopen("test.txt","rb"))==null) /* printf("output from file:\n"); for (i=0;i<2;i++) f r e a d (& s t u d e n t, s i z e o f ( s t u d e n t ), 1, f p 1 );/ * printf("%s %s %7.2f %7.2f\n",student.name,student.num, s t u d e n t. s c o r e [ 0 ], s t u d e n t. s c o r e [ 1 ]); /* f c l o s e ( f p 1 ); input data: xiaowan j001 87.5 98.4 xiaoli j002 99.5 89.6 output from file: xiaowan j001 87.50 98.40 xiaoli j002 99.50 89.60 C : int atoi(char *ptr) float atof(char *ptr) long int atol(char *ptr) m a t h. h

1 6 2 C s t d l i b. h [ 8-8] #include <stdlib.h> m a i n () FILE *fp1; char *temp; int i; struct stu char name[15]; /* char num[6]; /* float score[2]; s t u d e n t ; if ((fp1=fopen("test.txt","wb"))==null) for( i=0;i<2;i++) printf("input name:"); g e t s ( s t u d e n t. n a m e ); printf("input num:"); g e t s ( s t u d e n t. n u m ); printf("input score1:"); g e t s ( t e m p ); s t u d e n t. s c o r e [ 0 ] = a t o f ( t e m p ); printf("input score2:"); g e t s ( t e m p ); s t u d e n t. s c o r e [ 1 ] = a t o f ( t e m p ); /* / * /* /* /* /* f w r i t e (& s t u d e n t, s i z e o f ( s t u d e n t ), 1, f p 1 ); f c l o s e ( f p 1 ); /* if ((fp1=fopen("test.txt","rb"))==null) e x i t ( 0 ) ; p r i n t f ( " - - - - - - - - - - - - - - - - - - - - - \ n " ) ; p r i n t f ( " % - 1 5 s % - 7 s % - 7 s % - 7 s \ n ", " n a m e ", " n u m ", " s c o r e 1 ", " s c o r e 2 " ) ; p r i n t f ( " - - - - - - - - - - - - - - - - - - - - - \ n " ) ; for (i=0;i<2;i++) f r e a d ( & s t u d e n t, s i z e o f ( s t u d e n t ), 1, f p 1 ) ; p r i n t f ( " % - 1 5 s % - 7 s % 7. 2 f % 7. 2 f \ n ", s t u d e n t. n a m e, s t u d e n t. n u m, s t u d e n t. s c o r e [ 0 ], s t u d e n t. s c o r e [ 1 ] ) ;

8 163 f c l o s e ( f p 1 ) ; R U N input name:l i - y i n g input num: j 0 1 2 3 input score1:9 8. 6 5 input score2:8 9. 6 input name:l i - l i input num: j 0 1 2 4 input score1:6 8. 6 5 input score2:8 6. 6 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - n a m e n u m s c o r e 1 s c o r e 2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - l i - y i n g j 0 1 2 3 9 8. 6 5 8 9. 6 0 l i - l i j 1 2 4 6 8. 6 4 8 6. 6 0 8.1.3 C int fseek(file *fp,long d,int pos) f p d p o s P o s 0 1 2 d l o n g 0 f s e e k ( f p, 5 L, 0 ) 5 [ 8-9] f s e e k ( f p,- 10 L, 2 ) 10 rewind() ftell(file *fp) 5 #include <stdlib.h> #define n 5 m a i n ( ) FILE *fp1; /*

1 6 4 C char *temp; int i,j; struct stu char name[15]; char num[6]; float score[2]; s t u d e n t [ n ]; /* if ((fp1=fopen("test.txt","wb"))==null) for( i=0;i<n;i++) printf("input name:"); g e t s ( s t u d e n t [ i ]. n a m e ); printf("input num:"); g e t s ( s t u d e n t [ i ]. n u m ); printf("input score1:"); g e t s ( t e m p ); s t u d e n t [ i ]. s c o r e [ 0 ] = a t o f ( t e m p ); printf("input score2:"); g e t s ( t e m p ); s t u d e n t [ i ]. s c o r e [ 1 ] = a t o f ( t e m p ) ; /* /* /* /* /* fwrite(&student[i],sizeof(struct stu),1,fp1); /* fclose(fp1); /* if ((fp1=fopen("test.txt","rb+"))==null) /* p r i n t f ("- --- --- --- --- --- --- --\ n "); p r i n t f ("% - 15 s %- 7 s %- 7 s %- 7 s \ n "," n a m e "," n u m "," s c o r e 1 "," s c o r e 2 "); p r i n t f ("- --- --- --- --- --- --- --\ n "); for (i=0;i<n;i++) /* fread(&student[i],sizeof(struct stu),1,fp1); p r i n t f ("% - 15 s %- 7 s % 7. 2 f % 7. 2 f \ n ", s t u d e n t [ i ]. n a m e, s t u d e n t [ i ]. n u m, s t u d e n t [ i ]. s c o r e [ 0 ], s t u d e n t [ i ]. s c o r e [ 1 ]); /* fseek(fp1,3*sizeof(struct stu),0); /* fwrite(&student[1],sizeof(struct stu),1,fp1); /* rewind(fp1); /* p r i n t f ("- --- --- --- --- --- --- --\ n ");

8 165 p r i n t f ( " % - 1 5 s % - 7 s % - 7 s % - 7 s \ n ", " n a m e ", " n u m ", " s c o r e 1 ", " s c o r e 2 " ) ; p r i n t f ( " - - - - - - - - - - - - - - - - - - - - - \ n " ) ; for (i=0;i<n;i++) /* fread(&student[i],sizeof(struct stu),1,fp1); p r i n t f ("% - 15 s %- 7 s % 7. 2 f % 7. 2 f \ n ", s t u d e n t [ i ]. n a m e, s t u d e n t [ i ]. n u m, s t u d e n t [ i ]. s c o r e [ 0 ], s t u d e n t [ i ]. s c o r e [ 1 ]); f c l o s e ( f p 1 ) ; /* R U N input name:l i - y i n g input num: j 0 1 2 3 input score1:9 8. 6 5 input score2:8 9. 6 input name:l i - l i input num: j 0 1 2 4 input score1:6 8. 6 5 input score2:8 6. 6 input name:l i - p i n g input num: j 0 1 2 5 input score1:8 8. 5 input score2:8 4. 6 input name:w a n g - x i a n input num: j 0 1 2 6 input score1:9 8 input score2:94 input name:m a - l i n g input num: j 0 1 2 7 input score1:6 6. 5 input score2:8 0. 6 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - n a m e n u m s c o r e 1 s c o r e 2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - l i - y i n g j 0 1 2 3 9 8. 6 5 8 9. 6 0 l i - l i j 0 1 2 4 6 8. 6 4 8 6. 6 0 l i - p i n g j 0 1 2 5 8 8. 5 0 8 4. 6 0 Wang-xian j0126 98.009 4. 0 0 Ma-ling j01276 6. 5 0 8 0. 6 0 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - n a m e n u m s c o r e 1 s c o r e 2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - l i - y i n g j 0 1 2 3 9 8. 6 5 8 9. 6 0 l i - l i j 0 1 2 4 6 8. 6 4 8 6. 6 0 l i - l i j 0 1 2 4 6 8. 6 4 8 6. 6 0 Wang-xian j0126 98.009 4. 0 0

1 6 6 C M a - l i n g j 0 1 2 7 6 6. 5 0 8 0. 6 0 8.2 A N S I 1. A N S I U N I X I / O i o. h int open(char *fname,int access) f n a m e a c c e s s a c c e s s O _ R D O N LY O _ W R O N LY O _ R D W R close(int fd) U N I X #include "io.h" #include "fcntl.h" #include "sys\stat.h" m a i n ( a r g c, a r g v ) int argc; char *argv[] int fd; if((fd=open(argv[1],o_rdonly))==-1) /* printf("cannt open file!"); printf("file existent!"); if (close(fd)) printf("error in closing file\n"); 2. i o. h int read(int fd,void *buf,int count) r e a d () f d c o u n t b u f

8 167 int write(int fd,void *buf,int count) w r i t e () c o u n t b u f f d T E S T. T S T 100 b u ff e r #include "io.h" #include "stdio.h" #include "fcntl.h" m a i n () int fd; char buffer[100]; if ((fd=open("test.tst",o_rdonly))==-1) /* printf("cannot open file!\n"); if (read(fd,buffer,100)!=100) /* printf("possible read error."); 8.3 [ 8-10] a d d r. t x t t e l. t x t a d d r t e l. t x t type addr.txt h e j i e t i a n j i n g l i y i n g s h a n g h a i l i m i n g c h e n g d u w a n g p i n c h o n g q i n g type tel.txt l i y i n g 12 34 5 h e j i e 87 64 w a n g p i n 87 64 3 l i m i n g 765 432 14 1 0

1 6 8 C #include <stdlib.h> #include <conio.h> #include <string.h> m a i n ( ) FILE *fptr1,*fptr2,*fptr3; /* char temp[15],temp1[15],temp2[15]; if ((fptr1=fopen("addr.txt","r"))==null)/* if ((fptr2=fopen("tel.txt","r"))==null) if ((fptr3=fopen("addrtel.txt","w"))==null) c l r s c r (); /* while(strlen(fgets(temp1,15,fptr1))>1) /* 1 f g e t s ( t e m p 2, 15, f p t r 1 ); /* f p u t s ( t e m p 1, f p t r 3 ); /* f p u t s ( t e m p 2, f p t r 3 ); /* s t r c p y ( t e m p, t e m p 1 ); /* do /* f g e t s ( t e m p 1, 15, f p t r 2 ); f g e t s ( t e m p 2, 15, f p t r 2 ); while (strcmp(temp,temp1)!=0); r e w i n d ( f p t r 2 ); /* f p u t s ( t e m p 2, f p t r 3 ); /* f c l o s e ( f p t r 1 ); f c l o s e ( f p t r 2 ); f c l o s e ( f p t r 3 ); /*

8 169 a d d r t e l. t x t type addrtel.txt hejie tianjing 87 64 liying shanghai 12 34 5 liming chengdu 765 432 wangpin chongqing 87 64 3