Computing.Net > Forums > Programming > Read-in single char from stdin in C

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Read-in single char from stdin in C

Reply to Message Icon

Name: Wale A.
Date: November 12, 2003 at 16:46:53 Pacific
OS: Windows 2000 Pro
CPU/Ram: celeron/256
Comment:

This has been really bothering me, can someone pls help me out. I am trying to read-in a single character from stdin in my C program, so it ignores the '\n'. How do i go about it?? Help pls

thanx



Sponsored Link
Ads by Google

Response Number 1
Name: Ronin1
Date: November 12, 2003 at 18:58:55 Pacific
Reply:

getchar(); ?
getc(stdin); ?

Post your code, so others can see what it is that you're trying to do.


0

Response Number 2
Name: Wale A.
Date: November 12, 2003 at 19:09:06 Pacific
Reply:

for(;;){
menu();
scanf("%c", &choice);

switch(choice){
case '1': printf("Read File\n");
printf("Enter the name of input file: ");
scanf("%s", &filename);
infile = fopen(filename,"r");
ch = fgetc(infile);
while(ch != EOF){
printf("%c", ch);
ch = fgetc(infile);
}
fclose(infile);
break;
case '2': printf("Insertion\n");
break;
case '3': printf("Shell\n");
break;
case '4': printf("Print List\n");
break;
case '5': printf("Done!\n");
exit (0);
break;
default: printf("ERROR");
}
}

That is my code, problem is everytime i read in something, the menu shos up twice, supposedly because its reading the "return" with it.


0

Response Number 3
Name: Ronin1
Date: November 12, 2003 at 19:28:17 Pacific
Reply:

filename is a character array yes? You don't use the address operator with the scanf statement. A better choice in place of scanf is fgets IMO.

filename most likely would pick up the newline left in the buffer from choice, so either use something like getchar() after reading choice or perhaps scanf("%c%*c", &choice).

I'm not sure what you're doing with the file access part, but you should be testing it before trying to use it.

if((file_ptr = fopen(fname, "f_attrib")) != NULL) /* use the file */

your default *should* also have a break statement.

HTH


0

Response Number 4
Name: Gagey
Date: November 12, 2003 at 21:42:09 Pacific
Reply:

U only want to use scanf when u expect different data types in a predefined format ie, and int followed by a string. And the scan.. functions always read an entire line (or scan upto the first \n char)

getc(stdin) or getchar() don't.

When reading string input I find fgets the best lib function.

proto:
char *fgets(char *buf, int buf_size, FILE *input_device) ;

read (at most) 'buf_size' chars from 'input_device' into 'buf' and return 'buf'.

ie:
#define MAX_INPUT 256
#include <stdio.h>

char input[MAX_INPUT+1] ;
if ( fgets(input, MAX_INPUT, stdin) != NULL ) {

/* input array contains valid string */
}

********** Notice that the array is declared to be size 1 larger than the buf_size value passed to the fgets function: U need 1 extra char for the null terminator. This is why fgets is superior to gets, NO overruns or seg faults!

the other difference is that fgets retains the newline char at the end while gets trims it away.

U can trim manually using:
input[ strlen(input) - 1 ] = NULL ;
or
*(input + strlen(input) - 1) = NULL ;

depending on whether u like array or pointer notation (both compile to same code)


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


WSH vs VBS? SSL & ON Line payment...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Read-in single char from stdin in C

File I/O in C www.computing.net/answers/programming/file-io-in-c/13329.html

read file from subroutine in c++ www.computing.net/answers/programming/read-file-from-subroutine-in-c/3714.html

Reading ctrl-D from cin in C++ www.computing.net/answers/programming/reading-ctrld-from-cin-in-c/3430.html