简体   繁体   中英

Reading spaces on C

I'm trying to read a serie of characters like the following one in C:

   &&&&&&&&\n& & &    &\n& & &&&& &\n

(notice that in the beginning there are spaces too).

I'm using a cicle "while" with:

scanf("%c",&cvar);

because I'm "storing" the characters into an array of structs that i'm using.

However, the program keeps ignoring the first spaces, and only starts storing from the first '&' appearence.

Any tips? Thank you in advance and sorry for my poor english.

Here is part of the code:

while(scanf("%c",&lab[i].simb)!=EOF){
    lab[i].x=x;
    lab[i].y=y;
    if(lab[i].simb=='\n'){
        x=0;
        y++;            
    } else x++;
    i++;
}

Read this...

I'll suggest to use getline if you are using gcc , but if however you want to read each character use getc instead of scanf ... and sorry for my poor English too...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM