code 文件的保存、读取

来源:互联网 发布:自制手机铃声软件 编辑:程序博客网 时间:2024/05/24 08:34


#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();
}




0 0