我正在尝试解析具有以下信息的文件
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();
}
答案 0 :(得分: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
...
}
}
}
我已经将该部分评论为“存储字符串”,因为我不确定您要在此处做什么。可能你要求在存储之前将字符串转换为整数。如果是,那么添加该代码,或询问您是否不知道如何做到这一点。实际上不要问,搜索堆栈溢出,因为这个问题已被问过数百次。