Thread: "I'm go-ing craaazy..." (fin input problems

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    6

    "I'm go-ing craaazy..." (fin input problems

    I'm going crazy here..
    I think I've gone so long staring at this that I don't see the obvois...so pls

    here's the thing, i want to read a line from a (temp) file into a string

    Code:
    FILE *newNodes = fopen("new_nodes.tmp", "w+");
    ...
    fputs(argv[1], newNodes);
    ...
    char node[15];
    ...
    fgets(node, 15, newNodes);
    printf("value of node is %s", node);
    the fputs works fine, i get the string i want stored in the file. i can compile and all, but the printf just prints the text, nothing else.
    if i try what to me is just the same (with other names) in a test program it works fine, but not when it counts....

    and im going to put and read more things in the file later, so don't just tell me to take the string directly from argv

    /D

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're trying to read from where the fputs() finished writing.

    Code:
    fputs(argv[1], newNodes);
    
    /* add these 2 lines */
    fflush( newNodes );  /* ensure file is up to date */
    fseek( newNodes, 0, SEEK_SET ); /* reset file position indicator back to the beginning of the file */
    
    fgets(node, 15, newNodes);
    printf("value of node is %s", node);
    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.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    6
    thanx...
    that solved it.
    but i think another problem may have occured, i'm not sure though cuz i haven't gotten that far yet.
    this part of the program workes something like this:
    Code:
    while(!done){
    read next item from the file
    call functions with value taken from file
    }
    and these functions will add more stuff (lines) to the file. if i rewind the file within the loop, i'll just get the first line (right?).
    if i understand things right i need to seek before i want to read every time, and seek for what i can tell will rewind a given number of BYTES, not lines. i know how many lines have been written, but not how many chars.
    how do i do so that the next time the loop comes around to read the new line, it will get the line after the one it read the previos time.

    one way might be to store the last read line somwhere, make a loop to read until it's read the line it had the last time (just with strcmp) and then read one more line, but this seems rather silly...

    /D

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why are you writing to a file only to read it back again sometime later. Wouldn't an array / list in memory be more efficient?

    You can do this to remember the last read/write position
    Code:
    long write_pos = 0;
    ...
    fseek( fp, write_pos, SEEK_SET ); /* goto last write pos */
    fputs( line, fp ); /* write something */
    write_pos = ftell( fp ); /* save the write pos */
    Do the same for reading, and you can shuffle back and forth through the file as you want to
    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.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    6
    thanx again, that seems like it would do the trick (for now atleast).

    and why i used the file was because i don't know how much i'll data i have to store, and cuz i'm not very good with memory-managing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with commas as input
    By Yojimbo III in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2003, 09:18 PM
  2. Input file stream problems
    By Shadow12345 in forum C++ Programming
    Replies: 10
    Last Post: 10-06-2002, 06:33 PM
  3. Problems when trying to input a name to a program.
    By Grayson_Peddie in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 06-03-2002, 03:58 PM
  4. White space problems in file input
    By cxs00u in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2002, 11:06 PM
  5. function and input problems
    By meka in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2001, 11:56 AM