所以我创建了一个简单的测试应用程序。它的目的是读取或写入文本文件。它正在查找字符串“固件:”。这个想法是,一个设备将有一个包传输到它并存储,然后激活。我想存储该软件包的名称(例如:Update1),并在需要时查询并获取软件包名称(从文件读取)。C++:搜索和写入文件以替换字符串
问题1:它似乎没有写入文件。 问题2:当我得到它写入文件,如果行“固件:pkgName”存在的情况下,如何将pkgName替换为新的软件包名称?
该程序运行和编译,所以随意把它扔在一个IDE或文本编辑器,它只是不完全做我想要的。我使用log.txt作为我的示例文件来读取。我相信有一个更简单的方法来做到这一点。我已经有一段时间了,我已经变得难倒了。
感谢您的帮助!
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int writeFile(string filename, string pkgName);
int readFile(string filename);
int main(void) {
string filename = "log.txt", userInput, pkgName;
system("touch log.txt");
while (true) {
cout << "Write or Read?\n>>>";
cin >> userInput;
if (userInput == "Write") {
cout << "What's your package name?\n>>>";
cin >> pkgName;
writeFile(filename, pkgName);
} else if (userInput == "Read") {
readFile(filename);
} else {
cout << "Invalid Input";
}
}
}
int writeFile(string filename, string pkgName) {
bool found, exists = true;
string line, word;
fstream firmwareFile;
int pos, size, num = 1;
firmwareFile.open(filename.c_str(), ios::in | ios::out | ios::app);
//firmwareFile << "This is a test.";
if (firmwareFile.is_open()) {
while (!found) {
getline(firmwareFile, line);
pos = line.find("Firmware: ");
if (pos != string::npos) {
//Found It
found = true;
exists = true;
size = line.size();
word = line.substr(10, ((size - pos) + 10));
cout << word << " is the old package name" << endl;
//TODO - Replace that with the new package name
firmwareFile.close();
} else if (firmwareFile.eof()) {
//Doesn't Exist
found = true;
exists = false;
cout << "No last firmware package available.\n";
} else {
//Still Looking
found = false;
cout << "Searching line #" << num << endl;
}
num++;
if (!exists) {
firmwareFile << endl << "Firmware: " << pkgName << endl;
cout << "Wrote package name to file\n" << endl;
}
firmwareFile.close();
}
} else {
cout << "Unable to open file";
}
}
int readFile(string filename) {
bool found;
string line, word;
fstream firmwareFile;
int pos, size, num = 1;
firmwareFile.open(filename.c_str(), ios::in | ios::out | ios::app);
if (firmwareFile.is_open()) {
while (!found) {
getline(firmwareFile, line);
pos = line.find("Firmware: ");
if (pos != string::npos) {
found = true;
size = line.size();
word = line.substr(10, ((size - pos) + 10));
cout << word << endl;
} else if (firmwareFile.eof()) {
found = true;
cout << "No last firmware package available.\n";
firmwareFile << endl << "Firmware: ";
} else {
cout << "Searching line #" << num << endl;
}
num++;
}
firmwareFile.close();
} else {
cout << "Unable to open file";
firmwareFile.close();
}
firmwareFile.close();
}
这里是我的log.txt文件的内容(一切,但包含行 “固件:” 是微不足道的)
You
Think
It's
Here
But
It's
Not
So
That
Sucks
Doesn't
It
?
Firmware: Update1
I
Hope
You
Caught
It
!
如果您想在随机位置写入,则必须以二进制模式打开文件。另一种选择是读取字符串向量中的文件,替换所需字符串,写入新文件,删除旧文件并最终将新文件重命名为旧名称。现在你用'ios :: app'打开流,所以所有的写操作都会追加到文件的末尾。 – jrok 2012-08-02 19:54:07
我面临的唯一问题是我无法删除旧文件,因为这将在配置文件中,我将需要复制整个文件。我不能“简单地”(没有简单的,我知道)删除行并将其追加到最后,或者只替换行上的packageName? – Cr4cKeR 2012-08-02 19:57:38
不,你不能从文件中间擦除数据,只能替换它。你不会删除配置文件,你基本上会复制一些更改的数据。 – jrok 2012-08-02 19:58:26