Thread: FILE pointer

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    10

    FILE pointer

    Hi,
    I have to find the ‘position’ of ‘23’ in file a1.txt. At the corresponding ‘position
    in a2.txt, I want to add 500 to member value. I am able to pick up the values from both a1.txt and a2.txt but the problem comes when I a trying to replace the value in a2.txt by adding 500 to the corresponding member value. The value is not getting printed in a2.txt.

    I am using gcc compiler. Kindly help..

    a1.txt:
    11
    12
    13
    ....
    23
    24
    25

    a2.txt:
    61
    62
    63
    ...
    73
    74
    75

    Code:
    #include<stdio.h>
    int main()
    {
    
    FILE *fp,*fp1;
    int i,ch;
    long n;
    
    fp1=fopen("a1.txt","r");
    
    while(fscanf(fp1,"%d",&i) == 1)
    
    {
       
         if(i==23)
        {
            n=ftell(fp1);
            printf("%ld\n",n);
            break;
        }
    
    
    }
    
    fclose(fp1);
    
    n=n-2;                                      //offset starts with '0'
    fp=fopen("a2.txt","rw");
    
    fseek(fp,n,0);        
    fscanf(fp,"%d",&i);
    
    fprintf(fp,"%d\n",i+5);
    fclose(fp);
    
    return 0;
    
    }
    Last edited by Sanchari Sircar; 10-26-2012 at 11:47 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    i think you haven't understand the offset thing.If first search for 11 then you will get n equal to 2.Why?
    Because the pointer of file was moved by scanf until it reads the first number,which in this case is two digit number,so it will move from zero position to the second one (it will be increased by two).If you had a n-digit number it would be increased by n.Then if you search for 12,then fscanf will read the first two digit number,then the newline character and then the number 12(two digit),so it will increase the pointer by (2+1+2)...Hope this helps

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    I got that part...I am facing problem when i am trying to print i+500 instead of i in the second file. Its not getting printed at all. fp is correctly pointing at 73...I checked it.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    look at the manual for fopen: fopen(3): stream open functions - Linux man page. Where do you see "rw" as a valid mode? You have to pick a valid one for what you want to do. You want text mode, read and write.

    EDIT: Also, you should check the return value of fopen. If it fails, print a sensible error (use the perror function) and exit.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if your file looks like
    23\n
    24\n

    And you seek to the start of 23 and write 525, then the file is going to look like
    523\n
    4\n

    If you're changing the length of any line, you're going to have to rewrite the file.
    There is no magic "insert text here" option at this level.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    By looking at reference fopen - C++ Reference i would suggest another approach which is more costly but easy to achieve.Find the position as you do.Then go to the second file and open it with "r" .I think what you are using for it does not exist.Then read this data in an array.Then close the file.Then modify this array (go to the position you want and increase the value).Then open again the very same file with "w".Now you are overwriting what is in the file,so just go and write there the array.Then of course close it again

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    Thanks..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-27-2012, 07:05 AM
  2. File I/O: Offsetting file pointer (fp) using fseek
    By yesyesyes in forum C Programming
    Replies: 5
    Last Post: 03-21-2011, 05:33 PM
  3. file input and file pointer problem
    By drater in forum C Programming
    Replies: 6
    Last Post: 02-18-2008, 10:43 AM
  4. Position FILE pointer to a specified line of Text file
    By jessica_xie in forum C Programming
    Replies: 2
    Last Post: 02-04-2005, 03:52 PM
  5. Replies: 1
    Last Post: 10-21-2002, 02:32 PM

Tags for this Thread