当前位置:网站首页>Details of gets, fgetc, fgets, Getc, getchar, putc, fputc, putchar, puts, fputs functions
Details of gets, fgetc, fgets, Getc, getchar, putc, fputc, putchar, puts, fputs functions
2022-06-30 14:06:00 【Fat Xiao Deng】
1.gets( Read a string from the standard input device ):
The header file #include <stdio.h>
Defined function char * gets(char *s);
Function description gets() It is used to read characters from standard equipment and store them in parameters s The memory space referred to , Until a newline character appears or the end of the file is read ,
Then add NULL End as a string .
Return value gets() Return if successful s The pointer , return NULL It means there is an error .
Additional notes because gets() Unable to know string s Size , You must encounter a newline character or the end of the file to end the input , Therefore, it is easy to cause buffering
Security issues of overflow . It is recommended to use fgets() replace .
Example Reference resources fgets()
2.fgetc( Read a character from a file ):
The header file include<stdio.h>
Defined function nt fgetc(FILE * stream);
Function description fgetc() From parameter stream Read a character from the file referred to . If there is no data at the end of the file, it will return EOF.
Return value getc() The read characters will be returned , If returns EOF It means the end of the file .
Example
#include <stdio.h>
int main(void)
{
FILE *fp=fopen("exist", "r"); //exist Represents the created file
int c;
while((c = fgetc(fp)) != EOF)
printf("%c", c);
fclose(fp);
return 0;
}
for example :exist Change to 1.txt, hold 1.txt Write string in abc, The running result of the program is abc
3.fgets( Read a string from a file ):
The header file include<stdio.h>
Defined function har * fgets(char * s, int size, FILE * stream);
Function description fgets() Used to retrieve parameters from stream The characters read in the file referred to coexist in the parameter s The memory space referred to , Until a new line character appears 、
Read the end of the file or have read size-1 Up to characters , Finally, it will add NULL End as a string .
Return value gets() Return if successful s The pointer , return NULL It means there is an error .
Example
#include <stdio.h>
int main(void)
{
char s[80];
fputs(fgets(s, 80, stdin), stdout);
return 0;
}
For example, running a program
this is a apple // Input
this is a apple // Output ( adopt fgets To get the information entered from the keyboard )
4.getc( Read a character from a file ):
The header file #include <stdio.h>
Defined function int getc(FILE * stream);
Function description getc() Used to retrieve parameters from stream Read a character from the file referred to . If there is no data at the end of the file, it will return EOF. although
getc() And fgetc() The same effect , but getc() Define... For macros , Non real function calls .
Return value getc() The read characters will be returned , If returns EOF It means the end of the file .
Example Reference resources fgetc().
5.getchar ( Read a character from the standard input device ):
The header file #include <stdio.h>
Defined function int getchar(void);
Function description getchar() Used to read a character from a standard input device , Then the character is changed from unsigned char convert to int After the return .
Return value getchar() The read characters will be returned , If returns EOF It means there is an error .
Additional notes getchar() Non real functions , It is getc(stdin) Macro definition .
Example
#include <stdio.h>
int main(void)
{
FILE * fp;
int c, i;
for(i = 0; i < 5; i++)
{
c = getchar();
putchar(c);
}
return 0;
}
For example, running a program
abc // Input
abc // Output
6.putc( Write a specified character to the file ):
Defined function int putc(int c, FILE * stream);
Function description putc() Will set the parameters c To unsigned char Post write parameters stream In the specified file . although putc() And fputc()
The same effect , but putc() Define... For macros , Non real function calls .
Return value putc() It will return the characters successfully written , The parameter c. If returns EOF Then it means that the write failed .
Example Reference resources fputc().
7.fputc( Writes a specified character to the file stream ):
The header file #include <stdio.h>
Defined function int fputc(int c, FILE * stream);
Function description fputc Will set the parameters c To unsigned char Post write parameters stream In the specified file .
Return value fputc() It will return the characters successfully written , The parameter c. If returns EOF Then it means that the write failed .
Example
#include <stdio.h>
int main(void)
{
FILE * fp;
char a[26] = "abcdefghijklmnopqrstuvwxyz";
int i;
fp = fopen("noexist", "w");
for(i = 0; i < 26; i++)
fputc(a[i], fp);
fclose(fp);
return 0;
}
for example :noexist Change to 2.txt, You will find that the current folder generates a 2.txt file , And the content of the document is abcdefghijklmnopqrstuvwxyz
8.putchar( Writes the specified character to the standard output device ):
The header file #include <stdio.h>
Defined function int putchar (int c);
Function description putchar() Used to set the parameter c Characters are written to standard output devices .
Return value putchar() It will return the characters of successful output , The parameter c. If returns EOF It means that the output fails .
Additional notes putchar() Non real functions , It is putc(c, stdout) Macro definition .
Example Reference resources getchar().
9.puts( Used to output string to buffer ):
The header file #include <stdio.h>
Defined function int puts (char *string);
Function description putchar() Used to set the parameter c Characters are written to standard output devices .
Return value If this function outputs string to stream , Returns a nonzero value ; otherwise , return 0.
Additional notes putchar() Non real functions , It is putc(c, stdout) Macro definition .
Example
#include <stdio.h>
int main(void)
{
char string[]="This is a test\n";
puts(string);
return 0;
}
The result of running the program is This is a test
10.fputs( Writes a specified string to a file ):
The header file #include <stdio.h>
Defined function int fputs(const char *s, FILE *stream);
Function description fputs() Used to set the parameter s The indicated string is written to the parameter stream In the document referred to .
Return value If successful, the number of characters written out will be returned , return EOF It means there is an error .
Example Please refer to fgets().
边栏推荐
- 【刷题篇】爱吃香蕉的珂珂
- 我如何才能保护我的私钥?
- 【系统分析师之路】第五章 复盘软件工程(敏捷开发)
- [Title brushing] heater
- navicat数据库建表是没有utf8选项。
- Pytoch viewing model parameter quantity and calculation quantity
- Service online governance
- Configuration of headquarters dual computer hot standby and branch infrastructure for firewall Foundation
- exlipse同时操作多行。比如同时在多行同列输入相同的文字
- PG Basics - logical structure management (table inheritance, partition table)
猜你喜欢

