Reading strings with fgets
From Initq
These little programs will show you how to read input from the console and save it into character arrys. We are also going to use a few string functions such as getc, getchar, putc, putchar, strcpy, strcat and strlen.
Contents |
Understanding Standard I/O
- stdin
- The standard input for reading
- stdout
- The standard output for writing
- stderr
- The standard error for writing error messages.
Get one character from user
Write a program that gets one character from standard input and then prints it on the standard output.
#include <stdio.h> char input; main () { printf("Enter in one characher: "); input = getc(stdin); printf("The character you entered is %c\n", input); return (0); }
Using the getchar() function
Write a program that uses both getc and getchar. Get two characters from the standard input and then display both characters on the standard output using printf. Save the characters in variables.
#include <stdio.h> char input; char input2; main () { printf("Enter in two charachers: "); input = getc(stdin); input2 = getchar(); printf("The character you entered is %c\n", input); printf("The second character you entered is %c\n", input2); return (0); }
Output
Enter in two charachers: QA The character you entered is Q The second character you entered is A
Printing output with putc
Write a program that gets a character from the standard input and then displays the character on the standard output using putc.
#include <stdio.h> char input1; main() { printf("Please enter a char: "); input1 = getchar(); printf("you entered :"); putc(input1, stdout); return (0); }
Printing output with putchar
Write a program that reads a character from the standard input and then displays the character on the standard output using putchar.
#include <stdio.h> char input1; main() { printf("Please enter a char: "); input1 = getchar(); printf("you entered :"); putchar(input1); return (0); }
fgets example
Write a program that takes a string as an input and then displays the length of the string and displays the string on the standard output.
#include <stdio.h> #include <string.h> char line[100]; /* character arrys that contains 100 characters */ main () { printf("Enter some text: "); fgets(line, sizeof(line), stdin); /* fgets takes 3 parameters */ printf("the length of line is %d\n", strlen(line)); printf("you entered: \n%s\n",line); return(0); }
fgets with file
Another example that reads from a file.
#include <stdio.h> int main() { FILE * pFile; char mystring [100]; pFile = fopen ("myfile.txt" , "r"); if (pFile == NULL) perror ("Error opening file"); else { fgets (mystring , 100 , pFile); puts (mystring); fclose (pFile); } return 0; }
Parameters for fgets
- str
- Pointer to an array of chars where the string read is stored.
- num
- Maximum number of characters to be read (including the final null-character). Usually, the length of the array passed as str is used.
- stream
- Pointer to a FILE object that identifies the stream where characters are read from.
To read from the standard input, stdin can be used for this parameter.
Output aligning with printf
Write a program that shows how to left or right align numbers using printf.
#include <stdio.h> int num1, num2, num3, num4, num5; main() { num1= 1; num2= 12; num3= 123; num4= 1234; num5= 12345; printf("%8d %-8d\n", num1, num1); printf("%8d %-8d\n", num2, num2); printf("%8d %-8d\n", num3, num3); printf("%8d %-8d\n", num4, num4); printf("%8d %-8d\n", num5, num5); return (0); }
Output
1 1
12 12
123 123
1234 1234
12345 12345Playing with Strings
Let's write a program that asks for the first and last name and then displays it on the standard output.
#include <stdio.h> #include <string.h> char first[100]; char last[100]; char full[200]; main () { printf("Please enter your first name: "); fgets(first, sizeof(first), stdin); printf("Please enter your last name: "); fgets(last, sizeof(first), stdin); strcpy(full, first); strcat(full, " "); strcat(full, last); printf("Your full name is %s.\n",full); return (0); }
Output
Please enter your first name: qais Please enter your last name: chaudry Your full name is qais chaudry .
As you can see from the above out that we have a problem. The name is not on the same line and the full stop is also from down. So lets fix this issue. The reason we are getting a new line is because when we finish typing the first name we hit enter and that is also stored in the string. We need to get rid of that and replace it with NULL. We can do this by (first)-1='\0'. The statement will look like
first[strlen(first)-1]='\0';
fgets but without new-line
We need to do this for both, first name and last name so our code will look like this:
#include <stdio.h> #include <string.h> char first[100]; char last[100]; char full[200]; main () { printf("Please enter your first name: "); fgets(first, sizeof(first), stdin); printf("Please enter your last name: "); fgets(last, sizeof(first), stdin); first[strlen(first)-1]='\0'; strcpy(full, first); strcat(full, " "); last[strlen(last)-1]='\0'; strcat(full, last); printf("Your full name is %s.\n",full); return (0); }
Output on one line
Please enter your first name: qais Please enter your last name: chaudry Your full name is qais chaudry.
Multiple Dimensional Arrays
Arrays can be more than one dimension. It is declared
- int matrix[2][3]; /*a typical matrix*/
C allows the programmer to use as many dimensions as needed such as four_dimension[10][12][9][5];