appendinag a file

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.....

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

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

Thanks
Cyber Dude
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

These function can be useful:
http://en.cppreference.com/w/cpp/io/c/remove
http://en.cppreference.com/w/cpp/io/c/rename
http://en.cppreference.com/w/cpp/io/c/tmpnam
Last edited on
thanks,

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
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
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";
}



Last edited on
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();
could opening in ios::ate mode help me

Hey I tried and it worked thanks MiiNiPaa
Last edited on
Why do you need to opent it in ate mode if you do not want to write to the end of file?

Either way, you still cannot insert data to the random areas without overwriting it
Example
1
2
3
4
5
6
7
8
9
//File content and cursor position
Hello World!!!
^
//after seek
Hello World!!!
     ^
//Writing ", Happy"
Hello, Happy!!  //New data is underlined
              ^
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
Last edited on
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.
you mean like this??
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
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
Last edited on
fname is not opened for writing. You have only opened it for reading.
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)
I opened it as fstream so it should be opened both for reading and writing
No, it means, it supports both reading and writing.
do you want me to open it like this
If you specify openmode, you'll be better to specify it fully
fname.open(filename,ios::in|ios::out|ios::ate)
Last edited on
no,not that you said
  fname is not opened for writing. You have only opened it for reading.    
what does that mean could you please correct me.I didin't get it
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
Last edited on
sooo how do i rewrite my code if I use app I could not move the file pinter to any place....
MiiNiPaa wrote:
If you specify openmode, you'll be better to specify it fully
fname.open(filename,ios::in|ios::out|ios::ate)
Last edited on
but doesn't ios::out tranculate the existing file???BUT IT WORKED THANKS but I
am still curious
Last edited on
but doesn't ios::out tranculate the existing file
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?
ok thanks for the info MiiNiPaa
Topic archived. No new replies allowed.