开发者

Best way to read next non-blank character from file in C

开发者 https://www.devze.com 2023-01-26 19:31 出处:网络
What is the best (shortest) way to read the next non-blank (not space/newline/tab) character from a file in a C program?

What is the best (shortest) way to read the next non-blank (not space/newline/tab) character from a file in a C program?

I realize I could probably use gets followed by strtok, but it seems like there has to be somethi开发者_StackOverflowng more concise.

If so, let me know; if not, let me know that too.

Thanks.


char c;
fscanf(f, " %c", &c);

OR

int c;
while (isspace(c=fgetc(f)));


The next character can be read using fgetc. Since you cannot know in advance whether it is blank or not, you have to take the risk and read it. If it then turns out not to be non-blank, you can put it back using fputc.

0

精彩评论

暂无评论...
验证码 换一张
取 消