2014-04-25 67 views
2

我想將文本文件中的內容複製到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所有值都等於最後一行是點

+0

檢查[此](http://stackoverflow.com/questions/13035674/how-to-line-line-or-a-whole-text-file-at-once) –

+0

您應該使用'std :: vector '而不是一組字符指針。另外,你是否希望每行都有自己的字符串或每個單詞? – crashmstr

+0

每行只有一個單詞,所以它對我來說沒有什麼區別 – Fanckush

回答

2

您正在覆寫僅緩衝每次在inFile >> ab; 你讀的緩衝區內的一行並保存某處的緩衝區地址。下一次你讀取同一緩衝區中的下一行並保存與第二行完全相同的地址。如果您讀回第一行,您將最終讀取更新的緩衝區,即最後一行。

您可以更改您的代碼

#include <vector> 
#include <string> 
#include <fstream> 

using namespace std; 
int main() { 
    ifstream inFile; 
    inFile.open("index.in.txt"); //index.in has in each line a name and at the end there is a "." 
    string ab; //char ab[11]; 
    int q=0; 
    vector<string> a(111); //char *a[111]; 
    if (inFile.is_open()) { 
     while (!inFile.eof()) { 
     inFile >> ab; 
     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; 
} 

更好地使用的std :: string和std :: vector的,而不是陣列。

+0

爲什麼它使用字符串和向量 – Fanckush

+0

std :: string和std :: vectors是處理內存管理的智能容器。因此,每次將一個字符串分配給向量[idx]時,都會爲向量[idx]創建一個新的緩衝區。有關[c-dynamic memory management](http://en.wikipedia.org/wiki/C_dynamic_memory_allocation)malloc等的更多詳細信息,請參閱C++動態內存(即new/delete),然後是基本STL(std :: string,std :: vector,std :: list,std :: set,std :: map等 –

+1

注意:['while(!inFile.eof())'是個壞主意*](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – WhozCraig

2
#include <iostream> 
#include <fstream> 
#include <vector> 
#include <string> 

bool ReadFile(const std::string &sFileName, 
       std::vector<std::string> &vLines) 
{ 
    std::ifstream ifs(sFileName); 
    if (!ifs) 
     return false; 
    std::string sLine; 
    while (std::getline(ifs, sLine)) 
     vLines.push_back(sLine); 
    return !ifs.bad(); 
} 

int main() 
{ 
    const std::string sFileName("Test.dat"); 
    std::vector<std::string> vData; 
    if (ReadFile(sFileName, vData)) 
     for (std::string &s : vData) 
      std::cout << s << std::endl; 
    return 0; 
} 
4
int main() { 
    ifstream inFile; 
    inFile.open("index.in.txt"); //index.in has in each line a name and at the end there is a "." 
    std::vector<std::string> lines; 
    std::string line; 
    if (inFile.is_open()) { 
     while (getline(inFile, line)) { 
     lines.push_back(line); 
     } 
    } 
... 

現在lines是字符串的一個向量,每個是從文件中的一行

+0

謝謝,這對我來說更加複雜,但是h我應該學習:) – Fanckush