0
我有格式的文件爲:如何用C++中的空格解析文件?
2
3 4
7 8 9
10 20 22 02
...
在每一行基本上號,用空格分開。 我必須從文件中讀取數據,並提取所有數字並保留其行號,因爲我必須稍後再製作一棵樹。我正在這樣做,以獲取輸入,但得到奇怪的輸出。
#include<cstdio>
#include<iostream>
#include<cctype>
using namespace std;
void input()
{
char c,p;
while(c=getchar()!=EOF)
{
if(c=='\n') printf("},\n{");
else if(c==' ') printf(",");
else if(c=='0')
{
p=getchar();
if(p==' ')
{
printf("%c%c,",c,p);
}
else
{
printf("%c,",p);
}
}
else if(isalpha(c))
{
printf("%c",c);
}
}
}
int main()
{
input();
}
該圖像示出了輸入和輸出
而你得到的奇怪的輸出是...? –
你能展示什麼是預期產出?你實際得到的產出是多少? – A4L
我覺得很奇怪的是你使用'getchar()'從文件讀取!或者你使用'printf'寫入文件? –