#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char** argv) {
FILE* fp = NULL;
fp = fopen("data.txt", "w");
fputs("gooooooooogle", fp);
fclose(fp);
fp = NULL;
fp = fopen("data.txt", "r+");
// 讀取修改前的內(nèi)容
char str[50];
cout << "------------修改前的內(nèi)容---------" << endl;
cout << fgets(str, 50, fp) << endl;
// 隨機(jī)修改文件.
rewind(fp);
fseek(fp, 3L, SEEK_SET);
//cout << ftell(fp) << endl;
fputc('A', fp);
fputc('B', fp);
fseek(fp, 1L, SEEK_CUR);
fputc('C', fp);
// 讀出修改后的內(nèi)容
rewind(fp);
cout << "------------修改后的內(nèi)容---------" << endl;
cout << fgets(str, 50, fp) << endl;
fclose(fp);
return 0;
}
/**
* 不能用fopen(fileName, "w+"); // 如果文件不存在,創(chuàng)建文件,如果存在,則清空,然后讀寫
* fopen(fileName, "a+"); // 只能寫到文件尾,但可以讀取.
* fopen(fileName, "r+"); // 可以讀寫, 如果文件不存在, 則發(fā)生錯(cuò)誤,可以實(shí)現(xiàn)隨機(jī)讀寫.
*/