Whats wrong with this code?(only 28 lines)

Im trying to write a program that opens a text file(with stuff already written on it) and then changes all of its characters to uppercase
but its not working properly, it gets every character corectly and then displays it on the screen all uppercase like its supposed to do but when you look at the file it dosent change anything everything is still lowercase

Notes:
the changes have to be on the same file
and i cant use C++ cause were not using it yet(this is a exercise from my class)
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
#include <stdio.h> 
#include <conio2.h> 
#include <ctype.h> 
 
int main() 
{ 
    FILE *arq; 
    char c; 
    int n=1; 
    arq = fopen("arq.txt","a+"); 
     
    rewind(arq); 
    c = toupper( fgetc(arq) ); 
    while( !feof(arq) ) 
    { 
        fputc( (char)c, arq ); 
        printf("%c",c);    
        fseek(arq,( n*sizeof(char) ),0); 
        n++; 
        c = toupper(fgetc(arq)); 
    } 
    printf("\n\nDONE"); 
    fclose(arq); 
 
getch(); 
return 0; 
}
ok i made a few changes but now i ran into a different problem
if the file is just one line long it works perfectly
but if its 2 lines or more it enters a infinite loop =/
why is that???
it only changes the first line and then never finds the eof

here is the new code
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
#include <stdio.h> 
#include <ctype.h> 
 
int main() 
{ 
    FILE *arq; 
    char c; 
    int n=1; 
    arq = fopen("arq.txt","r+");
      
    c = toupper( fgetc(arq) ); 
    fseek(arq, (n-1)*sizeof(char), 0);
    fputc( c, arq );
    printf("%d",c);
    fseek(arq, n*sizeof(char), 0);
    n++;
    c = toupper( fgetc(arq) ); 
    while( !feof(arq) ) 
    { 
         fseek(arq, (n-1)*sizeof(char), 0); 
         fputc( c, arq ); 
         printf("%d",c); 
         fseek(arq, n*sizeof(char), 0); 
         n++; 
         c = toupper( fgetc(arq) );
    } 
    printf("\n\nDONE"); 
    fclose(arq); 
getchar(); 
return 0; 
}
Thanks everyone for all your help but a friend of mine told me of another solution to the problem
just open the file in binary mode
 
arq = fopen("arq.txt","rb+");


and here i quote r.stiltskin:

"As you probably know, in Windows, each line in a text file is terminated by two chars, 13 10 ('cr' 'lf'). For some stupid reason, when the file opened in text mode, when it comes to the end of the line fgetc (in Windows) ignores the 13 and just returns the 10. (And the same seems to be true for fread -- I tried that too.) But when you write a 'lf' to the file, fputc (in Windows) writes 'cr' 'lf'. So your program reads a 10, then writes 13 10, then advances 1 char and reads the 10 again, and so on forever. When you open it in binary mode, the program will read and write exactly what you want, 1 char at a time."
Topic archived. No new replies allowed.