0

我正在尝试解析具有以下信息的文件

test_wall ; Comments!!
je,5
forward
goto,1
test_random;
je,9

我应该忽略“;”之后的评论 并继续下一行。当有逗号时,我试图忽略逗号并存储第二个值。

string c;
int a;

c=getChar();

ifstream myfile (filename);
if (myfile.is_open())
{
   while ( c != ';' && c != EOF)
   {
       c = getchar();
       if( c == ',')
       {
         a= getChar();
       }

   }
}
myfile.close();
}
4

1 回答 1

1

这是一些代码。我不完全确定我是否正确理解了这个问题,但如果不是,希望这会让你朝着正确的方向前进。

ifstream myfile (filename);
if (myfile.is_open())
{
   // read one line at a time until EOF
   string line;
   while (getline(myFile, line))
   {
       // does the line have a semi-colon?
       size_t pos = line.find(';');
       if (pos != string::npos)
       {
           // remove the semi-colon and everything afterwards
           line = line.substr(0, pos);
       }
       // does the line have a comma?
       pos = line.find(',');
       if (pos != string::npos)
       {
           // get everything after the comma
           line = line.substr(pos + 1);
           // store the string
           ...
       }
   }
}

我将注释为“存储字符串”的部分留空,因为我不确定您想在这里做什么。可能您要求在存储之前将字符串转换为整数。如果是这样,请添加该代码,或者询问您是否不知道该怎么做。其实别问了,搜索栈溢出,因为那个问题已经被问了几百遍了。

于 2013-04-18T06:18:54.670 回答