ÿ rubbish in my .bak file

M

MFO

Hi.. i don't knw is it a correct place to ask.. but i really need
help...

i have this backup code (below).. it successfully backup a file.. but
with this ÿ rubbish/bug at end of file.. so i don't know what cause it
appear.. any idea???

TQ

// Creates the backup file with extension .bak
//////////////////////////////////////////////
void CreateBackup (char filename[256])
{
char backup_filename [256]="";
char *ptr;
FILE *in, *out;

// to create backup file must take the filename
// without its extension and put .bak extension
strcpy (backup_filename,filename);
ptr=strtok(backup_filename,".");
strcat (ptr,".bak");



if ((in = fopen(filename, "rt"))
== NULL)
{
fprintf(stderr, "Cannot open input file.\n");
exit(1);
}

if ((out = fopen(backup_filename, "wt"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
exit(1);
}

while (!feof(in))
fputc(fgetc(in), out);
fflush(in);
fflush(out);
fclose(in);
fclose(out);
}
 
M

Michael DOUBEZ

MFO a écrit :
Hi.. i don't knw is it a correct place to ask.. but i really need
help...

i have this backup code (below).. it successfully backup a file.. but
with this ÿ rubbish/bug at end of file.. so i don't know what cause it
appear.. any idea???
[snip]
while (!feof(in))
fputc(fgetc(in), out);
[snip]

When fgetc(in) reach the end of the in file, you write the value
returned (-1) into out.

A common idiom (although not accepted in all coding standards) is:
int c;
while(EOF!=(c=fgetc(in)))
{
fputc(c,out);
}
 
K

Kai-Uwe Bux

MFO said:
Hi.. i don't knw is it a correct place to ask.. but i really need
help...

i have this backup code (below).. it successfully backup a file.. but
with this ÿ rubbish/bug at end of file.. so i don't know what cause it
appear.. any idea???

What bug, what rubbish?
TQ

// Creates the backup file with extension .bak
//////////////////////////////////////////////
void CreateBackup (char filename[256])
{
char backup_filename [256]="";
char *ptr;
FILE *in, *out;

// to create backup file must take the filename
// without its extension and put .bak extension
strcpy (backup_filename,filename);
ptr=strtok(backup_filename,".");
strcat (ptr,".bak");



if ((in = fopen(filename, "rt"))
== NULL)
{
fprintf(stderr, "Cannot open input file.\n");
exit(1);
}

if ((out = fopen(backup_filename, "wt"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
exit(1);
}

while (!feof(in))
fputc(fgetc(in), out);
fflush(in);
fflush(out);
fclose(in);
fclose(out);
}

The following should be more or less equivalent to what you try (except that
only the last suffix of the filename is replaced by "bak" and that there is
no hard-coded upper bound on the length of a file name).

int CreateBackup ( std::string const & file_name ) {
std::string::size_type last_dot = file_name.rfind( '.' );
std::string backup_name ( file_name, 0, last_dot );
backup_name += ".bak";

std::ifstream in ( file_name.c_str() );
if ( ! in ) {
std::cerr << "Cannot open input file.\n"
return ( -1 );
}

std::eek:fstream out ( backup_name.c_str() );
if ( ! out ) {
std::cerr << "Cannot open output file.\n"
return ( -2 );
}

out << in.rdbuf();
}


Note: there should also be a check that the files have been closed
successfully. (On the other hand, what are you going to if not?)


Best

Kai-Uwe Bux
 
J

James Kanze

MFO wrote:

[...]
Note: there should also be a check that the files have been
closed successfully. (On the other hand, what are you going to
if not?)

Output an error message to cerr, and exit with EXIT_FAILURE. If
the user thinks he has a copy, and he doesn't, he's likely to
be very surprised later, when he needs the copy.
 
M

MFO

Thanks to all, for your response..
I'll try to figure it out.. test it again..

Thanks a lot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,370
Latest member
desertedtyro29

Latest Threads

Top