Any cool function to replace readln from pascal in ansi c?

匆匆过客 提交于 2019-12-04 05:22:46

问题


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.



来源:https://stackoverflow.com/questions/5772531/any-cool-function-to-replace-readln-from-pascal-in-ansi-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!