所以我創建了一個簡單的測試應用程序。它的目的是讀取或寫入文本文件。它正在查找字符串「固件:」。這個想法是,一個設備將有一個包傳輸到它並存儲,然後激活。我想存儲該軟件包的名稱(例如: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