C语言中stdin流的用法是什么?
C语言中stdin流的用法:
stdin是C语言中标准输入流,一般用于获取键盘输入到缓冲区里的东西。
访问stdin,可用stdio.h中提供的以下几个函数:
(1)scanf、getchar来从stdin流中获取指定的数据。scanf根据第一个参数指定的格式符将数据读入后续参数指定的内存位置中;getchar从stdin流中读取一个字符并返回。
(2)可以使用一般的访问输入流的函数来访问stdin,比如:fscanf、fgetc,使用方法和scanf、getchar类似,只是多了一个参数指定文件流。当然,scanf和getchar的具体实现就是通过调用fscanf和fgetc,这样可以提高代码利用率。
(3)可以用fflush和rewind这两个函数来刷新stdin,来清空缓冲区中已有的内容,不过这种方法不是标准方法,当stdin被重定向到文件时,这会导致指针重新指向文件开头。
C语言从stdin读取一行字符串的方法如下:
gets
gets函数的头文件是stdio.h,原型如下:
char *gets(char *s);
gets从stdin中读入一行内容到s指定的buffer中,当遇到换行符或EOF时读取结束。读取成功时,返回s地址;失败时返回null。需要注意的是,gets会将行末尾的’\n’字符或EOF替换成’\0’,这样,gets读取的内容中不包括’\n’字符。如果要获取读取字符串的长度,可以调用strlen函数获得。
fgets
fgets函数的头文件是stdio.h,原型如下:
char *fgets(char *s, int size, FILE*stream);
fgets从stream中读取最多size-1大小的内容到s指定的buffer中,当遇到换行符或EOF时读取结束。读取成功时,返回s地址;失败时返回null。需要注意的是,fgets会在所读取的内容后面添加’\0’,这样,fgets读取的内容中会包括行末尾的’\n’字符。如果要获取读取字符串的长度,可以调用strlen函数获得。
getline
getline函数的头文件是stdio.h,原型如下:
ssize_t getline(char **lineptr,size_t *n, FILE *stream);
getline从stream中读取一行内容到*lineptr所指定的buffer中,当遇到换行符或EOF时读取结束。*n是*lineptr所指定的buffer的大小,如果*n小于读入的内容长度,getline会自动扩展buffer长度,并更新*lineptr和*n的值。读取成功时,返回读入的字符个数;失败时返回-1。需要注意的是,gelines读取的内容中会包括行末尾的’\n’字符。
有关于c语言中fflush(stdin)的使用方法,我看了书本几个范例,始终不明白fflush(stdin)的用法
这个函数其实很简单,都是编程的软件搞的.
比如在DevC中,程序跑完以后编译软件的处理是直接将程序关闭,这时候我们是无法看到运行的结果的,需要手动设置让它停下来,比如用system(“pause”);或者是getchar(); 大多数书上选择的是后者,这样程序到这一步就会停下来等你按一个键.不过麻烦的是这个被读到缓冲区里的数还在,如果后面还有用到的话,就会有问题,这时候就需要用fflush()来清理一下.
说白了就是坏习惯搞出来的一个玩意,就像个回收站,里面有东西而又需要用它的时候,调用fflush()倒一次垃圾,就行了
全部手打…累死了…….
C语言中 stdin的 问题
If the given stream was open for writing and the last i/o operation was an output operation, any unwritten data in the output buffer is written to the file.
如果是输出,上一个操做是写入,那么将会把未写入的缓冲内容写入文件。
If it was open for reading and the last operation was an input operation, the behavior depends on the specific library implementation. In some implementations this causes the input buffer to be cleared, but this is not standard behavior.
如果是输入,这将取决于库的实现。在一些实现版本中,这将导致输入缓冲被清空,但这不是标准操作。
If the argument is a null pointer, all open files are flushed.
The stream remains open after this call.
如果函数的参数是空,将导致所有的文件被刷新,并且文件的开启属性不变。
When a file is closed, either because of a call to fclose or because the program terminates, all the buffers associated with it are automatically flushed.
当文件被关闭,或者因为程序终止而关闭,文件的缓冲区都被刷新。
所以,fflush(stdin)可能会清空stdin缓冲区。
在你输入时可不用stdin直接 scanf(“%d”,r);
C语言中的stdin和stdout是什么?是函数吗?
是函数库。
C语言中的 stdout 是一个定义在stdio.h的宏(macro),它展开到一个 FILE* (“指向 FILE 的指针”)类型的表达式(不一定是常量),这个表达式指向一个与标准输出流(standard output stream)相关连的 FILE 对象。
C语言fflush(stdin)函数是什么意思,在什么情况下用它
1、fflush(stdin)是清空输入缓冲区的意思。stdin就是标准输入 std即standard(标准),in即input(输入),合起来就是标准输入。 一般就是指键盘输入到缓冲区里的东西。
2、在清除文件缓冲区时使用,文件以写方式打开时将缓冲区内容写入文件。
例:
#include conio.h
#include io.h
#include stdio.h
#include string.h
void flush(FILE *stream);
int main(void)
{
FILE *stream;
char msg[] = “This is a test”;
/* create a file */
stream = fopen(“DUMMY.FIL”, “w”);
/* write some data to the file */
fwrite(msg, strlen(msg), 1, stream);
clrscr();
printf(“Press any key to flush DUMMY.FIL:”);
getch();
/* flush the data to DUMMY.FIL without closing it */
flush(stream);
printf(“\nFile was flushed, Press any key to quit:”);
getch();
return 0;
}
void flush(FILE *stream)
{
int duphandle;
/* flush the stream’s internal buffer */
fflush(stream);
/* make a duplicate file handle */
duphandle = dup(fileno(stream));
/* close the duplicate handle to flush the DOS buffer */
close(duphandle);
}
扩展资料
使用fflush的注意事项
1、MSDN 文档里也清楚地写着:fflush on input stream is an extension to the C standard (fflush 操作输入流是对C标准的扩充)。以下是 C99 对 fflush 函数的定义:int fflush(FILE *stream);
2、如果stream指向输出流或者更新流(update stream),并且这个更新流最近执行的操作不是输入,那么fflush函数将把任何未被写入的数据写入stream指向的文件(如标准输出文件stdout)。
3、fflush函数的行为是不确定的。fflush(NULL)清空所有输出流和上面提到的更新流。如果发生写错误,flush函数会给那些流打上错误标记,并且返回EOF,否则返回0。
4、如果 stream 指向输入流(如 stdin),那么 fflush 函数的行为是不确定的。故而使用 fflush(stdin) 是不正确的。
参考资料来源:百度百科—fflush(stdin)
C语言重启程序,解释下代码
system(“shutdown
-r”);就是重启命令
#includewindow.h
int
main
(){
system(“shutdown
-r”);
return
0;
}
1、system函数:
原型:int
system(const
char
*
command);
功能:执行
dos(windows系统)
或
shell(Linux/Unix系统)
命令,参数字符串command为命令名;
说明:在windows系统中,system函数直接在控制台调用一个command命令。在Linux/Unix系统中,system函数会调用fork函数产生子进程,由子进程来执行command命令,命令执行完后随即返回原调用的进程;
头文件:stdlib.h;
返回值:命令执行成功返回0,执行失败返回-1。
2、例程:
#includestdio.h
#includestdlib.h
int main(){
system(“del C:\\123.txt”);//在控制台中,执行命令del C:\\123.txt,删除C盘目录下的123.txt文件
return 0;
}