C/how to open a file?
Expert: Zlatko - 7/20/2010
QuestionQUESTION: Hi Zlatko,
When you have printed to the text file, (as when we make an error log file like we discussed last time), what would you do to have the program automatically open the text file (since I'm using windows OS, perhaps notepad) at the end and show up on the monitor like a virus scan tool or something of that sort would do to show the results of the scan it just completed?
I am only aware of the fopen function with the "r", "w", wb", etc commands.
Thanks
ANSWER: Hello Mike.
If you want the file opened in notepad for viewing there are 2 ways. The simpler is to use the ShellExecute function which will open the file with the program set to handle that file's extension. For example, by default on windows systems, the .txt extension is opened with notepad. There is no default association with a .log extension, so I suggest you use a .txt for your log file. Whatever happens when you double click on a file is what will happen when you call ShellExecute on the file with the "open" operation. Read about ShellExecute in your help system or online at msdn.microsoft.com .
Specifically
http://msdn.microsoft.com/en-us/library/bb762153%28VS.85%29.aspx
The other, more complicated way, is to use CreateProcess to start notepad.exe with the log file name. I suggest you give ShellExecute a try and let me know if you have problems.
Best regards
Zlatko
---------- FOLLOW-UP ----------
QUESTION: Zlatko,
I included <windows.h> header at the top and then later used
system("notepad.exe c:\\errlog.txt");
which does open file like I wanted, and that's appropriate to use?
Mike
AnswerSure that works too. I usually don't use the system call, because it opens a command interpreter to execute the command, so it is an extra step, and it would halt your program until you exit notepad. But I suppose there is nothing wrong with that in your case.