Almost done with This.

I need some Help with this one peace. This whole code works. But.. I used a .jpg image to test it out and It works. But the only thig is the jpg file gets corrupted while Moving.

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
#include <iostream>
#include <string>
using namespace std;

int main (void)
{
          size_t len = 0 ;
        const char a[] = "C:\\Users\\Darth Bane\\Desktop\\Gfx Stuff\\Images\\3761437952_084b306720.jpg";
  string b;
  cout << "What's your PSP Driver?\nMAKE SURE YOU USE TWO BACKLAHSES INSTEAD OF ONE!!!\n\n " ;
  getline (cin, b);
        FILE* in = fopen( a, "rb" ) ;
        FILE* out = fopen( b.c_str(), "wb" ) ;
              char buffer[BUFSIZ] = { '\0' } ;
                    if( in == NULL || out == NULL )
      {
      perror( "An error occured while opening files!!!" ) ;
      in = out = 0 ;
      }
      else // add this else clause
      {
      while( (len = fread( buffer, BUFSIZ, 1, in)) > 0 )
      {
      fwrite( buffer, BUFSIZ, 1, out ) ;
      }
       
      fclose(in) ;
      fclose(out) ;
       
      if( remove(a) )
      {
      printf( "File successfully moved. Thank you for using this mini app" ) ;
      }
      else
      {
      printf( "An error occured while moving the file!!!" ) ;
      }
      }

      system("PAUSE");
  return 0;
}


I have determined that the error is in here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
     fclose(in) ;
      fclose(out) ;
       
      if( remove(a) )
      {
      printf( "File successfully moved. Thank you for using this mini app" ) ;
      }
      else
      {
      printf( "An error occured while moving the file!!!" ) ;
      }
      }

      system("PAUSE");
  return 0;
}
How would I fix this. It is really appreciated Guys. I'm trying to Fix this as much as I can But it is easly fixed if I have some more people help.
Last edited on
In the following code snippet, fread is returning the number of bytes read successfully, which is not always equal to BUFSIZ.
1
2
3
4
      while( (len = fread( buffer, BUFSIZ, 1, in)) > 0 )
      {
                 fwrite( buffer, BUFSIZ, 1, out ) ;
      }

This means that if you read a smaller chunk than BUFSIZ, you will still be writing out a BUFSIZ length (and remember your buffer still contains the rest of what was in it from the last read).

Actually - looking at it, you should also swap around the count and element size, so that the return value is meaningful (i.e. between 0 and BUFSIZ, rather than 0 and 1).

From the top of my head:
1
2
3
4
      while( (len = fread( buffer, 1, BUFSIZ, in)) > 0 )
      {
                 fwrite( buffer, 1, min(BUFSIZ, len), out ) ;
      }


Might fix it :)
CC
Last edited on
Thanks that helped. I had to Delete the min in herefwrite( buffer, 1, min(BUFSIZ, len), out ) ; But it works now and It don't have problems. Now I can make that full program I was making
Actually - you're quite right - that you didn't need min here. len will always be the right size:

 
fwrite( buffer, 1, len, out ) ;


CC
Topic archived. No new replies allowed.