fille read/write

Hello.
My program doesn't do its task well.What's wrong?
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
#include <stdio.h>

int main()
{
    char buffer[32000];
    FILE * inFile;
    inFile = fopen("g:\\1.txt", "r+");
    int lSize;
    int j , k;
    
    
   fseek( inFile,  0, SEEK_END);
   lSize = ftell( inFile );
   rewind( inFile );
  
   lSize = fread( buffer, 1, lSize, inFile );
   
   for( int i = 0;i < lSize;++i )
   {  
        switch( buffer[i] )
        {
          case 'C':
               buffer[i] = 'c';
               break;
         case '-':
         case 'x':
              j=i-2;
              k  = i;
              for(i += 1;i <lSize;++i)
               buffer[j] = buffer[i];
               i = k;
               break;
         
           }
         
         fwrite( buffer, 1, lSize, inFile );
    }
    fclose( inFile );     
    return 0;
}

closed account (3TXyhbRD)
What is your program suppose to do?
replace in a file something like 'Nb1-c3' with something like 'Nc3'
closed account (3TXyhbRD)
And what happens with the extra space? I mean 'Nb1-c3' made intro 'Nc3' won't change the file size, it will result in 'Nc3-c3'. Is that what you want?
closed account (3TXyhbRD)
Reading the code I notice some potential invalid memory access errors. What if your buffer has on position 0 or 1 the letter x?

You're writing a lot to your file, for every pass in the 1st for loop you're appending your stream. Even if you place your function outside the for loop it will append the modified stream because the position in file is, theoretically, at it's end.

Also j gets i - 2 and then you keep copying on the same spot (buffer[j - 2]) other values, j does not increase.

What if your file has more than 32000 bytes?
Reading the code I notice some potential invalid memory access errors. What if your buffer has on position 0 or 1 the letter x?

my file doesn't

You're writing a lot to your file, for every pass in the 1st for loop you're appending your stream. Even if you place your function outside the for loop it will append the modified stream because the position in file is, theoretically, at it's end.

Why at the end?I rewind file pointer.

Also j gets i - 2 and then you keep copying on the same spot (buffer[j - 2]) other values, j does not increase.

What if your file has more than 32000 bytes?

my file doesn't

I changed the code(thanks to your advice):

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
#include <iostream>

using namespace std;

#include <stdio.h>

int main()
{
    char buffer[32000];
    FILE * inFile;
    inFile = fopen("g:\\1.txt", "r+");
    int lSize;
    int j , k;
    for(int i = 0;i <32000;++i)
       buffer[i] = 0;
    
    

   fread( buffer, 1, 2000, inFile );
     
    cout << buffer << endl;
    
rewind(inFile);
   
   for( int i = 0;i < 2000;++i )
   {  

        switch( buffer[i] )
        {
          case 'C':
               buffer[i] = 'c';
               break;
         case '-':
         case 'x':
              j=i-2;
              k  = i;
              for(i += 1;i <lSize;++i)
              {
                    buffer[j] = buffer[i];
                    j++;
              }
               i = k;
               break;
         
           }
         }
         cout << "*******************\n\n" << buffer;
         rewind( inFile );
    fwrite( buffer, 1, 2000, inFile );
    
   fclose( inFile );     
   cin.get();
    return 0;
}

closed account (3TXyhbRD)
Looks good-ish.

In the old code, you was writing at the end because you rewind the pointer and then read lSize bytes which is equivalent to reading the whole file. After that fread(), the file pointer is back at the end and when you write it writes from there.

Also you may want to initialize your buffer with white spaces instead of ASCII 0. ASCII 0 is a special character that cannot be displayed and the OS will put something to show you that there is something there (well, it usually does that).

Last but not least, lSize is now used without having an actual value. You declare that variable but you don't initialize it anywhere.
Topic archived. No new replies allowed.