0
一個新行我想讀取文本所以我做這個如何知道它在文本
int main(){
fstream fs("test.txt",fstream::in|fstream::ate);
int length = fs.tellg();
std::vector<char> buffer(length);
fs.seekg(0, ios::beg);
fs.read(buffer.data(), length);
int newlen= 0;
int ptrSeek = 0;
while(buffer.data()[ptrSeek] != 0){
ptrSeek++;
newlen++;
if(ptrSeek == buffer.size()) { break;}
}
std::vector<char> temp(newlen,0);
memcpy(&temp[0],&buffer[ptrSeek-newlen],newlen);
}
test.txt的所有行:
this is a test
this is a test
所以當它讀取它,它讀取像這樣
[t] [h] [i] [s] [ ] [i] [s] [ ] [a] [ ] [t] [e] [s] [t] [ ] [t] [h] [i] [s] [ ] [i] [s] [ ] [a] [ ] [t] [e] [s] [t]
我怎麼能知道它開始從下一行讀?
檢查'buffer.data()[ptrSeek] =='\ n'' –
不是 '\ n' Windows專用? – M2X
@ M2X:不,它在Unix上是通用的。 Windows使用'\ r \ n'。 – Mat