Waving flags and shouting for basic language

Today's sleep quality record 80 points

On the simplification and acceleration of join operation

【刷题篇】避免洪水泛滥

Embedded development: five C features that may no longer be prohibited

Intelligent operation and maintenance: visual management system based on BIM Technology
![[scientific research data processing] [basic] category variable frequency analysis chart, numerical variable distribution chart and normality test (including lognormal)](/img/70/8bf226964118efb324ca4d339df654.png)
[scientific research data processing] [basic] category variable frequency analysis chart, numerical variable distribution chart and normality test (including lognormal)

香港回归20余年,图扑数字孪生港珠澳大桥,超震撼

What is erdma as illustrated by Coptic cartoon?

Tencent two sides: @bean and @component are used on the same class. What happens?
随机推荐
How to execute a query SQL
Basic syntax of unity script (1) - common operations of game objects
Jetpack Compose 实现完美屏幕适配
Service online governance
随着产业互联网的发展,有关互联网的落地和应用也就变得宽阔了起来
用Unity实现Flat Shading
More than 20 years after Hong Kong's return, Tupu digital twin Hong Kong Zhuhai Macao Bridge has shocked
Read all the knowledge points about enterprise im in one article
SQL编程问题,测试用例不通过
嵌入式开发:5个可能不再被禁止的C特征
Configuration of headquarters dual computer hot standby and branch infrastructure for firewall Foundation
Apache Doris Compaction優化百科全書
get请求与post提交区别的简易理解
编程实战赛来啦!B站周边、高级会员等好礼送你啦!
优思学院:六西格玛不只是统计!
IM即时通讯应用开发中无法解决的“顽疾”
我想问一下招商证券怎么开户?通过链接办理股票开户安全吗
How does MySQL merge columns?
Knowledge dissemination cannot replace professional learning!
Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source!