受人之托,原来就感觉文件这块难。现在硬着头皮也要上,大家一起加油努力鸭!!!
文件基本操作
#include <stdio.h> #include <stdlib.h> /* 1.如何打开文件 1.1 路径 1.1.1 相对路径 1.1.2 绝对路径(计算机里面的位置如D:\...) 1.2 打开方式 r:只读 read w:写的方式 write 如果文件不存在,创建文件,如果存在,清空文件 a:追加 append 在源文件的末尾接着写(不清空) +:可读可写 d:二进制 binary r+ w+ a+ rb+ wb+ ab+ rb wb ab 2. 如何关闭文件 fclose(文件指针); */ int main() { // 1.要起一个名字在程序中表示文件 // 文件指针表示, FILE *名字 FILE* MM; // FILE* MM = NULL; // 2.把名字分配给相应的文件 // fopen("路径", "读写法"); // 错误:记住stream!=NULL代表文件打开失败 MM = fopen("girl.txt", "r"); // 如果有这个文本,就只读打开,否则报错 // 防御性编程,不会报错 if (MM == NULL) { // 如果没有,就用w+创建一个 MM = fopen("girl.txt", "w+"); } fclose(MM); printf("Hello World\n"); system("pause"); return 0; }
读写方式介绍
#include <stdio.h> #include <stdlib.h> #include <string.h> /* 比较无规律的数据 1.以字符的方式读写 file get char fgetc():读 file get char fputc(); 2.以字符串方式读写 结构化数据:结构体数据 3.格式化读写--->表格数据读写 4.以字节流形式读写 */ int main() { FILE* write = fopen("girl.txt", "w+"); char str[] = "I Love you,but you don't love me!"; // 所有的文件操作函数,都包含了移动文件指针 for (int i = 0; i < strlen(str) + 1; i++) { fputc(str[i], write); } fclose(write); FILE* read = fopen("girl.txt", "r"); int ch = fgetc(read); // 文件常识:文件末尾的标记:EOF while (ch != EOF) { putchar(ch); ch = fgetc(read); } printf("\n"); fclose(read); system("pause"); return 0; }
以字符串的方式读写
#include <stdio.h> #include <stdlib.h> #include <string.h> /* 2.以字符串方式读写 fgets() file get string fputs() file put string */ int main() { FILE* write = fopen("string.txt", "w+"); char str[] = "I Love you,but you don't love me!"; // 所有的文件操作函数,都包含了移动文件指针 fputs(str, write); fclose(write); FILE* read = fopen("string.txt", "r"); char buff[1024] = ""; fgets(buff, 1024, read); puts(buff); printf("\n"); fclose(read); system("pause"); return 0; }
实例:copy
/* 命令行结合文件操作写的案例 简单实现dos(就是cmd里面的)中的copy指令 命令行参数 :主函数的参数 文件操作 :以字符的方式读写 */ #include <stdio.h> #include <stdlib.h> int main(int argv, char* argc[]) { if (argv != 3) { printf("use myCopy 1.txt 2.txt"); system("pause"); return 0; } else { //printf("argc[0] = %s\n", argc[0]); //printf("argc[1] = %s\n", argc[1]); //printf("argc[2] = %s\n", argc[2]); printf("覆盖 %s 吗? (Yes/No/All):", argc[2]); int userkey = getchar(); if (userkey == 'y' || userkey == 'Y') { FILE* file1 = fopen(argc[1], "r"); FILE* file2 = fopen(argc[2], "w"); int cNum = fgetc(file1); while (cNum != EOF) { fputc(cNum, file2); cNum = fgetc(file2); } fclose(file1); fclose(file2); printf("已复制 1 个文件。\n\n"); } else { printf("已复制 0 个文件。\n\n"); } } system("pause"); return 0; }
格式化读写
#include <stdio.h> #include <stdlib.h> /* stdout: 屏幕 标准输出 stdin: 键盘 标准输入 文件指针: 自定义文件 printf("%st\%d\t%d\n", "MM", 12, 13); fscanf(FILE *read); */ struct student { char name[20]; int age; char sex[6]; }; int main() { int studentNum = 0; printf("请输入学生数:"); scanf("%d", &studentNum); struct student* array = (struct student*)malloc(sizeof(struct student) * studentNum); FILE* write = fopen("student.txt", "w+"); // 可以用其他后缀名,但是可能不正常,例如.rar不能解压 for (int i = 0; i < studentNum; i++) { printf("请输入第 %d 位学生的信息:", i + 1); scanf("%s%d%s", array[i].name, &array[i].age, array[i].sex); fprintf(write, "%s\t%d\t%s\n", array[i].name, array[i].age, array[i].sex); } fclose(write); // woshiyitiaomirendefenggexian------------------------------------------------ FILE* read = fopen("student.txt", "r"); struct student temp; // 存储读出来的信息 while (fscanf(read, "%s\t%d\t%s\n", temp.name, &temp.age, temp.sex) != EOF) { printf("%s\t%d\t%s\n", temp.name, temp.age, temp.sex); } fclose(read); system("pause"); return 0; }
自动朗读
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char userInput[1024] = ""; printf("请输入要朗读的文字:"); gets_s(userInput, 1024); FILE* write = fopen("Speak.vbs", "w+"); fprintf(write, "CreateObject(\"SAPI.SpVoice\").Speak(\"%s\")", userInput); fclose(write); system("Speak.vbs"); // 运行文件 system("del Speak.vbs");// 删除文件 system("pause"); return 0; }
C++
流对象与文件操作
- 程序建立一个流对象
- 指定这个流对象与某个文件对象建立连接
- 程序操作流对象
- 流对象通过文件系统对所连接的文件对象产生作用
提取与插入
- 读操作在流数据抽象中被称为(从流中)提取
- 写操作被称为(向流中)插入
预先定义的输出流对象
- cout标准输出
- cerr标准错误输出,没有缓冲,发送给它的内容立即被输出
- clog类似于cerr,但是有缓冲,缓冲区满时被输出。
标准输出换向
ofstream fout("b.out");
streambuf* pOld = cout.rdbuf(fout.rdbuf());
//
cout .rdbuf(pOld);——-