Linux文件操作2

3/27

ftruncate:将指定文件大小修改成length指定的大小(用来给文件扩容,如果指定的大小小于当前文件,那就删除后面的数据)
ftruncate(int fd,length)
Ftruncate(fd,0)

stdio.h:C的标准输入输出库:I/O(input output)

stdout:输出流
行缓冲:stdout在终端上进行输出的时候,输出的规则为每当出现换行符的时候,进行一次刷新缓存,然后再进行操作(printf输出的时候,是看到换行符才进行输出)

fopen():r :只读
w:只写:如果文件不存在,创建一个新的,如果文件存在,清空原先文件的文件内容;
a:追加:不存在,创建一个新的,如果存在在文件末尾进行追加。
r+:可读可写,文件不存在,打开失败。
w+:w+r+
a+:a + r+
fclose(fp)

fread
size_t fread(void *ptr, size_t size , size_t nmemb,FILE *stream );
ptr:字符串指针
size:读取每个字符大小
number:读多少个
Stream:文件描述符指针

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    FILE *fp;
    fp = fopen("./text.txt", "w+");
    if(NULL == fp)
    {
	perror("open");
	exit(1);
    }

    char *str = "helloworld";
    
   int number = fwrite(str, sizeof(char), strlen(str), fp);
   if(0 == number)
   {
       perror("fwrite");
       fclose(fp);
       exit(1);
   }
    fseek(fp,0,SEEK_SET);
      char buffer[1024]={0};
      int number2 = fread(buffer,sizeof(char),1024,fp);
      if(0 == number2)
      {
	  perror("fread");
	  fclose(fp);
	  exit(1);
      }
      printf("read:%s\n",buffer);

    fclose(fp);
    return 0;
}

4、Fseek
5、读字符的家族:getc:从文件里读取一个字符getc(fp)返回值:当读到文本末尾五字符的时候,返回EOF(NULL)
fgetc:函数调用
getchar():将字符从unsigned char转换成int进行返回,带走缓冲区

#include<stdio.h>
#include<stdlib.h>

int main()
{
    FILE *fp;
    int c;
    fp = fopen("./text.txt", "r");
    if(NULL == fp)
    {
	perror("fopen!");
	exit(1);
    }
    while((c = getc(fp)) != EOF)
    {
	printf("%c", c);
    }
    printf("\n");
    return 0;
}
#include<stdio.h>
#include<stdlib.h>

int main()
{
    FILE *fp;
    int c;
    int i=0;
    for(i=0;i<5;i++)
    {
	c=getchar();
	printf("%c ",c);
    }
    printf("\n");
    return 0;
}

6写字符的时候:fputc:将某个字符写入文件流
Int fputc(int c, FILE stream)
putc:宏
puchar©
1.printf
2.fprintf:int fprintf(FILE
stream,constchar*format,…)
fprintf(stdout,"%d\n%d\n%.7LF\n",i,j,k);

sprintf():网字符串里写字符串;
fscanf():从文件流里写读字符串
int fscanf(FILE *stream,const char *format,…);
stdin

Sscanf:从字符串里读取变量
int sscanf(const char *str, const char *format, …);

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main()
{
    FILE *fp;
    int c;

    fp = fopen ("./text.txt","w+");
    char *str="this is text";

    int i;
    for( i = 0; i < 15; i++)
    {
	fputc(*(str+i),fp);
    }
    fclose(fp);
    printf("\n");

    return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    FILE *fp;
    int c;
    int i=10;
    int j=20;

    long double k= 3.1415926;

    fp = fopen("./text.txt","w+");

    fprintf(fp,"%d\n%d\n%.7LF\n",i,j,k);
    printf("\n");
    return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    char str[1024]={0};//初始化
    int i=10;
    int j=20;
    long double k= 3.1415926;

    sprintf(str,"hello %d\n%d\n%.7LF\n",i,j,k);
    printf("%s",str);
    printf("\n");
    return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    FILE *fp;
    int i;
    int j;
    double k;

    fscanf(stdin," %d %d %lf",&i,&j,&k);
    fprintf(stdout,"%d,%d,%.2f",i,j,k);
    printf("\n");
    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

躺尸研究员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值