Any cool function to replace readln from pascal in

2019-02-28 08:58发布

问题:

The readln reads until the end of line (enter pressed) with spaces and everything,

I would like something like that but for ansi c (not c++ and need to be for linux and windows)

I know that I can make a function that reads every char until the enter pressed but If there is anything cooler it would be great =D

Thanks!

回答1:

You can use scanf with a scanset conversion something like this:

char buffer[256];
scanf("%255[^\n]", buffer);

another possibility is to use fgets:

char buffer[256];
fgets(buffer, sizeof(buffer), stdin);

On Linux (and other POSIX-ish systems) you should probably also have a function named (surprise, surprise) readline that's similar, but will allocate the space necessary for the incoming data.



回答2:

From here there is fgets that does this.



回答3:

Yes.

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


回答4:

Use fscanf function.



回答5:

Are you looking for something like this.