간단한 테스트 응용 프로그램을 만들었습니다. 목적은 텍스트 파일을 읽거나 쓰는 것입니다. "Firmware :"문자열을 찾고 있습니다. 아이디어는 장치가 패키지를 전송하여 저장 한 다음 활성화하는 것입니다. 해당 패키지의 이름 (예 : Update1)을 저장하고 싶습니다. 쿼리를 통해 패키지 이름을 다시 가져옵니다 (파일에서 읽기).C++ : 문자열을 대체하는 파일 검색 및 쓰기
문제 1 : 파일에 쓰지 않는 것 같습니다. 문제점 2 : 파일에 쓸 때 "Firmware : 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
내가 직면 한 유일한 문제는 구성 파일에있는 이전 파일을 삭제할 수 없으며 전체 파일을 복사해야한다는 것입니다. 내가 "간단하게"(아무것도 간단하지 않다, 나는 알고있다) 라인을 지우고 끝에 추가하거나 라인의 packageName만을 바꿀 수 없습니까? – Cr4cKeR
아니요, 파일 가운데에서 데이터를 지울 수는 없으며 파일을 바꿀 수만 있습니다. 그리고 config 파일을 지우지는 않을 것입니다. 기본적으로 변경된 일부 데이터를 복사합니다. – jrok