CLang-Lect14-en-v2
CLang-Lect14-en-v2
2
Standard input/output streams
3
Example
$input
Input.c
10
Input number10
#include <stdio.h>
$input
void main()
{
abc
int a;
This is not integer
if ( scanf("%d", &a) != 1 ) $input >out.txt
perror(“This is not integer\n”); 10
else $input >out.txt
printf(“Input number%d", a); abc
} This is not integer
Redirect stdout
to file out.txt
4
Input/output file
5
Example
#include <stdio.h>
Open file to
int main() write
{
FILE * out = fopen(“hello.txt”, “w”);
if (out == NULL)
{
perror(“Unable to write to the file.\n”);
return 1;
} Write data to
file
fprintf(out, “Hello world”);
fclose(out);
return 0;
Close file when
}
terminate
6
Modes in open file
• r: read
• w: write
• a: append
• r+: read/write on a new file if not exist
• w+: write on a new file if not exist
• a+: append on a new file if not exist
7
fprintf() và printf()
8
fscanf() and scanf()
9
Input data
• Input number following formats %d, %l, %x,…, will skip spaces and
• %s scans a string not including spaces and .
• %c scans any character at the pointer’s position (including spaces and )
• Example, if we enter “12 ab”
• "%d%s" gives us a number 12 and a string “ab”
• "%d%c%s" gives us a number 12, a space and a string “ab”
• "%d %c%s" gives us a number 12, a character a and a string “b”
• "%s%s" gives us two strings “12” and “ab”
• "%d%s%c" give us a number 12, a string “ab” and a character
11
fflush()
12
Example
Input.c
#include <stdio.h> C:\>input
Input a number: 12
void main() Input a string: ab
{
number 12, string ab
int a;
char s[20];
printf(“Input a number: ”);
scanf(“%d”, &a);
13
Calculate total words of a file
#include <stdio.h>
int main()
{
Open file to
int count = 0;
char s[80];
read
FILE * f = fopen(“text.txt”, “r”);
if (f == NULL)
{
perror(“Failure when opening text file.txt\n”);
return 1;
}
while (!feof(f))
Read a word
dem += fscanf(f, “%s”, s);
each time
fclose(f);
printf(“Total number of words: %d”, dem);
return 0;
}
14
fgetc() and fputc()
ch = fgetc( input );
while( ch != EOF ) {
fputc( ch, output );
ch = fgetc( input );
}
fclose(input);
fclose(output);
fgets()
#include <stdio.h>
#define LINE_LENGTH 80
main()
{
FILE* fp;
char line[LINE_LENGTH];
int count=0;
fp=fopen("input.txt","r");
while ( fgets(line, LINE_LENGTH, fp) != NULL)
count++;
printf("File contains %d lines.\n", count);
fclose(fp);
}
Text file vs. binary file
17
Input/ouput in binary mode
size_t fread(void* buf, size_t size,
size_t num, FILE* f);
size_t fwrite(void* buf, size_t size,
size_t num, FILE* f);
• Read and write data in the memory with the pointer buf, with the total elements num, size of each
element size
Example:
int a[10];
f=fopen("integer.dat", "r+b");
fread(a, 10, sizeof(int), f);
18
Exercises
1. Write a program to create a text file F3 by concatenate two text files F1 and F2
F1 = “ha noi”; F2 = “ viet nam” F3 = “ha noi viet nam”
2. Write a program to remove all comments from a C program which is stored in a file. The name of
the file is entered from the keyboard. Assume that the program does not have syntax errors.
3. Assume that a data file consisting information about weather in a year has the format for each
line as follow: \
<day>/<month> <lowest temperature>-<highest temperature> <humidity>
1/1 11-17 70
2/1 12-17 75
…
4. Write a program read data from this file and print the average temperature of all months in a
year, the most humid month and the dryest month.
19
THANK YOU !
20