-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile_processing.cpp
65 lines (45 loc) · 1.4 KB
/
File_processing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
FILE *fp;
struct Orders{
int quantity;
char makanan[255];
int hargaSatuan;
int total;
};
int main(){
// buka file
fp = fopen("testdata.txt", "r");
char string[255];
// fscanf utk read file
fscanf(fp, "%[^\n]\n", string); // \n di akhiran buat baca enter, (membaca Hello World)
printf("%s\n", string);
fscanf(fp, "%[^\n]\n", string); // \n di akhiran buat baca enter, (membaca This is Binus University
printf("%s\n", string);
/* Klo mw baca smpe akhir file pke looping
while(!feof(fp)){
fscanf(fp, "%[^\n]\n", string); // \n di akhiran buat baca enter, (membaca This is Binus University
printf("%s\n", string);
}
*/
fclose(fp);
// klo udh close, variable FILE *fp bisa dipake buat akses file lain
struct Orders ord[100];
fp = fopen("transaksiRestoran.txt", "r");
int i = 0;
while(!feof(fp)){
fscanf(fp, "%d-%[^\-]-%d\n", &ord[i].quantity, &ord[i].makanan, &ord[i].hargaSatuan); // \n utk pindah line
ord[i].total = ord[i].quantity * ord[i].hargaSatuan;
i++;
}
for(int j = 0; j < i; j++){
printf("%-3d | %-20s | %-10d | %-10d\n", ord[j].quantity, ord[j].makanan, ord[j].hargaSatuan, ord[j].total); //-3 itu biar rapi
}
fclose(fp);
return 0;
}
/*
Mode dalam fopen (yang bisa dipake di oj cuma read data)
1. Read = r
2. Write = w // jika sudah ada data, data yg lama akan di replace dengan data baru (overwrite)
3. Append = a // menambahkan data
*/