您的位置:首页 > 编程语言 > C语言/C++

谈谈C语言中的输入和输出

2018-01-19 23:11 405 查看
要使用C语言中的输入和输出,第一步要引入标准的库函数<stdio.h>




1.getc&putc函数

int getc( File * stream)

int putc(int char, File * stream)

getc函数通过接收一个流对象,将流中的一个字符读入,而putc将传入的字符输送到对应的流中,流对象既可以是标准输入输出流也可以是用户自己进行的文件操作。

如果是标准输入输出流对象的操作,那么这两个方法等价与getchar(void)和putchar(void) which means:

int character=getc(stdin)其效果等价与int character=getchar();

putc(character,stdout)=putchar(character);

void testChar(){

int character=0;
printf("请输入一个字符:");
character=getc(stdin);

int character2=0;
printf("请输入另外一个字符:\n");
character2=getchar();

printf("我是字符1:");
putc(character,stdout);
printf("\n");

printf("我是字符2:");
putchar(character2);
printf("h");

}




当我们输入第一个字符x并且激动的按下回车的时候却发现,我们没有办法再输入第二个字符了!其实按下回车的这个动作也被stdin收录在缓冲区中了,也就是相当于x\n,所以最后我们打印出来的h是在新的一行出现的,而不是紧跟在字符2:之后。\n是不可见字符但是却占据一个字符的。

2.gets&puts函数

char *gets(char *str);

int puts(const char *str);

在C语言中没有定义字符串,在程序中要想表示字符串通常使用字符数组的形式 char str[size] ;而gets和puts函数是从stdin和stdout进行字符串的读入和写出的。这里要注意的是gets()中输入的长度不能够超过字符数组的size否则程序会报错



调用fgets函数的时候需要注意,fgets函数只能读取buffer容量的size-1个字符,fgets函数会默认将最后的一个字符填充为\0,并且将str内容进行返回。



3.fgetc&fputc&fgets()&fputs()

其实这几个函数的作用和上面函数的差不多,只不过传递的参数不同

int fgetc(FILE *stream);

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

int fputc(int char, FILE *stream);

int fputs(const char *str, FILE *stream);

int ungetc(int char, FILE *stream); //这个方法是将stream中的指针位置后退一步,也就是恢复一个字符。

void getcharMethod(){
int character=0;
int number=0;
char str[10];
char str2[10];

printf("you need to input an character:");
character=getchar();
printf("you need to input an interger too:");
scanf("%d",&number);

printf("please input another series of string:");
fgets(str2,10,stdin);
printf("you need to input a series of string:");
gets(str);//gets function does not work on linx OS so we need to use the fgets

printf("the character you input is %c\n",character);
//  %c %f %s %d represents different types
printf("another way to ouput this charcter:");
putchar(character);
printf("\n");
puts(str);//puts function does not work on Linux OS
fputs(str2,stdout);
}




stdin首先读入一个c\n9.getchar处理了第一个c,scanf又读取了后面的9此时的stdin中尚且还有一个换行字符,所以str2最后输出的也是空行。由于str2的容量大于1,所以str可以完整的读取abcdef并且输出。

我们来调整一下程序顺序

void getcharMethod(){
int character=0;
int number=0;
char str[10];
char str2[10];

printf("you need to input a series of string:");
gets(str);//gets function does not work on linx OS so we need to use the fgets

printf("please input another series of string:");
fgets(str2,10,stdin);

printf("you need to input an character:");
character=getchar();
printf("you need to input an interger too:");
scanf("%d",&number);

printf("the character you input is %c\n",character);
//  %c %f %s %d represents different types
printf("another way to ouput this charcter:");
putchar(character);
printf("\n");
puts(str);//puts function does not work on Linux OS
fputs(str2,stdout);
}


当str2=1234567890,str=123456789的时候我们又发现了无法输入的问题



str2句末的时候塞了一个回车给str,而str又将后面的回车塞给了charcter.(O(≧口≦)O)!

所以如果str的可见字符长度为8的话,我们就可以继续进行输入
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C语言 基础入门
相关文章推荐