Thread: how to create file in the running program

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    how to create file in the running program

    Hi,

    I am currently working on C programming language but I have encounter some doubt on it and wish to seek for someone who can help me solve my problem.

    I would like to know how can I create a file to save all my execution information that display on my monitor when I run my program.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You can write to a file instead of your screen.
    Code:
    #include<iostream>
    #include <fstream> //for ofstream
    
    using namespace std;
    
    int main()
    {
    	ofstream myOutputFile("C:\\TestData\\output.txt");
    	
    	for(int i=0; i<10; i++)
    	{
    		myOutputFile<<i<<"\n";
    	}
    
    	return 0;
    }
    or, do both:
    Code:
    #include<iostream>
    #include <fstream> //for ofstream
    
    using namespace std;
    
    int main()
    {
    	ofstream myOutputFile("C:\\TestData\\output.txt");
    	
    	for(int i=0; i<10; i++)
    	{
    		myOutputFile<<i<<"\n"; //output to file
    		cout<<i<<"\n";  //output to screen
    	}
    
    	return 0;
    }

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Create a file:
    Code:
    std::ofstream File("Output.txt");
    And for each std::cout you use, call a corresponding:
    Code:
    std::cout << xxx;
    
    --->
    
    File << xxx;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Another way is to start your program from the command line and append > filename.txt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Last program!!
    By buckwheat88 in forum C++ Programming
    Replies: 12
    Last Post: 01-17-2006, 12:31 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM