从文件逐行读取

时间:2019-05-31 20:43:56

标签: c++ file fstream ifstream

我必须从文件中读取不同的命令。在输入“命令”之后,应该有一系列不同的命令,其格式为“ string:int”或“ string:int int”或“ string:”。 但是,如果我的命令不止一个,程序将无法正常运行。 我必须在执行某些命令后打印,这就是为什么我要同时阅读和写作的原因,是否有更好的方法来做到这一点?

fstream example_file;
example_file.open("file._");
string line;
int id1,id2;

if(example_file.is_open())
{
    while (getline(example_file,line))
    {
        if(line=="Command")
        {
            while(getline(example_file,line,':'))
            {
            if(line=="Print")
            {
                example_file>>id1;
                example_file<<endl;
                arr.getAutomat(id1).print(example_file);
            }
             if(line=="List")
            {
                cout<<"nn"<<endl;
                example_file<<endl;
                Listfunction(arr,example_file);
            }
            if(line=="Union")
            {
                example_file>>id1>>id2;
                example_file<<endl;
                Union(arr,id1,id2,example_file);
            }
            if(line=="Un")
            {
                example_file>>id1>>id2;
                example_file<<endl;
                Un(arr,id1,example_file);
            }
            if(line=="Concat")
            {
                example_file>>id1>>id2;
                example_file<<endl;
                Concat(arr,id1,id2,example_file);
            }

0 个答案:

没有答案