linux文件编程:fread fwrite
2017-06-07 20:27
169 查看
已知
file1 file2
x1 x2 x3... y1 y2 y3...
x4 x5 x6... y4 y5 y6...
................ .................
file1 and file2行数列数相同
编程建立
file3
z1 z2 z3....
z4 z5 z6....
..................
满足zn = xn + yn
note
1.EOF is an indicator, a const int ,default set as -1
2.feof is a function -- int feof(FILE * stream)
returning an non-zero indicator(which is equal to EOF) if reach the end of file,else return 0.
3.a way to turn char into int
char a; //set as what you want
int b = a - '0';
file1 file2
x1 x2 x3... y1 y2 y3...
x4 x5 x6... y4 y5 y6...
................ .................
file1 and file2行数列数相同
编程建立
file3
z1 z2 z3....
z4 z5 z6....
..................
满足zn = xn + yn
note
1.EOF is an indicator, a const int ,default set as -1
2.feof is a function -- int feof(FILE * stream)
returning an non-zero indicator(which is equal to EOF) if reach the end of file,else return 0.
3.a way to turn char into int
char a; //set as what you want
int b = a - '0';
#include<stdio.h> int readfile(const char *, int *, int *, int *); void writefile(const char *, int, int, int*); int main() { int mrow, mcol; int all; int num1[100] = {0}; int num2[100] = {0}; int num3[100] = {0}; int count = 0; all = readfile("./file1", &mrow, &mcol, num1); readfile("./file2", &mrow, &mcol, num2); printf("all = %d\n", all); for(count = 0; count <= all; count++) { num3[count] = num1[count] + num2[count]; } writefile("./file3", mrow, mcol, num3); return 0; } void writefile(const char * path, int mrow, int mcol, int* num3) { FILE *fp = fopen(path, "w"); if(fp == NULL) { perror("writefile"); return; } int cr = 0, cc = 0; int c = 0; while(num3[c] != 0) { for(cr = 0; cr < mrow; cr++) { for(cc = 0; cc <= mcol; cc++) { fprintf(fp, "%d", num3[c]); c++; if(cc != mcol) { fprintf(fp, " "); } } fprintf(fp, "\n"); } } fclose(fp); return; } int readfile(const char * path, int * mrow, int * mcol, int num1[]) { FILE * fd = fopen(path, "r"); if(fd == NULL) { perror("open file1"); return 1; } char c; char buf1[100]; int i = 0; while((c = fgetc(fd)) != EOF) { buf1[i] = c; i++; } i = 0; int a = 0; int t = 0; int col = 0; int row = 0; int flag = 0; while(buf1[i + 1] != '\0') { if(buf1[i] == ' ') { a++; i++; if(flag == 0) { col++; } continue; } if(buf1[i] == '\n') { a++; i++; row++; flag = 1; continue; } t = buf1[i] - '0'; num1[a] = 10 * num1[a]; num1[a] = num1[a] + t; i++; } *mrow = row; *mcol = col; fclose(fd); return a; }
相关文章推荐
- linux操作系统下c语言编程入门 -- (3)文件操作
- Linux下perl编程读取rss文件(以CSDN和yahoo为例)
- linux嵌入式编程高手历程系列8-NFS网络文件系统配置方法
- Linux中.a,.la,.o,.so文件的意义和编程实现
- Linux网络编程一步一步学-编写一个HTTP协议的目录浏览和文件下载服务器
- Linux下C语言编程--文件的操作
- Linux中.a,.la,.o,.so文件的意义和编程实现
- [转] linux中.o,.a,.o,.so文件的意义和编程实现
- linux嵌入式编程高手历程系列6-定制uboot和文件系统
- Linux下C编程入门笔记——文件IO操作(二)
- linux中.o,.a,.o,.so文件的意义和编程实现
- Linux下C语言编程--文件的操作
- linux编程——makefile文件
- Linux下的文件I/O编程小结
- Linux网络编程一步一步学-自己编写一个HTTP协议的目录浏览和文件下载服务器
- linux编程_Makefile文件
- linux下c语言编程:文件操作
- Linux下的C编程实战(二)――文件系统编程 推荐
- linux/unix文件编程---简单例子
- linux下c语言编程:文件操作