开发者

fgets - maximum size (int num)

In my program, i'm calling the function fgets:

char * fgets ( char * str, int num, FILE * stream );  

in a loop several times, and then deal with the new 开发者_运维技巧coming input (in case there is one).

in the fgets specifications, it says that:

num: Maximum number of characters to be read (including the final null-character). Usually, the length of the array passed as str is used.

The problem is that i want to rean NO MORE than the specified num - and IGNORE the rest of it, if it's in the same line.

What i've found out, is that the fgets reads the next part of the line in the next call to the function.

How can i avoid this behavior?


You'll need to do it manually - consume the characters with fgets and copy the results to a result array until a newline character is found, dump the contents, and continue with fgets.


The size parameter is intended to be used to prevent reading more data than your buffer can hold. It won't work for skipping over data.

You'll have to write code to throw away the parts of the string you don't want after it's read.

fgets() is a old C function. The idea is that the language will provide minimal complexity functions that you can combine to do what you like. They don't include any extra capability on purpose. This keeps everyone from paying for things they don't use. Think LEGO.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