我想將文本文件中的內容複製到string
或*char
。如果我可以將文件內容複製到字符串的array
(每行array
的一個元素),會更好。這是我的代碼:將文件內容複製到字符串
int main() {
ifstream inFile;
inFile.open("index.in.txt"); //index.in has in each line a name and at the end there is a "."
char ab[11];
int q=0;
char *a[111];
if (inFile.is_open()) {
while (!inFile.eof()) {
inFile >> ab; //i think i don't understand this line correctly
a[q]=ab;
cout<<a[q]<<endl;
q++;
}
}
else{
cout<<"couldnt read file";
}
inFile.close();
cout<<"\n"<<ab<<endl; //it shoud be "." and it is
cout<<"\n"<<a[0]<<endl; //it should be "ion" but it gives me "."
return 0;
}
在一個array
所有值都等於最後一行是點
檢查[此](http://stackoverflow.com/questions/13035674/how-to-line-line-or-a-whole-text-file-at-once) –
您應該使用'std :: vector'而不是一組字符指針。另外,你是否希望每行都有自己的字符串或每個單詞? –
crashmstr
每行只有一個單詞,所以它對我來說沒有什麼區別 – Fanckush