how to open a file after running the program

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 109
Reputation: rajatC is an unknown quantity at this point 
Solved Threads: 7
rajatC's Avatar
rajatC rajatC is offline Offline
Junior Poster

how to open a file after running the program

 
0
  #1
Dec 19th, 2007
I want to write all the output to the output file and don't want to print on the console output...the file (in which the output has been written) should be opened after i run the program.

which function to use for opening a file to see output( at run time)??

Thanks & Regards.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: how to open a file after running the program

 
0
  #2
Dec 19th, 2007
@rajatC
your question is not clear. Do you want to redirct standered output?
You can use CreateFile API to open the file. However, keep this in mind, and make sure there is no sharing violation.
Last edited by dubeyprateek; Dec 19th, 2007 at 3:26 pm.
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 109
Reputation: rajatC is an unknown quantity at this point 
Solved Threads: 7
rajatC's Avatar
rajatC rajatC is offline Offline
Junior Poster

Re: how to open a file after running the program

 
0
  #3
Dec 20th, 2007
ok...let me restate my problem..
see this code snippet
  1. int main()
  2. {
  3. ofstream fout("output.txt");
  4. //processing.
  5. //output to file
  6. //using fout
  7. //no console output
  8.  
  9. //showFile(output.txt);
  10. return 0;
  11. }

see the second last line of my code... the output has been saved to output.txt.. no console output..
when i run the program the command window will blink...as i have not used system("pause") to retain it...

what i want to do with showFile function is that it will open the file output.txt so that user can see the file...

i hope now you got it...
Last edited by rajatC; Dec 20th, 2007 at 3:31 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: how to open a file after running the program

 
0
  #4
Dec 20th, 2007
do it the same way as you would with cout to print to the console
fout << "Hello World\n"; . The syntax is identical to cout. Then close the file fout.close(); before calling your showFile() function so that everything gets physically written to the disk.
Last edited by Ancient Dragon; Dec 20th, 2007 at 7:01 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 109
Reputation: rajatC is an unknown quantity at this point 
Solved Threads: 7
rajatC's Avatar
rajatC rajatC is offline Offline
Junior Poster

Re: how to open a file after running the program

 
0
  #5
Dec 20th, 2007
@ancient dragon..

you didn't get my question...i know how to output to file...but i wanted to ask is there any function which will open the file for user to see the output...
is there any showFile() type of function in libraries??
Life is about being happy...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,576
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: how to open a file after running the program

 
0
  #6
Dec 20th, 2007
>is there any showFile() type of function in libraries??
No, you have to write it yourself.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 109
Reputation: rajatC is an unknown quantity at this point 
Solved Threads: 7
rajatC's Avatar
rajatC rajatC is offline Offline
Junior Poster

Re: how to open a file after running the program

 
0
  #7
Dec 20th, 2007
I DONT know how to do that...can you help me in writing that...
how a file would be opened for user to show the output by the code itself??
Life is about being happy...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,576
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: how to open a file after running the program

 
0
  #8
Dec 20th, 2007
>I DONT know how to do that...
I'm willing to bet you do, but you're just not making the connection:
  1. #include <fstream>
  2. #include <string>
  3.  
  4. // Open a file and display it
  5. void showFile ( const char *filename )
  6. {
  7. std::ifstream in ( filename );
  8. std::string line;
  9.  
  10. while ( std::getline ( in, line ) )
  11. std::cout<< line <<'\n';
  12. }
Though I'll go out on a limb and wager that you don't want console output, but rather you want to start some random text editor that will display the file for you. That's non-portable, but you might do something like this:
  1. #include <cstdlib>
  2. #include <string>
  3.  
  4. // Open a file and display it in the specified program
  5. void showFile ( const std::string& program, const std::string& filename )
  6. {
  7. std::system ( ( program + " " + filename ).c_str() );
  8. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: how to open a file after running the program

 
0
  #9
Dec 20th, 2007
Of course the user could always use Notepad to view the file.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,576
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: how to open a file after running the program

 
0
  #10
Dec 20th, 2007
>Of course the user could always use Notepad to view the file.
Oddly enough, my test call for that function was:
  1. showFile ( "notepad.exe", "C:\\narue_is_kewl.txt" );
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC