Thread: Why do I have to fseek in this code?

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    29

    Why do I have to 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:

    Code:
    #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 must use fseek there.
    Last edited by Awareness; 03-05-2014 at 08:14 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    fseek adds an offset to the current possition of the file pointer. At a quick glance, that looks like offset2+offset2

    fseek() - C Library Function Example

    If you are curious, you can use printf to see what ftell returns; then add another one after the fseek to see where it is after.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    29
    Thanks for your answer

    You said fseek adds an offset to the current position of the file pointer.If I use SEEK_CUR ( fseek(fp,offset2,SEEK_CUR)) you are right,but if I use SEEK_SET instead of SEEK_CUR, shouldn't it add offset from the position of beginning of the file?

    I tried what you said:

    Code:
             int offset2=ftell(fp);
             printf("\noffset2:%d\n",offset2);
             
            
             fseek(fp,offset2,SEEK_SET);
    
    
             int offset12=ftell(fp);
             printf("\noffset2 : %d\n", offset12 );
    I used this code inside the code I wrote in my question,and both offset2 and offset12 printed the same values?
    Last edited by Awareness; 03-06-2014 at 01:32 AM.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You are correct with the SEEK_SET [sorry]

    If you had a file with the numbers 0 to 1000, what do you get when you read from the file?

    Where is the program running, by the way? (i.e. Windows 8.1, Linux, PIC micro, ...)
    Fact - Beethoven wrote his first symphony in C

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, you should not be using feof in a control loop
    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    29
    Thanks for your answer.

    Where is the program running, by the way? (i.e. Windows 8.1, Linux, PIC micro, ...)
    Program is running on Windows 7.

    You are right about using feof() in loops,in my program I think it doesn't work actually, the lines:

    Code:
    if ( fgets ( buffer3, 6, fp ) == NULL)
             {
                 exit(0);
             }
    these lines do the job of checking if the end of file is reached in my code I think,feof which I used in my code doesn't work(or at least doesn't work right).
    Last edited by Awareness; 03-10-2014 at 06:57 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to 'C' using fseek(), please help
    By leopardforest@g in forum C Programming
    Replies: 9
    Last Post: 05-09-2007, 04:53 PM
  2. Reg. fseek
    By pokks in forum C Programming
    Replies: 1
    Last Post: 01-16-2006, 01:28 PM
  3. Help With fseek();
    By Explicit in forum C Programming
    Replies: 3
    Last Post: 05-26-2004, 08:40 PM
  4. fseek
    By Max in forum C Programming
    Replies: 5
    Last Post: 12-15-2003, 03:21 PM
  5. fseek ???
    By davie_scotland in forum C Programming
    Replies: 2
    Last Post: 02-19-2002, 06:13 PM