1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{FILE* fileFrom; //file created to be source
FILE* fileTo; //file created to be destination
string m_string1; //first string variable
string m_string2; //second string variable
string m_string3; //third string variable
fileFrom=fopen("c:/file_From.cbin","wb+"); //we create the file_From, writing the string values and its lengths
int m_integer1;
int m_integer2,m_integer3;
m_string1="first string";
m_string2="second string is very much longer than the first one, as you easily can see";
m_string3="third string is very much longer than the first one, too";
m_integer1=m_string1.length();
m_integer2=m_string2.length();
m_integer3=m_string3.length();
//now we write first the length, after that, the string itself
fwrite((char*)&m_integer1,sizeof(int),1,fileFrom);
fwrite((char*)&m_string1,sizeof(char),m_integer1,fileFrom);
fwrite((char*)&m_integer2,sizeof(int),1,fileFrom);
fwrite((char*)&m_string2,sizeof(char),m_integer2,fileFrom);
fwrite((char*)&m_integer3,sizeof(int),1,fileFrom);
fwrite((char*)&m_string3,sizeof(char),m_integer3,fileFrom);
fclose(fileFrom);
string string_from1,string_from2,string_from3; //created to store the value read from file_From in the following steps,
fileFrom=fopen("c:/file_From.cbin","rb+");
fileTo=fopen("c:/file_To.cbin","wb+");
//Noe we're going to read data from the first file and store the values in the new variables,
//to write them into another file, a copy of the first one..
fread((char*)&m_integer1,sizeof(int),1,fileFrom);
fwrite((char*)&m_integer1,sizeof(int),1,fileTo);
fread((char*)&string_from1,sizeof(char),m_integer1,fileFrom);
fwrite((char*)&string_from1,sizeof(char),m_integer1,fileTo);
fread((char*)&m_integer2,sizeof(int),1,fileFrom);
fwrite((char*)&m_integer2,sizeof(int),1,fileTo);
fread((char*)&string_from2,sizeof(char),m_integer2,fileFrom);
fwrite((char*)&string_from2,sizeof(char),m_integer2,fileTo);
fread((char*)&m_integer3,sizeof(int),1,fileFrom);
fwrite((char*)&m_integer3,sizeof(int),1,fileTo);
fread((char*)&string_from3,sizeof(char),m_integer3,fileFrom);
fwrite((char*)&string_from3,sizeof(char),m_integer3,fileTo);
fclose(fileFrom);
fclose(fileTo);
//now we check if the values written are ok
fileTo=fopen("c:/file_To.cbin","rb+");
fread((char*)&m_integer1,sizeof(int),1,fileTo);
cout<<m_integer1<<endl;
fread((char*)&string_from1,sizeof(char),m_integer1,fileTo);
cout<<string_from1<<endl;
fread((char*)&m_integer2,sizeof(int),1,fileFrom);
cout<<m_integer2<<endl;
fread((char*)&string_from2,sizeof(char),m_integer2,fileTo);
cout<<string_from2<<endl;
fread((char*)&m_integer3,sizeof(int),1,fileTo);
cout<<m_integer3<<endl;
//here jumps the segmentation fault at debugging
fread((char*)&string_from3,sizeof(char),m_integer3,fileTo);
cout<<string_from3<<endl;
fclose(fileTo);
return 0;}
| |