첫 단어 발생 위치를 찾는 데 어려움을 겪고 있습니다. 소프트웨어는 지정된 단어를 결정된 txt 파일로 검색해야합니다. 그래서 저는 fstream 라이브러리를 사용하여 열었습니다. 그리고 결국 무언가를 씁니다. 그러나 나는 그 단어의 정확한 위치를 얻기 위해 어떻게 소프트웨어를 만들 것인지 전혀 모른다. 내가 가진 것은 이것이다 :C++ txt 파일의 한 줄을 읽고 수정하십시오.
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
int main() {
fstream myfile; // fstream to read and write the myfile
string cText = "\n--something--!\n"; // what it will write
string line; // will assign the getline() value;
unsigned int pos = 0; //will be assigned to "seekp" - where it will start to write
myfile.open("example.html", ios::binary | ios::in | ios::out);
if(!myfile) {
cout << "File does not exist!" << endl;
cin.get();
return false;
}
do {
size_t found = line.find("<head>");
cout << string::npos << endl;
if (found != string::npos) {
cout << line << endl;
}
}while(getline(myfile, line));
myfile.seekp(pos+1, ios::beg);
//myfile.write(cText, strlen(cText)); //write cText
myfile.close();
cin.get();
return 0;
}
어떤 제안이라도?
......... tellg? – derpface