output to file

This is a discussion on output to file within the C++ Programming forums, part of the General Programming Boards category; if i have the following code #include <iostream.h> #include <conio.h> #include <fstream.h> #include <assert.h> #include <STDIO.h> //LEVEL ONE FUNCTION PROTOTYPES/////////////////////////////////////////// ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    8

    output to file

    if i have the following code
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    #include <assert.h>
    #include <STDIO.h>

    //LEVEL ONE FUNCTION PROTOTYPES///////////////////////////////////////////
    void SaveToDisk(ofstream &neil);


    int main()
    {
    clrscr();
    ofstream neil;

    SaveToDisk(neil);

    neil.close();

    getch();
    return 0;
    }

    void SaveToDisk(ofstream &neil)
    {
    neil.open"test_file.txt");

    //why wouldn't neil.open(test_file.cstr()); work?

    assert(! neil.fail());
    neil<<1234<<endl;
    }

    where is the number1234 outputted to?-my floppy disk?
    also, why couldn't i use neil.open(test_file.cstr()); where i have the comment?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    1234 is written to the folder where your app is. If you wanted to write to the floppy, you could use "A:\\test_file.txt", or whatever your floppy drive letter is.

    You can't use test_file.c_str( ), because it will look for a variable named test_file, which isn't declared.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    you could have asked this in your other thread right below this one.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    "test_file" This is a string literal
    string test_file; This is a string object

    The c_str() method is a member of a string object. You can use it to obtain a char* representation of the string.

    test_file.c_str() This is valid because test_file has been declared as a string object

    As for your file output, try this:

    Code:
    #include <fstream>
    
    using namespace std;
    
    //Later...
    void SaveToDisk(ofstream & neil) {
            
            if(neil.is_open()) neil.close();
    
            neil.open("c:\\test_file.txt");
            neil <<1234 <<endl;
    
            neil.close();
    }

    This will write "1234" to the file c:\test_file.txt. Pretty straightforward.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 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