Professional Documents
Culture Documents
Chapter09 File
Chapter09 File
File
… EOF
1 2 3 N
Binary file
Created by some programming, Human cannot read and
understand directly by NOTEPAD
After open the file successfully, the marker will point to the first byte of the file
CH = Read a character
false
CH <> EOF
true
Save CH to buffer
CH = Read a character
"test.txt”: the program tries to find this file in the recent folder
which contains the cpp files, or in the directory of the PATH.
If we use the path, please use “\\” in stead of “\”; Because “\” is a
control character in string.
Mode Description
r Only for reading
w For writing. If the file exists, the content will be removed.
a Open the existence file or create a new one, write the data at the end
of the file if it existed.
r+ Open the existence file to write or read. Do not create a new one if the
file does not exists.
w+ Open the existence file to write or read. Create a new one if it does not
exists.
a+ Open the existence file or create a new one, write the data at the end
of the file if it existed. Create a new one if it does not exists.
int ch = fgetc(file_ptr);
while(ch != EOF){
buffer[index] = ch;
ch = fgetc(file_ptr);
index += 1; CH = Read a character
}
buffer[index] = '\0'; CH <> EOF
false
}
true
fgetc: read a character Add CH to the buffer
CH = read a character
int ch = fgetc(file_ptr);
while(ch != EOF){
if(index < MAX_LEN - 1){
buffer[index] = ch;
ch = fgetc(file_ptr);
index += 1;
}
else break;
}
• Index lager than the size of
buffer[index] = '\0';
buffer: break
}
In the case we want to check the • Add the end index for the buffer
size of the buffer
character.
fclose(file_ptr);
printf("%s", buffer);
Problem analysis:
The content in a row: Name, 3 grade columns (real number). The
width of each column is fixed
fprintf function have the same properties of printf (apply to write
the data to the file) can use.
Problem analysis :
Define the struct data type to manage the information of a student
typedef struct{
char name[20];
float math, physics, english;
} Student;
Problem analysis s:
Declare the list of students:
Student list[] = {
{"Nguyen Van A", 9.8f, 7.2f, 9.5f},
{"Tran Van B", 4.0f, 5.3f, 2.5f},
{"Phan Dinh C", 8.7f, 7.9f, 8.1f},
};
Problem analysis:
The main idea: scan each element in the list and write down to the
file.
typedef struct{
char name[20];
float math, physics, english;
} Student;
int main(){
Student list[] = {
{"Nguyen Van A", 9.8f, 7.2f, 9.5f},
{"Tran Van B", 4.0f, 5.3f, 2.5f},
{"Phan Dinh C", 8.7f, 7.9f, 8.1f},
};
fclose(file_ptr);
system("pause");
return EXIT_SUCCESS;
}
int main(){
Student list[100];
int size = 0;
system("pause");
return EXIT_SUCCESS;
}
typedef struct{
char name[20];
float math, physics, english;
} Student;
doc_tiep = xoa_xuong_hang(file_ptr);
size += 1;
if(!doc_tiep) break;
}//while
return size;
Reading progress
}
Trường Đại Học Bách Khoa Lập trình C/C++
Trung Tâm Kỹ Thuật Điện Toán 35
© 2016
Formatted text file: read
Operations
V.v
file is changed)
With binary file:
We do not know the length of the file
int main(){
Student list[MAX_SIZE];
int size;
size = sinh_du_lieu_mau(list);
ghi_du_lieu(list, size, "stu_list.data");
in_du_lieu(list, size);
printf("\n\n");
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE 100
typedef struct{
char name[20];
float math, physics, english;
} Student;
Open file
Read profiles
int c= 0;
int num = 0;
while(true){
num = fread(&list[c], sizeof(Student), 1, file_ptr);
if(num != 1){
break;
}
c++;
}
*size = c;
Close file
fclose(file_ptr);