7
void docDB(){
     int sdb = 0;
     ifstream dacb("kitudacbiet.txt");
     if(!dacb.is_open())
         cout<<"Deo doc dc file"<<endl;
     else{
          while(!dacb.eof()){
               dacb>>dbiet[sdb].kitu;
               dacb>>dbiet[sdb].mota;
               //getline(dacb,dbiet[sdb].mota);
               /*
               string a="";
               while((dacb>>a)!= '\n'){
                //strcat(dbiet[sdb].mota,a);
                dbiet[sdb].mota+=a;
               }
               */
               sdb++;
          }
     }

}

テキストファイル:「kitudacbiet.txt」

\ Dau xuyet phai
@ Dau @
# Dau #
$ Ky hieu $
( Dau mo ngoac
) Dau dong ngoac

画面

行の最初の文字列をdbiet[sdb].kituに読み込み、残りの行をdbiet[sdb].motaに読み込みます。

例:1行目= \ Dau xuyet phai

dbiet [sdb] .kitu="\"およびdbiet[sdb].mota = "Dau xuyet phai"

ダウンライン文字('\ n')に出会うまで、1行ずつ読みたいと思います。これを行う方法。申し訳ありませんが私の英語は良くありません。ありがとう

4

3 に答える 3

28

ファイルから文字列に1行全体を読み取るには、次のstd::getlineように使用します。

 std::ifstream file("my_file");
 std::string temp;
 std::getline(file, temp);

次のように、ファイルの最後までループでこれを行うことができます。

 std::ifstream file("my_file");
 std::string temp;
 while(std::getline(file, temp)) {
      //Do with temp
 }

参考文献

http://en.cppreference.com/w/cpp/string/basic_string/getline

http://en.cppreference.com/w/cpp/string/basic_string

于 2012-11-25T14:31:11.477 に答える
5

各行を解析しようとしているようです。getlineループで各行を区切る方法を別の回答で示しました。必要なもう1つのツールはistringstream、各トークンを分離することです。

std::string line;
while(std::getline(file, line))
{
    std::istringstream iss(line);
    std::string token;
    while (iss >> token)
    {
        // do something with token
    }
}
于 2012-11-25T14:36:49.730 に答える
1

getline(fin, buffer, '\n')
ここで、finファイル(ifstreamオブジェクト)が開かbufferstring/char、行をコピーするタイプです。

于 2012-11-25T14:31:39.140 に答える