appending output file...

This is a discussion on appending output file... within the C Programming forums, part of the General Programming Boards category; Hi, I'm wondering if someone can help get me started on this. I sat down to do it and realized ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    32

    appending output file...

    Hi, I'm wondering if someone can help get me started on this. I sat down to do it and realized I couldn't. lol
    This is what I'm trying to do:
    I have an output file that I want to append. The tricky part is I want to scan in the last number listed in the file, add to it another number, then print the sum onto the next line. Then do it again, so scan in the sum I just printed (now the last number listed), add a number, and print the new sum on the next line.

    Just in case that was confusing this is what I mean.
    In the file I have

    1

    So I take 1 and add 2 so I have

    1
    2

    Then I take 2 and add 3 so I have

    1
    2
    5

    etc.

    I was using "a+" to open the file... I didn't get very far.
    Any suggestions appreciated.
    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    But you always know the last number in the file, because you just wrote it.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    Well, if you could tell me a better way to update the scan to that value, I'd be fine with that. But I'm doing this with a huge list, so it's not like I can just type it in each time like in my little example.
    I can do this with an array, but like I said, it's huge and my compiler was freaking out because I had a lot of huge arrays, so I wanted to just output everything instead.

    There has to be some fairly simple way of doing this. :S

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    The last successful read of a number will place you at the end of the file.
    So a loop to read the 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    I was trying something along these lines...
    It would maybe do the first number correctly, but then I'm not sure what it was doing. I really didn't get that far.

    (This is simplified, delta is defined and things.)

    Code:
         fx=fopen("x.txt", "a+");
         for(i=0; i<10; i++){
                  fscanf(fx, "&#37;lf\n", &previous);
                  fprintf(fx, "%e", previous+delta);
                  }
                  
         fclose(fx);
    Any comments on that?

    I was hoping that each time previous would switch to the number that had just been printed and it would move down the line, but it doesn't seem to be happening.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by sqytoad View Post
    Hi, I'm wondering if someone can help get me started on this. I sat down to do it and realized I couldn't. lol
    This is what I'm trying to do:
    I have an output file that I want to append. The tricky part is I want to scan in the last number listed in the file, add to it another number, then print the sum onto the next line. Then do it again, so scan in the sum I just printed (now the last number listed), add a number, and print the new sum on the next line.

    Just in case that was confusing this is what I mean.
    In the file I have

    1

    So I take 1 and add 2 so I have

    1
    2

    Then I take 2 and add 3 so I have

    1
    2
    5

    etc.

    I was using "a+" to open the file... I didn't get very far.
    Any suggestions appreciated.
    Thanks.
    The way I would do this is:

    Open the file for r+
    read the first number from the file
    Assign it to old_num variable.
    Assign the value for new_num. This is necessary only on the first number.

    Start the loop:
    Add old_num + new_num to get the comp_num.
    write out the computed number (comp_num), to the file.

    Now assign the value of new_num to old_num, and the value of comp_num to new_num
    And loop back.

    I'm working out the above without benefit of working through it with paper and pencil, but you should use both to check if the logic above is correct.

    Once you see the pattern of how it works on paper, you'll be able to program it up, correctly.

    There is no need to fscanf() anything from the file.
    Last edited by Adak; 06-18-2008 at 12:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  2. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21