sprintf、fprintf、fscanf函数

1、sprintf函数

        snprintf(aszFileName[i],32, "stream_chn%d_%d%s", i, u32PictureCnt[i],szFilePostfix);   // sprintf(scan_str, "%s=%%d.%%d.%%d.%%d", type_str);
         pFile[i] = fopen(aszFileName[i], "wb");
         if (!pFile[i])
        {
              SAMPLE_PRT("open file err!\n");
              return NULL;
        }
        s32Ret = SAMPLE_COMM_VENC_SaveStream(pFile[i], &stStream);

//定义上面两个用到的函数

HI_S32 SAMPLE_COMM_VENC_SaveStream(FILE* pFd, VENC_STREAM_S* pstStream)
{
    HI_S32 i;
    static HI_S32 oneTimeFlag;   //by Yugang
    if(oneTimeFlag < 300) 
    {
        oneTimeFlag ++;
        printf("pstStream->u32PackCount: %d\n", pstStream->u32PackCount);
    }

    for (i = 0; i < pstStream->u32PackCount; i++)
    {
        fwrite(pstStream->pstPack[i].pu8Addr + pstStream->pstPack[i].u32Offset,
               pstStream->pstPack[i].u32Len - pstStream->pstPack[i].u32Offset, 1, pFd);

        fflush(pFd);
    }

    return HI_SUCCESS;
}

#ifndef SAMPLE_PRT
#define SAMPLE_PRT(fmt...)   \
    do {\
        printf("[%s]-%d: ", __FUNCTION__, __LINE__);\
        printf(fmt);\
    }while(0)
#endif

 

2、fprintf函数

功能

fprintf()函数根据指定的格式(format),向输出流(stream)写入数据(argument)。

函数说明

fprintf( )会根据参数format 字符串来转换并格式化数据,然后将结果输出到参数stream 指定的文件中,直到出现字符串结束('\0')为止。

#include <cstdio>

int main(void) {

    FILE *FSPOINTER;

    char STRBUFF[16] = "Hello World.";

    //...

    FSPOINTER = fopen("HELLO.TXT""w+");

    //...

    fprintf(FSPOINTER, "%s", STRBUFF);

    //...

    return 0;

}

输出至文件HELLO.TXT:

Hello World

 

3、fscanf函数

首先我有一个data。txt的文件里面的数据格式如下:

stu1=2,50,41,w,20.585828

stu2=4,52,51,r,52.012547

.........................

 许多条类似的记录,都是以,来分隔的

.......................

我实现的功能就是把上面文件中的数据的五个字段赋值给相应的五个变量,并且输出这些变量的值。实现的程序如下:

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


int main()
{
  int fd;
  long dev;
  long offset;
  long length;
  char ch;
  double ts=0.000000;
  if((fd=fopen("/home/haixian/ceshi/data.txt","r"))<0)
   {
     printf("open the file is error!\n");
     exit(0);
   }
  lseek(fd,0,SEEK_SET);
  
  
  while(5==fscanf(fd,"%s=%ld,%ld,%ld,%c,%lf\n",&dev,&offset,&length,&ch,&ts))  // %s提前做好标识字符串 如:stu1  stu2 stu3
  {在这里就是第二个参数指定分隔参数的格式,在这里使用的是,来分隔。这样就很容易的获取了记录的各个字段的值并不需要自己编写函数来进行解析什么的
     printf("%ld,%ld,%ld,%c,%lf\n",dev,offset,length,ch,ts);
  }
close(fd);
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值