2013-06-12 61 views
0

我需要知道您是否可以輕鬆地獲取另一個文件中的數據條目數並將該數字保存在原始文件中。無論其中有多少條目,都需要一個處理其他文件的程序。希望這是有道理的。獲取另一個文件中的數據條目數量?

+0

我的回答是否解決您的問題?如果不讓我知道。如果是這樣,請將其標記爲答案(點擊答案旁邊的「正確標記」)。 – Dennis

回答

1

你的問題措辭很差,但我認爲你正在尋找getline。此功能可以基於換行符(默認行爲)或基於用戶提供的分隔符解析輸入文件:

int entryCount = 0; 
std::string currentLine; 
std::ifstream inFile("in.txt"); 
std::ofstream outFile; 
if (inFile) // short for inFile.good()) 
{ 
    while (std::getline(inFile, currentLine)) 
    { 
     ++entryCount; 
     // Do your processing 
    } 
    inFile.close(); 
    outFile.open("out.txt"); 
    outFile << "End of file. " << entryCount << " entries read." << std::endl; 
    outFile.close(); 
} 
else 
    std::cout << "oops... error opening inFile" << std::endl; 
相關問題