NO

Author Topic: using stdout for error messages in a console program  (Read 3944 times)

Slothrop

  • Guest
using stdout for error messages in a console program
« on: October 08, 2013, 10:48:36 PM »
Hello - I am working on a console program that takes a text file as input and writes to another text file. I've been using the console window to display error messages, which works fine when I run the program from within the IDE. But when I run it by double-clicking the .exe file the console window closes instead of waiting with "Press any key to continue..." Is there a way to get the standalone program to retain this behavior? Sorry if this is a stupid question. I did try searching but didn't find an answer.

A simplified version of what I'm doing looks like this:

Code: [Select]
#include <stdlib.h>
#include <stdio.h>

#define INPUT_FILENAME "input001.txt"
#define OUTPUT_FILENAME "output001.txt"

void error(char *message);

int main(void) {
   FILE *infile;
   FILE *outfile;

   if ((infile = fopen(INPUT_FILENAME, "r")) == NULL) {
      error("[error: couldn't open input file]\n");
   }
   if ((outfile = fopen(INPUT_FILENAME, "w")) == NULL) {
      error("[error: couldn't open output file]\n");
   }
   // read text
   // do useful things to it
   // write text
   fclose(infile);
   fclose(outfile);
   return 0;
}

void error(char *message) {
   printf(message);
   exit(0);
}

Many thanks in advance.

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: using stdout for error messages in a console program
« Reply #1 on: October 08, 2013, 11:28:24 PM »
You may use getchar();
However, in contrast to what the name suggests, it expects a Return

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: using stdout for error messages in a console program
« Reply #2 on: October 09, 2013, 12:50:19 AM »
Hello - I am working on a console program that takes a text file as input and writes to another text file. I've been using the console window to display error messages, which works fine when I run the program from within the IDE. But when I run it by double-clicking the .exe file the console window closes instead of waiting with "Press any key to continue..." Is there a way to get the standalone program to retain this behavior? Sorry if this is a stupid question. I did try searching but didn't find an answer.
Well, you have simply been looking for something that doesn't exist (to be able to be retained)...  ;)

The "Press any key to continue..." is printed/executed by the IDE, on return of the finished console program.

In order to "retain" this feature in your console program, you have to explicitly program it to print this text and wait for a key response...

And in contrast to what jj2007 suggested, use _getch (), it does exactly what you need...  ;)
Code: [Select]
#include "stdio.h"
#include "conio.h"

int main (void)
{
puts ("Hit any key to continue...");
_getch ();

return (0);
}

Ralf

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: using stdout for error messages in a console program
« Reply #3 on: October 09, 2013, 08:44:16 PM »
And in contrast to what jj2007 suggested, use _getch (), it does exactly what you need...  ;)
Ralf,
Thanks for reminding me. I found this:
What is the Difference between getch(),getche(),getchar() functions ?
Quote
getch() : Reads a character and never waits for Enter key.Just gets processed after getting any key pressed and it never echoes the character on screen which u pressed.

getche() : it works same as getch() but it echoes on screen.

getchar() : It works differently from the other two. Whenever you press any key these are kept in Buffer. After hitting enter the first character gets processed and it echoes on the screen
... which remarkably reminds me of my 68k phase (GEMDOS, 1985):
$01 CONIN
$08 CONIN WITHOUT ECHO
$0A READLINE
Who knows what's under the hood of getchar() ;-)

Slothrop

  • Guest
Re: using stdout for error messages in a console program
« Reply #4 on: October 09, 2013, 09:32:04 PM »
Thanks very much; that worked like a shot...

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: using stdout for error messages in a console program
« Reply #5 on: October 09, 2013, 10:15:08 PM »
Thanks very much; that worked like a shot...
Glad to hear that!  8)

Ralf