2013-04-05 76 views
1

我是noob C++的學生。我無法將文件讀取到數組結構中。這是一個班級任務,所以我不需要任何人爲我做代碼,我只是想知道我做錯了什麼。我讀的文本文件的格式爲這樣:使用getline讀取文件時遇到問題(noob)

Giant Armadillo#443#M 
Hawaiian Monk Seal#711#M 
Iberian Lynx#134#M 
Javan Rhinoceros#134#M etc... 

使用getline()工作正常讀取與'#'分隔字符串,但不與intchar工作。在檢查分隔符時,如何閱讀intchar? 謝謝,抱歉,如果這沒有寫清楚,或格式正確。我是全新的SO。

#include <iostream> 
#include<string> 
#include <fstream> 
#include <cstdlib> 
using namespace std; 

//Create a new structure 
struct Endangered 
{ 
    string name; 
    int population; 
    char species; 
}; 


int main() { 

Endangered animals[200]; 

//initialize animals 
for (int i=0; i<50; i++) 
{ 
    animals[i].name=" "; 
    animals[i].population=0; 
    animals[i].species=' '; 
} 

ifstream myFile; 
myFile.open("animals.txt"); 
int i=0; 
if (myFile.is_open()) 
{ 
    while (myFile.eof()) 
    { 
     //read the string until delimiter # 
     getline(myFile, animals[i].name, '#'); 

     //read the int until delimiter # 
     getline(myFile, animals[i].population, '#'); 

     //read the char until the delimiter 
     getline(myFile, animals[i].species, '/n'); 
     i++; 
    } 


    return 0; 
} 
+1

使用'「\ n'',而不是'」/n'' – 2013-04-05 07:05:47

回答

0

我敢肯定,你不能從文件中像這樣讀取數據:

//read the int until delimiter # 
getline(myFile, animals[i].population, '#'); 

,因爲你有刺痛,要爲其指定爲int或焦炭領域。

看看這個聲明:

istream& getline (istream& is, string& str, char delim); 

你必須有字符串作爲第二個參數。你不能傳遞給這個函數int或者char和animals [i] .population是typeof int。

您應該首先將數據加載到某個字符串或char *緩衝區,然後使用正確的函數將其轉換爲您想要的類型。例如尋找諸如atoi的功能。

總結。讀取數據到臨時緩衝區,然後轉換它。

如果不幫助你,我會改正的代碼,但你不想:)

0

程序的讀取部分可以是這樣的:

size_t i=0; 
ifstream myFile("animals.txt"); 
if(!myFile) 
{ 
    throw std::runtime_error("Failed opening file"); 
} 
string line; 
while(std::getline(myFile, line)) 
{ 
    istringstream line_stream(line); 
    string temp; 
    if(!std::getline(line_stream, temp, '#')) 
    throw std::runtime_error("expected #"); 
    animals[i].name = std::stoi(temp); 

    if(!std::getline(line_stream, temp, '#')) 
    throw std::runtime_error(expected #"); 

    animals[i].population = std::stoi(temp); 

    if(!std::getline(line_stream, temp) || temp.size() != 1) // or whatever input verification you need 
    throw std::runtime_error("expected species"); 
    animals[i].species = temp[0]; // or whatever input you need, this assumes species is a char 
} 

有沒有在這裏完成所有緩衝的方式,但這應該工作。

0

的問題是,getline函數返回下一個分隔的值作爲字符串: 功能DEF:函數getline(istream的&是,字符串& STR,炭DELIM);

更改您的代碼以將值讀入臨時字符串變量。

然後使用std :: stoi(str)將字符串值轉換爲一個整數並將返回的值賦給總體。

您要求不提供任何代碼,因此我會將實施留給您。

希望這會有所幫助。

-1

這裏有兩個問題:讀取非空格分隔的文本(包含空格的字符串)並將文本轉換爲數字。

快速和骯髒的方式可以是:

for(int i=0; myFile && i<sizeof(animals)/sizeof(Endangered); ++i) 
{ 
    char delim = 0; 

    //read the string until delimiter # 
    getline(myFile, animals[i].name, '#'); 

    //read the int 
    myfile >> animals[i].population; 
    //read and check delimiter 
    if(myfile>>delim && delim!='#') mifile.setstate(myfile.failbit); 

    //read the species 
    myfile >> animals[i].species; 

    //discard any other rubbish untile the end of line 
    myfile.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); 
    myfile >> delim; // this is '\n'; 
} 

需要注意的是:

  • 我用(不同時),使計數器(i)和增量(++i)是定義在一起(nmo風險被遺忘)。退出條件不是myfile.eof()(當它發生的時候已經太晚了),只需要myfile(如果「可讀」,它可以自我轉換爲「true」:如果文件變爲不可讀的,不管是什麼錯誤,即使尚未在結束)

  • 另一個退出條件(在& &與第一)是陣列已經完全充滿(表達只是它的大小,不管它是什麼)

    • 第一個字符串(可能包含空格)被讀取p到'#'(這是讀取,但沒有存儲,就像你需要)

    • 該數字被讀作輸入字段(如果沒有數字,>>操作符將設置失敗位,並且你將不再讀取並安全退出)

    • 將分隔符讀作輸入字段:數字之後和#之前的任何空格被丟棄。 #進入delim,如果我們發現它不是#,我們設置failbit(該文件不包含應該包含的內容),並阻止任何讀取並安全地退出循環(myfile將會評估爲false)。

    • 每個最終的空間都被丟棄,並且下一個字符進入species

    • 現在讓我們放棄任何事情,最終保留在行(包括'\ n')。

    • 我們現在可以繼續下一行。不需要你的包裹if

注:如果MYFILE沒開,它評估爲「假」循環將不被輸入。

這裏的eof()條件不能被視爲「終止失敗」,而是「達到成功」。

如果 - 在loop-file.eof()爲true後,您成功讀取所有文件,否則由於出錯提前停止。

還要注意的是整個身體for可以是命名

std::isream& operator>>(std::istream& myfile, Endangered& endangered) 
{ 
    ..... 
    ..... 
    return myfile; 
} 

函數體循環將只是

for(int i=0; myFile && i<sizeof(animals)/sizeof(Endangered); ++i) 
    myfile >> animals[i]; 
+0

這是非常有幫助的。謝謝!! – 2013-04-05 15:32:41

+0

什麼是'h':if(myfile >> h && h!='#')mifile.setstate(myfile。failbit); – 2013-04-05 15:39:44

+0

@dana_muise:oops ...一個錯字:它本來就是'delim'。剛剛編輯。 – 2013-04-05 18:38:42