【发布时间】:2022-12-01 18:52:37
【问题描述】:
I need to read a PPM file but I'm limited to only using getchar() but I'm running into trouble ignoring whitespaces.
I'm using num=num*10+(ch-48); to read the height and width but don't know how to read them all at once while ignoring spaces and '\n' or cmets.
I use this to read the magic number:
int magic;
while(magic==0){
if (getchar()=='P') //MAGIC NUMBER
magic=getchar()-48;
}
printf("%d\\n",magic);
i used this function to read the height and width which works only when the data in the header is seperated only by '\n'
int getinteger(int base)
{ char ch;
int val = 0;
while ((ch = getchar()) != '\\n' && (ch = getchar()) != '\\t' && (ch = getchar()) != ' ')
if (ch \>= '0' && ch \<= '0'+base-1)
val = base\*val + (ch-'0');
else
return ERROR;
return val;
}
this is the part in main()
height=getinteger(10);
while(height==-1){
height=getinteger(10);
}
【问题讨论】: