今天我们来学习C语言最后的基本知识,关于输入输出的问题。
一。错误报告函数:perror。
它以一种简单、统一的方式报告错误。例如:
#include<stdio.h>
#include<Windows.h>
int main()
{
FILE * pFile;
pFile=fopen("unexit.ent","rb");
if(pFile==NULL)
{
perror("The following error occurred");
}
else
{
fclose(pFile);
}
system("pause");
return 0;
}
这就是perror的用法。注:如果perror函数的参数str指针不是NULL,并指向一个非空的字符串,perror函数先打印这个字符串,然后跟着输出一个分号和一个空格,接着输出错误提示信息。
二。终止执行:exit.
这个函数定义在stdlib.h中。例如:void exit(int status);
参数status返回给了操作系统,用于提示程序是否完成。C语言定义了预定义符号:EXIT_SUCCESS和EXIT_FAILURE来表示成功或者失败返回。举例:
#include<stdio.h>
#include<Windows.h>
int main()
{
FILE * pFile;
pFile=fopen("myfile.txt","r");
if(pFile==NULL)
{
printf("Error opening file");
exit(EXIT_FAILURE);
}
else
{
}
system("pause");
return 0;
}
这个程序会直接退出。
三。流。
首先,程序运行之后默认打开三个流:(1)标准输入(2)标准输出(3)标准错误。
标准输入为键盘设备,标准输出为终端或屏幕。
在stdio.h中定义了很多I/O常量。(1)EOF-文件结束标志,表示文件到了结尾。(2)FOPEN-MAX-一个程序最多打开文件数。(3)FILENAME_MAX-文件名的最大长度。
四。IO函数总览。有三种方式:单个字符,文本行,二进制数据。
(1)字符输入函数:getchar,标准输入流。
(2)字符输出函数:putchar,标准输出流。
(3)字符输入函数:fgetc,getc,所有输入流。
(4)字符输出函数:fputc,putc,所有输出流。
(5)文本行输入函数:fgets,gets,所有输入流。
(6)问本行输出函数:fputs,puts,所有输出流。
(7)格式化输入函数:scanf,标准输入流。
(8)格式化输出函数:printf,标准输出流。
(9)格式化输入函数:fscanf,所有输入流。
(10)格式化输出函数: fprintf,所有输出流。
(11)二进制输入:fread,文件
(12)二进制输出:fwrite,文件。
五。字符I/O:getchar putchar
#include<stdio.h>
#include<Windows.h>
int main()
{
int c;
puts("Enter text. Include a dot ('.') in a sentence to exit:");
do{
c=getchar();
putchar(c);
}while(c!='.');
system("pause");
return 0;
}
结果是打什么显示什么。
getc和putc函数:
#include<stdio.h>
#include<Windows.h>
int main()
{
FILE * pFile;
int c;
int n=0;
pFile=fopen("myfile.txt","r");
if(pFile==NULL)
{
perror("Error opening file");
}
else{
do{
c=getc(pFile);
if(c=='$')
{
n++;
}
}while(c!=EOF);
fclose(pFile);
printf("File contains %d$.\n",n);
}
system("pause");
return 0;
}
fgetc和fputc:
getc和fgetc功能一样,putc和fputc功能也一样。
六。未格式化的行I/O。
gets和puts函数:
#include<stdio.h>
#include<Windows.h>
int main()
{
char string[256];
printf("Insert your full address:");
gets(string);
printf("Your address is: %s\n",string);
system("pause");
return 0;
}
#include<stdio.h>
#include<Windows.h>
int main()
{
char string[]="Hello world";
puts(string);
system("pause");
return 0;
}
fgets和fputs函数:
#include<stdio.h>
#include<Windows.h>
int main ()
{
FILE * pFile;
char mystring [100];
pFile=fopen("myfile.txt","r");
if(pFile==NULL)
{
perror("Error opening file");
}
else{
if(fgets(mystring,100,pFile)!=NULL)
{
puts(mystring);
}
fclose(pFile);
}
system("pause");
return 0;
}
七。格式化的行I/O
scanf和printf:这就不说了,想必大家都很了解了。
唯一要注意的的就是各种格式,比如:scanf中:%79s表示只读取79个。printf中:%10d表示预留10个位置。%010d表示预留的位置都赋值为0. %#x表示要带上进制标识符。%-10d表示向左对齐,%-010d表示左对齐,不能添0. %4.2f表示预留4个位置,保留小数后2位。%*d表示预留5个位置。
fscanf和fprintf函数:
#include<stdio.h>
#include<Windows.h>
int main()
{
char str[80];
float a;
FILE * pFile;
pFile=fopen("myfile.txt","w+");
fprintf(pFile,"%f %s",3.1416,"PI");
rewind(pFile);
fscanf(pFile,"%f",&a);
fscanf(pFile,"%s",&str);
fclose(pFile);
printf("I have read: %f and %s \n",a,str);
system("pause");
return 0;
}
八。二进制I/O
fread和fwrite函数:
#include<stdio.h>
#include<Windows.h>
int main()
{
FILE * pFile;
long LSize;
char * buffer;
size_t result;
pFile=fopen("myfile.bin","rb");
if(pFile==NULL)
{
fputs("File error",stderr);
exit(1);
}
fseek(pFile,0,SEEK_END);
LSize=ftell (pFile);
rewind(pFile);
buffer=(char*)malloc(sizeof(char)*LSize);
if(buffer==NULL)
{
fputs("Memory error",stderr);
exit(2);
}
result=fread(buffer,1,LSize,pFile);
if(result!=LSize)
{
fputs("Reading error",stderr);
exit(3);
}fclose(pFile);
free(buffer);
system("pause");
return 0;
}
这个程序告诉我们如何来求文件的大小,因为二进制有可能是-1,所以不能用EOF来判断,所以得用其他函数来求大小。正如这个程序,首先fseek函数将文件从头到尾赋值0,再用ftell函数移到当前写入位置也就是文件的最后一位,这样就获取了文件的大小。
以上就是我们I/O函数的内容了,希望大家多多练习,熟练掌握这些知识。