0

Line 12 to 23 runs. But doesn't actually run when the if statement is added. it does compile and runs. It asks the first printf statement then terminates when I choose a character. Why is this happening and how do I fix it.

#include <stdio.h>
#include <stdlib.h>

int main()
{
   char ch, file_name[25];
   FILE *fp;
   printf("Enter [A] and select file or [X] to exit:"); // Prompt user to select file or exit
   scanf("%c",&ch);
   scanf("%c",&ch);

   if (ch=='A')
   {
       printf("Enter the file name\n"); // if user chooses 'A' this code should run
       gets(file_name);

       fp = fopen(file_name,"r"); // reading file

       if( fp == NULL )
       {
           perror("File not found.\n");
           exit(EXIT_FAILURE);
        }
        printf("Contents of %s are:\n", file_name);

        while( ( ch = fgetc(fp) ) != EOF )
            printf("%c",ch);
    }
    else if (ch=='X')
    {
        printf("Exiting program...");
        exit(0);
    }
}
user3555512
  • 97
  • 1
  • 1
  • 6
  • Why should a compilable code actually work? A non-syntax error code doesn't mean that it doesn't have logic error – phuclv Apr 21 '14 at 07:45

3 Answers3

1

Because you have two calls to scanf..

In the first one, you are reading your input 'A' or 'X' successfully. In the next call, you are reading the newline character(\n) which was pressed earlier into the same variable ch. So it doesn't satisfy any if clause and simply comes out of program..

Instead make second call to temporary variable..

char temp;
scanf("%c", &temp);

Also fgets is preferred over gets

GoldRoger
  • 1,263
  • 7
  • 10
0

There are a large class of programs that compile yet don't run properly. That's why a distinction is made between syntax errors and runtime/logic errors.

scanf("%c",&ch);
scanf("%c",&ch);

I'm assuming that's to get rid of the newline character but it's a bad idea to read it into ch, since that should keep the first character.

If that is the case, simply read it into some junk variable so that ch is preserved.

char ch, junk, file_name[25];
:
scanf("%c",&ch);
scanf("%c",&junk);

Sadly though, there may be numerous other problems with this approach. If you want a decent line input function, you can find one here. That's much better than using gets(), which is inherently unsafe.

It has buffer overflow detection and prevention, automatic flushing of input lines where too long, prompt output and so on. Once you've used it to get an input line, all you need to do is compare that with what you want, something like:

if (strcmp (buff, "A") == 0)
    doSectionA();
else
    if (strcmp (buff, "X") == 0)
        doSectionX();
Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

I guess you are not reading properly from the file , Try:

char buff[255];  //define buffer to read lines

while ( !feof(fp ) ){
         memset(buff, '\0', sizeof( buff) );
         fgets(buff, 255, (FILE*)fp);
         printf("%s", buff );
      }

fclose(fp); //don't forget to close
chouaib
  • 2,763
  • 5
  • 20
  • 35