You are here:

C/how to open a file?

Advertisement


Question
QUESTION: 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

Answer
Sure 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.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.