Why do I have to use fseek in this code?

Why do I have to use fseek in this code?

There is a sentence in a text file and I am trying to insert a word into 9. character in the text file. For example,if the sentence is "Los Angeles is a good city,Paris is a good city" , my program will insert the text "CHPBL" into 9. character,after this modification the sentence in the file1.txt will look like:

"Los AngeleCHPBLs is a good city,Paris is a good city"


I did a little program to do it:

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);
 
 
}


This code works as I want but there is something which I am not able to understand.If I don't use fseek there ( I commented it in the code) it works wrong and I do not understand why I have to use fseek there.
Last edited on
You open the file, then:
fseek( fp, 9, SEEK_SET); // go to offset 9
int offset3 = ftell(fp); // where am I? offset3 = 9
fgets( buffer2, 6, fp); // read 6 bytes from offset 9, we're moved to offset 15
fseek( fp, offset3, SEEK_SET); // go back to offset 9
// ...

I guess someone wanted to make the code difficult to follow. I wouldn't bother going futher and not knowingly use it.
> Why do I have to use fseek in this code?

You should not be using fseek() in this manner.

For text streams, the only valid values of offset are ​0​ (applicable to any origin) and a value returned by an earlier call to ftell (only applicable to SEEK_SET).
http://en.cppreference.com/w/c/io/fseek
Thanks for your answers.
Topic archived. No new replies allowed.