从文件读取直到特殊字符

问题描述 投票:0回答:1

我有一个包含如下内容的文件:

10003;Tony;Stark;6:3:1990;Avengers Tower;New York City;12222;Iron Man

我想这样阅读

10003

托尼

鲜明

6:3:1990 ......

我已经尝试过了,但是似乎无法走得更远

 std::ifstream file;
 file.open ("OUT.txt")
 while (in)
    std::cout << char(in.get());
c++ file-io iostream ifstream
1个回答
0
投票
You can read each line and you can assign letters to a string until detecting ';':    

#include <iostream>
#include <fstream>

using namespace std;

int main()
{

    ifstream file;
    file.open("/directory of ur txt file/example.txt",ios_base::app);
    string lines;        
    while(!file.eof())
    {
        getline(file,lines);
        string desired_word = "";

        for(int i=0;i<lines.length();i++)
        {
            if(lines[i] != ';')
              desired_word += lines[i];
            if(lines[i]==';')
            {
               cout<<desired_word<<endl;
               desired_word = "";
            }
        }

    }



    return 0;

}
© www.soinside.com 2019 - 2024. All rights reserved.