通过ofstream 如何更改一个字符!求高人指点
在一个txt文件中 里有一行信息 0#abc,如何通过ofstream 将文件中的'0'修改成其他值 。。。只想改变'0'其他的值不改变 应该如何实现 !
[解决办法]
楼主是要遇到 '0'就改 还是只改 0#abc这类信息的'0'??
[解决办法]
- C/C++ code
#include <fstream>#include <iostream>using namespace std;int main(int argc, char **argv){ ofstream ofs("test.txt", ios::in | ios::out); if(!ofs.is_open()) { cout << "error file" << endl; return 1; } ofs.seekp(1, ios::beg); cout << ofs.tellp() << endl; ofs << '*'; ofs.close(); return 0;}
[解决办法]
先用 ifstream 把文件中的数据读出来,把数据中的0改成其它值后,再用ofstream保存到文件
[解决办法]
好像做不到。
[解决办法]
[解决办法]
- C/C++ code
#include <iostream>#include <string>#include <fstream>using namespace std;int main(int argc, char **argv){ const char *alp = "*"; const string filePath = "a.txt"; string word; fstream file(filePath.c_str()); if (!file) { cout<<"Cannot open file!"<<endl; return 0; } while (file>>word) { if (word == "0#abc") { int pos=file.tellp(); file.seekp(pos - word.size()); file.write(alp, 1); } } file.close(); system("pause"); return 0;}