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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char buffer[6];
char buffer2[6];
char buffer3[6];
char word[6];
strcpy( word, "CHPBL" );
FILE* fp;
if ((fp = fopen( "file1.txt", "r+t" )) == NULL)
{
printf( "File error!" );
exit( EXIT_FAILURE );
}
fseek( fp, 9, SEEK_SET);
int counter=1;
int offset3 = ftell(fp);
fgets( buffer2, 6, fp);
fseek( fp, offset3, SEEK_SET);
fprintf( fp, "%s", word);
int offset = ftell(fp);
fgets( buffer, 6, fp);
int counter2=0;
while( !feof(fp) )
{
system( "pause" );
int offset2 = ftell(fp);
fseek( fp, offset2, SEEK_SET); //if I don't use this fseek
//program works wrong
//buffer3 takes wrong values
if ( fgets ( buffer3, 6, fp ) == NULL)
{
exit(0);
}
sayac++;
printf( "%d. while loop", counter2);
printf( "\nbuffer : %s \n ", buffer);
printf(" \nbuffer3 : %s \n ",buffer3);
if (counter==1)
{
fseek( fp, offset, SEEK_SET);
fprintf( fp, "%s", buffer2);
printf(" \nbuffer2 : %s \n", buffer2);
}
if (counter==0) fseek( fp, offset2, SEEK_SET);
fprintf(fp, "%s", buffer);
counter = 0;
strcpy( buffer, buffer3);
}
| |