Thread: fseek - need some info please

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    23

    fseek - need some info please

    I have read the boards, and It seems I am doing this right, but I am receiving garbage in my file:

    I want to open a file, move to the 98th byte and write an email address:
    Code:
    define EMAILADDRLEN 90 // this is set above in the program
     
    int e_loc = 98; // this is set above in the program
     
     
    if ( (out = fopen("gorseve_10774.csv","w")) != NULL ) 
    { 
    while ( (returnCode = fgets(lineOfText, LINESIZE, in )) != NULL) 
    { 
      e_loc = parseGorseveRecord(lineOfText); 
      printf("location found %d\n", e_loc); 
      if ( (ret = fseek(out, e_loc, SEEK_CUR)) == 0 ) 
      { 
        fwrite("[email protected]",sizeof("[email protected]"),EMAILADDRLEN,out); 
      } 
      else 
      { 
        printf("Error locating write position\n"); 
        exit(1); 
      } 
      fclose(out); 
      exit(0); 
      } 
    } 
    else 
    { 
    printf("Error Opening File: filename\n"); 
    exit(1); 
    }
    Last edited by Salem; 08-03-2006 at 11:34 AM. Reason: Nice shiny code tags added, please use them in future

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    should I be using fgetpos and fsetpos instead?

    Thanks in advance !

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by mhenderson
    I want to open a file, move to the 98th byte and write an email address:
    Your code does something else. First of all the position value is overwritten by
    Code:
    e_loc = parseGorseveRecord(lineOfText);
    and then you use
    Code:
    fseek(out, e_loc, SEEK_CUR)
    This moves e_loc bytes forward from the current position.
    Kurt
    EDIT sorry overlooked that read from a different file. But still the position is overwritten with another value.
    Last edited by ZuK; 08-03-2006 at 11:24 AM.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    sorry for that confusion,

    that's what I meant by e_loc being set to 98 above, i just didn't want to copy in my parseGorseveRecord function. But I test the value of e_loc here:

    printf("location found %d\n", e_loc);
    This prints out 98.

    Then the program exits.
    When I check the file it has written over everything that was in it and just puts a 2 -3 lines of garbage?

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The problem is the open mode "w" creates a new file. guess you want "a" ( but then you should use use SEEK_SET )
    Kurt
    Last edited by ZuK; 08-03-2006 at 11:29 AM.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    Wow, I had that all wrong....

    thank you so much!!

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    One more question

    Once I've written that email address, is there an easy way to skip to the beginning of the next line ?

    thanks in advance )))

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by mhenderson
    One more question

    Once I've written that email address, is there an easy way to skip to the beginning of the next line ?

    thanks in advance )))
    To find the next line you could open the file in read/write mode ("a+") and just read until you find a '\n'
    On the othe hand combining textmode with fseek() is not a very good idea. try "rb+"
    Kurt

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > fwrite("[email protected]",sizeof("[email protected]"),EMAIL ADDRLEN,out);
    This writes a hell of a lot more than you bargained for to the file.
    Sizeof on a string constant is the number of chars in the string, and the \0 at the end.
    And you write 90 copies of it!


    In-place edits on text files isn't really the thing to do.

    Read a line from the input file
    Modify the line
    Write the line to the output file.

    When you're done, delete the old file and rename the new file.
    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.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    yes, this sounds much better, if I use write ,will it just write an entire line over current line?

    How do I go to the next line, and the next, etc.

    Thanks again...I've been looking at this too long

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can only write to the end of a file opened in append mode. You'd be best off creating a new file as Salem suggested.

    [edit] The thread is continued here: http://cboard.cprogramming.com/showthread.php?t=81559 [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fseek failing - Error: Invalid argument
    By avi2886 in forum C Programming
    Replies: 14
    Last Post: 05-14-2009, 08:49 PM
  2. help displaying info from structure
    By kisiellll in forum C Programming
    Replies: 6
    Last Post: 04-04-2009, 12:51 PM
  3. fseek() changing an unrelated variable?
    By aaronvegh in forum C Programming
    Replies: 3
    Last Post: 11-21-2007, 02:30 PM
  4. Question about getting an info class from another Form
    By Joelito in forum C# Programming
    Replies: 0
    Last Post: 10-16-2006, 01:02 PM
  5. Help doing an e-mail program in c...
    By Tyler_Durden in forum C Programming
    Replies: 88
    Last Post: 01-02-2005, 03:12 PM