Writing to a file

This is a discussion on Writing to a file within the C++ Programming forums, part of the General Programming Boards category; Hey guys. I have a problem with writing to a file. I will try and simplify the problem so it ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    36

    Writing to a file

    Hey guys. I have a problem with writing to a file.

    I will try and simplify the problem so it might sound strange.

    Lets say I have a function which works out a series of numbers, once each number is found I call another function (say, printnumber). This function writes the number to the output file.

    My problem is, the output file seems to only display the last number. I think this is because in the print function i have a piece of code like this
    Code:
    ofstream output;
    output.open (argv);
    .....
    .....
    output.close();
    This would sort of over-write the file would it not.

    My question is, is there any simple way around this? I have thought about perhaps opening the file only once - outside the first function, but I am unsure how I would send the output file as a function parameter.

    Also, another quick question if I may. Say I have two command line parameters (file names). In main, how would I go about doing this :
    Code:
    ofstream output;
    output.open (??argv??);

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Couldn't you open the file just once and just write the numbers there?
    Otherwise you might open the file for appending.
    You might also post code.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can pass an ofstream via a const reference (that is,
    Code:
    void printnumber(int number, const ofstream& output)
    And I think your command-line arguments are going to be in argv[1] and argv[2].

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    It doesn't make sense to make the ofstream a reference to const, since you write to an ofstream and you can't do that if it is const. A plain reference is more appropriate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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