hi,
I was writing a function in c++(with file handling) that takes the name of a file and another file on the same directory and copys the contents of the 2nd file to the beginning of the first file.....
void filappb()
{
ofstream fname;
ifstream inf;
char filename[25],info[25],ch;
cout<<"\nmake sure that the file to be appended and the info that needed to be added are in the same folder";
cout<<"\n enter file name with extension:";
gets(filename);
cout<<"\n enter info file name(with extension if any):";
gets(info);
fname.open(filename,ios::app);
if(!fname)
cout<<"\nerror required filename not found";
inf.open(info,ios::in);
if(!inf)
cout<<"\nerror required filename not found";
fname.seekp(0);
inf.seekg(0);
while(!inf.eof())
{
inf.get(ch);
fname.put(ch);
}
cout<<"\nappending process to the beginning of file completed";
}
but the problem is that when I check the first file the data seems to be added to the end of the first file insted of beginning(sigh!).Any help will be wonderfull
Append means write further data at the end of current file. One of the possible solution is to read all content of first file to memory, write second file content from the beginning of first file, overwriting content and then write saved content of first file. However it is unefficient if files are large.
Solution 1:
create a temporary file.
copy content of second file to it
copy content of first file to it
remove first file
rename temp file to first file name.
Solution 2:
Rename file1 to temp_file
create new file with file1 name
copy file2 here
copy temp_file (previously file1) here
delete temp_file
isint my code theoretically possible .Is something wrong with my code .I also wrote a function to add data to any selected part of the file but it also adds data to the end of the file
void filapmb()
{
ofstream fname;
ifstream inf;
char filename[25],info[25],ch;
cout<<"\nmake sure that the file to be appended and the info that is needed to be added are in the same folder";
cout<<"\n enter file name with extension:";
gets(filename);
cout<<"\n enter info file name(with extension if any):";
gets(info);
fname.open(filename,ios::app);
if(!fname)
cout<<"\nerror required filename not found";
inf.open(info,ios::in);
if(!inf)
cout<<"\nerror required filename not found";
inf.seekg(0);
fname.seekp(0);
double flag,no;
flag=filesize(filename);
cout<<"\n the file "<<filename<<" has "<<flag<<" bits of info\n enter a number between 0 and "<<flag<<" for bitwise insertion:";
cin>>no;
if(no>flag)
{
cout<<"\ninsertion not possible <OUT OF BONDS>";
exit(0);
}
fname.seekp(no);
while(!inf.eof())
{
inf.get(ch);
fname.put(ch);
}
cout<<"\nappending process bit-wise to any selected part of file completed";
}
When you are opening file in append mode, any writes will be made after the end of file. Seek is meaningless for it.
Also it is impossible to just insert data in the middle of file. If seek was succesfull, you would overwrite beginning of file with new data
1 2 3 4 5
while(!inf.eof())
{
inf.get(ch);
fname.put(ch);
}
is terribly slow.
Useful trick for dumping all content of stream into another stream: fname << inf.rdbuf();
Yes you were right it is overwriting the existion file so is there any way to do without overwriting the existing file and is it possible to write contents to random positions of the file
The one way is following:
1) Seek to the point in file you want to write.
2) save everything after it in memory/temp file
3) Seek back to the point of wrriting
4) Write needed information
5) Write saved data back.
void filapmb()
{
fstream fname;
fstream inf;
char filename[25],info[25],ch;
cout<<"\n\tmake sure that the file to be appended and the info that is needed to be added are in the same folder";
cout<<"\n\tenter file name with extension:";
gets(filename);
cout<<"\n enter info file name(with extension if any):";
gets(info);
fname.open(filename,ios::ate|ios::in);
if(!fname)
cout<<"\nerror required filename not found";
inf.open(info,ios::in|ios::ate);
inf.seekp(0);
if(!inf)
cout<<"\nerror required filename not found";
inf.seekg(0);
fname.seekp(0);
double flag,no;
flag=filesize(filename);
cout<<"\n\tthe file "<<filename<<" has "<<flag<<" bits of info\n enter a number between 0 and "<<flag<<" for bitwise insertion:";
cin>>no;
if(no>flag)
{
cout<<"\ninsertion not possible <OUT OF BONDS>";
exit(0);
}
fname.seekp(no);
fstream temp;
char ch2;
temp.open("tempfile",ios::out|ios::in);
while(!fname.eof())
{
fname.get(ch2);
temp.put(ch2);
ch2='\0';
}
fname.seekg(no);
temp.seekp(0);
while(!inf.eof())
{
inf.get(ch);
fname.put(ch);
ch='\0';
}
while(!temp.eof())
{
temp.get(ch2);
fname.put(ch2);
ch2='\0';
}
cout<<"appending to the selected part completed";
}
But it is not working and is only writing the info to temp file.Could you please explain problem with my code
I didin't get it I opened it as fstream so it should be opened both for reading and writing:do you want me to open it like this>> fname.open(filename,ios::ate)
fname.open(filename,ios::ate|ios::in); ate meants that file will be rewinded to the end after opening. in means you can read from it
You did not specify other open modes, so it will not do anything else
No. It opens it for writing. ios::trunc truncates file.
Maybe you are just open file with ios::out and then immideatly write without seeking, overwriting previous info?