code 文件的保存、读取

本文介绍了一个使用C++进行文件读写的简单示例,包括如何保存字符串路径及评分到文本文件,并从该文件中读取数据。通过示例展示了文件操作的基本流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include "FRSAPI.h"

//save file
void save_file(string path_m,string path_n,float score)
{
   
FILE *fp = NULL;
   
char word[1000];

       
memset(word,'\0',sizeof(word));
   
if((fp=fopen("score.txt","w+"))==NULL)
   
{
       
cout<<"save scorefail."<<endl;
       
exit(0);
   
}
    sprintf(word,"%s %s
%f",(path_m).c_str(),(path_n).c_str(),score);

    fputs(word,fp);//文本
    //
fwrite(word,sizeof(word),1,fp);//二进制

   
fclose(fp);
   
cout<<"save scoresuccessed."<<endl;
}

int main()
{
   
string it1 ="/usr/local/src/NeoFRS2/Releasev1.0/sample/images/Photo1.jpg";
   
string it2 ="/usr/local/src/NeoFRS2/Releasev1.0/sample/images/Photo2.jpg";
   
float fScore = 1.0;
   
save_file(it1,it2,fScore);
}




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

void read()
{
    char word[1000];
    char ch;
    FILE *fp=NULL;
    if((fp=fopen("score.txt","r+"))==NULL)
    {
        printf("read filefailed\n");
        exit(0);
    }
    ch=fgetc(fp);
    if(ch==EOF)
    {
        exit(0);
    }
    rewind(fp);
    while(!feof(fp))
    {

        fgets(word,sizeof(word),fp);
        fread(word,sizeof(word),1,fp);
        printf("word :%s\n",word);
    }
    fclose(fp);
}

int main()
{
    read();
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值