#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    9
    Rep Power
    0

    Unhappy Input and Output to file


    Hi guys, I need some major help here! I'm trying to add a few things to a current text file, and i'm having some major problems.

    well, I have one program that creates expressions of the form num1 + num2 and puts into a file say, file.txt.

    Then another program is supposed to access the file, and then concatenate = num3 to it. so for example,

    program 1 does

    1 + 2
    3 + 4
    10 + 4

    then program2 makes it

    1 + 2 = 3
    3 + 4 = 7
    10 + 4 = 14

    Sofar, the closest I got is to make the new writing to the bottom of the current text file. so for example,

    1 + 2
    3 + 4
    10 + 4
    1 + 2 = 3
    3 + 4 = 7
    10 + 4 = 14

    is there any way to write over the current line, or to just add a few numbers to the end of it?

    Please reply with any ideas! thanks!!!
  2. #2
  3. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    There are three usual ways to do this sort of thing

    1) While reading file.txt, write the intended results to a new file (file2.txt, say). Then delete the original file, and rename file2.txt to file.txt.

    2) Read the entire contents of file.txt into memory, close the file, optionally delete the file, reopen it for writing, rewind it, then write the intended results back.

    3) [actually a variant of approach 2] Open the file for both reading and writing ("rw"), read the contents into memory, rewind it, then write the intended results back.

    There are trade-offs with every approach; the best depends on your requirements.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    9
    Rep Power
    0
    But, if i were to say, put the whole thing into memory, what would be the best way? The thing is, I do need to put it into memory eventually.

    Also, with the rewinding idea... I tired to do that by opening the file again. but all that it seems to do is write to the end of the file! could anybody give me some examples of how to go about this? And what's the best way to check if i'm at the end of the file? I currently have it as feof(fp). Is that the best way??
  6. #4
  7. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    It's not really possible to say what the best way of reading a file into memory is: the best approach depends on what you're trying to achieve.

    For example, do you simply want the raw character data to be loaded into memory? One way is to use something like

    char *loaded_data = malloc(size_of_file);
    /* read into loaded_data */

    Of course, that changes the problem from one of parsing a file into one of parsing a lump of data. Not much of an improvement.

    Another way may be to define a linked list to hold your values.

    The following is pseudo-code something like C++, to illustrate the idea.


    struct Data
    {
    int left, right;
    byte op;
    Data *next;
    };

    main()
    {
    // open your file

    Data *data = NULL;
    Data *last;

    while (!end_of_file_reached)
    {
    if (data == NULL)
    {
    data = new Data;
    last = data;
    }
    else
    {
    last->next = new Data;
    last = last->next;
    }
    // read values from file and store into variable last
    // eg ...

    last->left = ...;
    last->right = ...;
    last->op = ...;
    last->next = NULL;

    }

    // close the file
    // open a new one

    last = data;
    while (last != NULL)
    {
    // write info from last back into new file
    last = last->next; // next time in loop, act on next lot of data
    }

    }


    As to your question about rewinding files.... opening a file does not necessarily rewind it. In the C standard library, for example, there is a function called rewind(FILE *) that rewinds a file.

IMN logo majestic logo threadwatch logo seochat tools logo