This code works, opens file "myfile.txt" for reading and writing and writes a-z characters in the file.
Code:
#include <stdio.h>
#include <stdlib.h>
int main ( void )
{
int c;
FILE * fp;
fp = fopen ( "myfile.txt", "r+" );
for ( c = 'a'; c <= 'z'; c++ )
putc ( c, fp );
/*while ( ( c = getc ( fp ) ) != EOF )
putchar ( c );*/
fclose ( fp );
return 0;
}
This code also works ( reads characters from the file )
Code:
#include <stdio.h>
#include <stdlib.h>
int main ( void )
{
int c;
FILE * fp;
fp = fopen ( "myfile.txt", "r+" );
/*for ( c = 'a'; c <= 'z'; c++ )
putc ( c, fp );*/
while ( ( c = getc ( fp ) ) != EOF )
putchar ( c );
fclose ( fp );
return 0;
}
But when I uncomment all code (code for writing characters and reading characters ) I get garbage characters both in my file and console window.
Something like this in console window:
═══════════════════════════════════
═══════════════════════════════════
═══════════════════════════════════
═══════════════════════════════════
═══════════════════════════════════
═══════════════════════════════════
═══════════════════════════════════
═══════════════════════════════════
═══════════════════════════════════...
and like this in the file:
abcdefghijklmnopqrstuvwxyzÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ
ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ...
This is the code that should only write characters a-z in the file and than read them in console window, but it doesn't work. Why?
Code:
#include <stdio.h>
#include <stdlib.h>
int main ( void )
{
int c;
FILE * fp;
fp = fopen ( "myfile.txt", "r+" );
for ( c = 'a'; c <= 'z'; c++ )
putc ( c, fp );
while ( ( c = getc ( fp ) ) != EOF )
putchar ( c );
fclose ( fp );
return 0;
}