linux c编程 文件IO

文章内容参考自

21天学通linux c编程

主要函数:

打开:  fopen fdopen  freopen

关闭: fclose

读取:    getc  fgets fread

写入:  putc  fputc  fputs fwrite


读取字符

#include <stdio.h>
#include <errno.h>

main()
{
   FILE * fp;
   extern int errno; 
   char file[]="/root/a1.txt";
   fp=fopen(file,"r");
   int i;
   char a;
   if(fp==NULL)
   {
   	  printf("cant't open file %s.\n",file);
   	  printf("errno:%d\n",errno);
   	  printf("ERR  :%s\n",strerror(errno));
   	  return;
   	}
   	else
   	{
   		printf("%s was opened.\n",file);   		
	}
	for(i=0;i<10;i++)
	{
		a=getc(fp);
		if(a==EOF)
		{
			break;
		}
		printf("%c",a);
   }
   printf("\n");
   fclose(fp);   
}

读取字符串

#include <stdio.h>
#include <errno.h>

main()
{
   FILE * fp;
   extern int errno; 
   char file[]="/root/a1.txt";
   fp=fopen(file,"r");
   int i;
   char a[5];
   if(fp==NULL)
   {
   	  printf("cant't open file %s.\n",file);
   	  printf("errno:%d\n",errno);
   	  printf("ERR  :%s\n",strerror(errno));
   	  return;
   	}
   	else
   	{
   		printf("%s was opened.\n",file);   		
	}
    fgets(a,5,fp);
   printf("%s\n",a);
   fclose(fp);   
}
读取数据

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
struct stu
{
	char name[10];
	int age;	
};
main()
{
   struct stu mystu[3];
   FILE * fp;
   extern int errno; 
   char file[]="/root/a1.txt";
   int i,j;
 
   fp=fopen(file,"a+");
   if(fp==NULL)
   {
   	  printf("cant't open file %s.\n",file);
   	  printf("errno:%d\n",errno);
   	  printf("ERR  :%s\n",strerror(errno));
   	  return;
   	}
   	else
   	{
   		printf("%s was opened.\n",file);   		
	}
    i=fread(mystu,sizeof(struct stu),3,fp);
    printf("%d students was read.\n",i);
    close(fp);
}



写入字符

#include <stdio.h>
#include <errno.h>

main()
{
   FILE * fp;
   extern int errno; 
   char file[]="/root/a1.txt";
   char txt[5]="hello";
   int i=0;
   fp=fopen(file,"a+");
   if(fp==NULL)
   {
   	  printf("cant't open file %s.\n",file);
   	  printf("errno:%d\n",errno);
   	  printf("ERR  :%s\n",strerror(errno));
   	  return;
   	}
   	else
   	{
   		printf("%s was opened.\n",file);   		
	}
	
   
   for(i=0;i<5;i++)
   {
   	  putc(txt[i],fp);
   	  printf("%c",txt[i]);

   	}   	
   	fclose(fp);    
}


写入字符串

#include <stdio.h>
#include <errno.h>

main()
{
   FILE * fp;
   extern int errno; 
   char file[]="/root/a1.txt";
   char *txt="hello";
   int i;
   fp=fopen(file,"a+");
   if(fp==NULL)
   {
   	  printf("cant't open file %s.\n",file);
   	  printf("errno:%d\n",errno);
   	  printf("ERR  :%s\n",strerror(errno));
   	  return;
   	}
   	else
   	{
   		printf("%s was opened.\n",file);   		
	}
 
   	i=fputs(txt,fp);
   	{
   		printf("%d char was written.\n",i);
	}
	close(fp);
}

写入数据

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
struct stu
{
	char name[10];
	int age;	
};
main()
{
   struct stu mystu[3];
   FILE * fp;
   extern int errno; 
   char file[]="/root/a1.txt";
   int i;
   strcpy(mystu[0].name,"Jim");
   mystu[0].age=14;
   strcpy(mystu[0].name,"Jam");
   mystu[1].age=14;  
   strcpy(mystu[0].name,"Lily");
   mystu[2].age=19; 
   fp=fopen(file,"a+");
   if(fp==NULL)
   {
   	  printf("cant't open file %s.\n",file);
   	  printf("errno:%d\n",errno);
   	  printf("ERR  :%s\n",strerror(errno));
   	  return;
   	}
   	else
   	{
   		printf("%s was opened.\n",file);   		
	}
 
    i=fwrite(mystu,sizeof(mystu),3,fp);
    printf("%d students was written.\n",i);
    close(fp);
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值