Linux应用软件编程
常见的操作系统Windows界面友好使用方便但是闭源收费。PCLinux类Unix开源免费。常用于嵌入式平台Unix收费FreeRTOS小型嵌入式平台实时性文件编程IOIOinput/output 输入/输出为什么学习文件操作保存数据掉电数据不丢失保存在硬盘外存一切皆文件硬盘、鼠标、键盘、显示器、普通文件....Linux下的文件类型ls -l 查看Linux下的文件相关详情ll文件操作方法Linux下的两种文件操作方式标准IOC标准库提供的对文件的操作接口库函数如printf、scanf文件IOLinux内核提供的对文件的操作方法系统调用文件操作思想打开文件读写文件关闭文件标准IO学习标准C库提供的一堆对文件的操作接口函数----通过man手册打开文件 fopen读写文件 fgetc/fputc 、fgets/fputs、fread/fwrite关闭文件 fclosefopen函数文件流从文件中读写数据时体现出来的字节流。文件流指针FILE *。标准IO中对文件的操作都是面向文件流指针。#include stdio.h int main(int argc, char **argv) { FILE* fpfopen(1.txt,a); if(NULL fp) { printf(fopen error\n); return -1; } fclose(fp); return 0; }fclose函数fputc函数od -c 文本文件名 以字符方式查看该文件中的数据//向文件中写入“hello world” #include stdio.h int main(int argc, char **argv) { FILE* fpfopen(1.txt,a); if(NULL fp) { printf(fopen error\n); return -1; } /* fputc(H,fp); fputc(e,fp); fputc(l,fp); fputc(l,fp); fputc(o,fp); fputc(W,fp); fputc(o,fp); fputc(r,fp); fputc(l,fp); fputc(d,fp); */ char str[]{hello world}; char* pstrhello world; int i0; while(pstr[i]!\0) { fputc(str[i],fp); i; } fclose(fp); return 0; }fgetc函数//使用fgetc实现cat的功能将一个指定的文本文件内容输出到终端。 #include stdio.h int main(int argc, char **argv) { FILE* fpfopen(1.txt,r); if(NULL fp) { printf(fopen error\n); return -1; } int retfgetc(fp); if(EOF ret) { printf(error or end of file\n); return -1; } printf(%c,ret); while(EOF !ret) { retfgetc(fp); printf(%c,ret); } fclose(fp); return 0; }//使用fgetc和fputc实现文件的拷贝 #include stdio.h int main(int argc, char** argv) { if (argc 3) { printf(Usage:./a.out srcfile dstfile\n); return -1; } FILE* fp fopen(argv[1], r); FILE* fp2 fopen(argv[2], w); if (NULL fp) { printf(fopen error\n); return -1; } int ret 0; while ((ret fgetc(fp)) ! EOF) { fputc(ret,fp2); } fclose(fp); fclose(fp2); return 0; }fgetc和getcharret getchar() ret fgetc(stdin);fputc和putcharputchar(ret) fputc(ret, stdout);fgets函数gets和fgets的区别gets只能从终端读取数据fgets可以任意文件流指针中读取fgets会保留用户收入的\n字符gets不会读取\n字符fgets是安全的gets是危险的fgets最多读取size-1个数据最后一个位置存放字符串结束标志gets没有大小的限制。//利用fgets函数实现cat的功能 #include stdio.h int main(int argc, const char *argv[]) { FILE *fp fopen(./01_fopen.c, r); if (NULL fp) { printf(fopen error\n); return -1; } char buff[128] {0}; while (1) { char *p fgets(buff, sizeof(buff), fp); if (NULL p) { //printf(error or end of file\n); break; } printf(%s, buff); } fclose(fp); return 0; }fputs函数#include stdio.h int main(int argc, const char *argv[]) { FILE *fp fopen(1.txt, w); if (NULL fp) { printf(fopen error\n); return -1; } char *pstr how are you; char str[] {hahahahaha}; fputs(hello world, fp); fputs(pstr, fp); fputs(str, fp); fclose(fp); return 0; }//使用fgets和fputs实现文件的拷贝注意只能拷贝文本文件 #include stdio.h int copy_file(const char *srcname, const char *dstname) { int ret 0; FILE *fpsrc fopen(srcname, r); FILE *fpdst fopen(dstname, w); if (NULL fpsrc || NULL fpdst) { printf(fopen error\n); return -1; } char buff[256] {0}; while (1) { char *p fgets(buff, sizeof(buff), fpsrc); if (NULL p) { break; } fputs(buff, fpdst); } fclose(fpsrc); fclose(fpdst); return 0; } int main(int argc, const char *argv[]) { if (argc 3) { printf(Usage : ./a.out srcname dstname\n); return -1; } copy_file(argv[1], argv[2]); return 0; }fwrite函数#include stdio.h struct stu { int id; char name[32]; int score; }; int main(int argc, char **argv) { FILE* fpfopen(1.txt,w); if(NULL fp) { printf(fopen error\n); return -1; } struct stu s{1,zhangsan,99}; struct stu ss[]{{2,lisi,79},{3,wanger,90},{4,zhaowu,59}}; size_t sizefwrite(s,sizeof(struct stu),1,fp); printf(size is %ld\n,size); sizefwrite(ss,sizeof(struct stu),3,fp); printf(size is %ld\n,size); fclose(fp); return 0; }fread函数#include stddef.h #include stdio.h struct stu { int id; char name[32]; int score; }; int main(int argc, char **argv) { FILE* fpfopen(1.txt,r); if(NULL fp) { printf(fopen error\n); return -1; } struct stu ss[10]; size_t sizefread(ss,sizeof(struct stu),10,fp); printf(size is %ld\n,size); for(int i0;isize;i) { printf(%d %s %d\n,ss[i].id,ss[i].name,ss[i].score); } fclose(fp); return 0; }注意fread和fwrite可用来读写字符型文件也可以用来读写二进制文件fread和fwrite大多数场景用在读写固定大小的数据如结构体。文件流定位函数fseek函数int fseek(FILE *stream, long offset, int whence); 功能实现文件流中的文件位置定位符的偏移 参数 stream要偏移的文件流 offset偏移量 whence偏移的起始位置 SEEK_SET:文件开头 offset 0 SEEK_CUR:当前读写位置 offset可正可负 SEER_END:文件末尾 offset可正可负 返回值 成功0 失败-1 long ftell(FILE *stream); 功能获取当前流所在位置到文件开头的偏移量 void rewind(FILE *stream); 功能流复位将流定位到文件开头 获取文件的大小 fseek(fp, 0, SEEK_END); len ftell(fp); rewind(fp);#include stdio.h int main(int argc, char **argv) { FILE* fpfopen(1.txt,w); if(NULL fp) { printf(fopen error\n); return -1; } long offset0; fseek(fp,5,SEEK_SET); fputc(A,fp); offsetftell(fp); printf(offset is %ld\n,offset); fseek(fp,10,SEEK_CUR); fputc(B,fp); offsetftell(fp); printf(offset is %ld\n,offset); fseek(fp,-5,SEEK_CUR); fputc(C,fp); fseek(fp,5,SEEK_END); fputc(D,fp); rewind(fp);//将流复位到文件开头 fclose(fp); return 0; }其他int printf(const char *format, ...); int fprintf(FILE *stream, const char *format, ...); 功能将格式化后的数据输出到文件 int sprintf(char *str, const char *format, ...); 功能格式化后的字符串保存在内存