Learning C by Example (2015)
5. I/O Operations
This chapter explains how to work with I/O operations.
5.1 Getting Started
In this section, we learn how to work with I/O operation. The following is our scenario to illustrate how deal with I/O using C:
· Reading input from keyboard
· Reading program arguments
· Writing data into a file
· Reading data from a file
5.2 Reading Input from Keyboard
We can read input from keyboard in many ways. The following is a list of function for reading input from keyboard:
· getchar()
· gets()
· scanf()
·
We will implement theses functions on next section.
5.2.1 getchar() and putchar() functions
getchar() function is used to get a character from keyboard and putchar() function is used to print a character to Terminal.
For illustration, create a file, called keyboard.c, and write this code.
#include <stdio.h>
void getchar_putchar();
int main() {
getchar_putchar();
return 0;
}
void getchar_putchar(){
int c;
printf ("Type a character: ");
c = getchar();
printf ("char: %c\n",c);
putchar(c);
printf("\n");
}
Compile and run this program.
$ gcc -o keyboard keyboard.c
$ ./keyboard
You can see the program output in the following Figure below.
5.2.2 gets() and puts() functions
gets() is used to read a text. It will stop to read if you type newline (ENTER key). Unfortunately, gets() is unsafe function(). We can use fgets() to read text with length limitation.
For testing, we add get_puts() function on keyboard.c file, and write this code.
#include <stdio.h>
void gets_puts();
int main() {
gets_puts();
return 0;
}
void gets_puts(){
printf("-----unsafe input-----\n");
char name[256];
printf ("Your name: ");
gets (name);
printf ("name: %s\n",name);
puts(name);
printf("-----fget() input-----\n");
name[0] = '\0'; // clear
printf ("Your name: ");
fgets(name,256,stdin);
printf ("name: %s\n",name);
puts(name);
}
Save this code. Try to compile and run this program.
A sample output can be seen in Figure below.
5.2.3 scanf() function
Another approach, we can use scanf() function to read a text.
For illustration, we add scanf() function on keyboard.c file, and write this code.
#include <stdio.h>
void scanf_demo();
int main() {
scanf_demo();
return 0;
}
void scanf_demo(){
int num;
char c;
char city[15];
float dec;
printf("Please enter an integer value: ");
scanf("%d", &num );
// %c ignores space characters
printf("Please enter a character: ");
scanf(" %c", &c );
printf("Please enter a city name (no space): ");
scanf("%s", city );
printf("Please enter a decimal value: ");
scanf("%f", &dec );
printf("\n-----result-------\n");
printf("number = %d\n", num );
printf("character = %c\n", c );
printf("city name = %s\n", city );
printf("decimal number = %f\n", dec );
}
Save this code. Compile and run this program
5.3 Reading Program Arguments
We can read arguments and its length from our program. This information already pass to main() function.
For illustration, create a file, called argument.c, and write this code.
#include <stdio.h>
int main(int argc, const char* argv[]) {
int i;
printf("total argument: %d\n",argc-1);
if(argc>1){
for(i=1;i<argc;i++){
printf("%s\n",argv[i]);
}
}
return 0;
}
Compile and run this program.
$ gcc -o argument argument.c
$ ./argument
A sample output can be seen in Figure below.
5.4 Writing Data Into A File
We can write data into a file. The following is the algorithm for writing data:
· Create a file using fopen() function
· Write data into a file using fprintf() and fputs() functions
· Close a file using fclose() function
For testing, we create data into a file, demo.txt. For implementation, create a file, called filewrite.c, and write this code.
#include <stdio.h>
int main(int argc, const char* argv[]) {
int i;
FILE *f;
f = fopen("demo.txt", "w+");
for(i=0;i<5;i++){
fprintf(f, "fprintf message %d\n",i);
fputs("fputs message\n", f); // no format
}
fclose(f);
printf("Data was written into a file\n");
return 0;
}
Save this code. Compile and run this program.
$ gcc -o filewrite filewrite.c
$ ./filewrite
A sample output can be seen in Figure below.
If success, it will generate demo.txt. You can open this file using text editor.
5.5 Reading Data From A File
We also can read data from a file using fgetc() function. For testing, create file, called fileread.c, and write this code.
#include <stdio.h>
int main(int argc, const char* argv[]) {
char ch;
FILE *f;
printf("Reading a file....\n");
f = fopen("demo.txt", "r");
if(f==NULL){
printf("Failed to read file\n");
return 0;
}
while((ch = fgetc(f)) != EOF )
printf("%c",ch);
fclose(f);
return 0;
}
Compile and this program.
$ gcc -o fileread fileread.c
$ ./fileread
A sample output can be seen in Figure below.