C语言 文件操作
来源:互联网 发布:淘宝店铺冻结能退货吗 编辑:程序博客网 时间:2024/06/05 07:44
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>int main(void) { static int a[100], a2[100]; FILE * fp1; FILE * fp2; FILE * fp3; int num, i; int index = 0; float fnum = 1.0f / 7.0f, fnum2 = 0.0; if ((fp1 = fopen("C:\\1.txt", "r")) == NULL) { puts("File 1.txt cannot be opened !"); exit(1); } if ((fp2 = fopen("C:\\2.txt", "w+")) == NULL) { puts("File 2.txt cannot be opened !"); exit(1); } if ((fp3 = fopen("C:\\3.bin", "wb+")) == NULL) { puts("File 3.bin cannot be opened !"); exit(1); } while (fscanf(fp1, "%d", &num) == 1) { a[index++] = num; fprintf(fp2, "%d ", num); } /* ungetc(' ', fp2); */ putc('\n', fp2); fputs("Hello file1 !", fp2); printf("The offset of fp1 is: "); printf("%d\n", ftell(fp1)); printf("The offset of fp2 is: "); printf("%d\n", ftell(fp2)); fseek(fp1, 0L, SEEK_SET); fseek(fp2, 0L, SEEK_SET); /* for (i = 0; i < index; i++) { printf("%d ", a[i]); } putchar('\n'); */ if (fwrite(a, index * sizeof(int), 1, fp3) != 1) { puts("fwrite error !"); exit(1); } fseek(fp3, 0L, SEEK_SET); /* 还原fp3 */ if (fread(a2, sizeof(int), index, fp3) != index) { puts("fread error !"); exit(1); } puts("The integers read from fp3 is: "); for (i = 0; i < index; i++) { printf("%d ", a2[i]); } putchar('\n'); fseek(fp3, 0L, SEEK_END); if (fwrite(&fnum, sizeof(float), 1, fp3) != 1) { puts("The float number cannot be writen to the file !"); exit(1); } fseek(fp3, -1L * sizeof(float), SEEK_END); if (fread(&fnum2, sizeof(float), 1, fp3) != 1) { puts("The float number in the file is failed to read !"); exit(1); } printf("%.7f\n", fnum2); fclose(fp1); fclose(fp2); fclose(fp3); return 0;}
0 0
- c语言文件操作
- C语言文件操作
- C语言文件操作
- C语言文件操作
- C语言文件操作
- C语言文件操作
- C语言文件操作
- c语言文件操作
- C语言文件操作
- C语言文件操作
- C语言文件操作
- C语言文件操作
- C语言文件操作
- C语言文件操作
- c语言文件操作
- c语言 文件操作
- c语言文件操作
- C语言文件操作
- PL/SQL 注释(Comment)
- Android中的android.text.Html类学习(补全了TagHandler部分)
- sar命令详解
- 酷宝童车完美创业支持体系 助力财富梦想
- 搭建xampp和wordpress问题解决
- C语言 文件操作
- 更新Anadroid SDK Tooks之后,Eclipse提示No update were found
- android socket通信(上)
- json学习 资料转载
- vs2012 二进制“==”: 没有找到接受“...”类型的左操作数的运算符(或没有可接受的转换)
- 浅析linux 下的/etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 文件
- LeetCode N-Queens II
- 关于cocos2dx RenderTexture的newImage为空
- 酷宝童车书城推荐金融一骗千金(史上十大金融欺诈案)