i cant read my text file.......

i can read my first customer detail,others cant...How me solve please,i'm still new...

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
void main() {
system("color 8a");

SYSTEMTIME t;
int choice, txnCode;


/*TARBANK Logo
printf("\n\ ========== __ ~~~~> ~~~~>\n");
printf("\n\||===||===|| / \ | @@ > | @@ >\n");
printf("\n\ || / /_ \ | @ > | >\n");
printf("\n\ || / /__\ \ | << | <<\n");
printf("\n\ || / / \ \ | \ \ | @@ >\n");
printf("\n\ || /_/ \_\ | \ \ |___ >\n");
*/	

//Display Today's Date and Time
GetLocalTime(&t);
printf("\t\tDate! Today is %d/%d/%d and \n\
it's %d:%d:%d now.\n\n", t.wDay, t.wMonth, t.wYear, t.wHour, t.wMinute, t.wSecond);

printf("\t\tCustomer:Press 1\n");
printf("\t\tManager:Press 2\n\n");
printf("\t\tYour choice: ");
scanf("%d", &choice);
if(choice == 1) {
fptr1 = fopen("Customer.txt", "r");
fptr2 = fopen("Customer_temp.txt", "w");

//Check if customer.txt can be opened or not
if(fptr1 == NULL || fptr2 == NULL) {
printf("ERROR in opening file!Try again.....");
exit(-1);
}

//Randomly generate ATM No.(6-10)
srand(time(NULL));
atm = (rand() % 5) + 6;
//Display ATM No.
printf("ATM No: %d\n", atm);

//Prompt and read user's A/C No.
printf("\t\tPlease enter your A/C No:\t\t_____\b\b\b\b\b");
scanf("%s", &temp.accNo);
fflush(stdin);

//Prompt and read user's PIN No.
printf("\t\tPlease enter your PIN No:\t\t_____\b\b\b\b\b");
scanf("%s",temp.PIN);
fflush(stdin);

//Read customer.txt
while(!feof(fptr1)) {
for(int i = 0; i < 10; i++) {
fscanf(fptr1, "%[^|]|%[^|]|%[^|]|%c|%[^|]|%[^|]|%[^|]|%d|%d%d%d%d%d%d", 
&customer[i].accNo, &customer[i].PIN, &customer[i].name, &customer[i].gender, 
&customer[i].address, &customer[i].state, &customer[i].contact, 
&customer[i].balance, &customer[i].lastTxn.date, &customer[i].lastTxn.month, &customer[i].lastTxn.year, 
&customer[i].lastTxn.hr, &customer[i].lastTxn.min, &customer[i].lastTxn.sec);

//Check if the results match(update is pending)
if(strcmp(temp.accNo, customer[i].accNo) == 0 && strcmp(temp.PIN, customer[i].PIN) == 0) {
customerCode = i;

strcpy(temp.name, customer[i].name);
temp.balance = customer[i].balance;

temp.gender = customer[i].gender;
strcpy(temp.address, customer[i].address);
strcpy(temp.state, customer[i].state);
strcpy(temp.contact, customer[i].contact);
temp.lastTxn.year = customer[i].lastTxn.year;
temp.lastTxn.month = customer[i].lastTxn.month;
temp.lastTxn.date = customer[i].lastTxn.date;
temp.lastTxn.hr = customer[i].lastTxn.hr;
temp.lastTxn.min = customer[i].lastTxn.min;
temp.lastTxn.sec = customer[i].lastTxn.sec;
}
}
}

printf("\n");
printf("Name: %s\n\n", temp.name);
printf("Balance: RM%d\n\n", temp.balance);

printf("Last Transaction Date: %d/%d/%d %d:%d:%d\n\n", temp.lastTxn.date, temp.lastTxn.month, temp.lastTxn.year, 
temp.lastTxn.hr, temp.lastTxn.min, temp.lastTxn.sec);



here is the customer.txt~
12345|54321|Tao Ka Cheng |M|12 Jalan 3/45 KL |Wilayah P|012-3456789|5000| 3 10 2015 2 30 00
12350|05321|Lim Ke Yuan |F|13 Jln 5/50 Gtown |Penang |013-4567890|4000| 4 10 2015 4 30 00
12355|55321|Lin Lao ba |F|14 Jalan 7/15 KK |Sabah |014-5678901|2000| 4 10 2015 2 00 00
12360|06321|Chau Ah Gua |M|15 Jalan 2/25 Ipoh |Perak |015-6789012|3000| 5 10 2015 4 30 00
12365|56321|Marempit Chong|M|16 Jalan 1/34 Muar |Johor |016-7890123|5000| 7 10 2015 8 30 00
12370|53344|Kang Mei Mei |F|17 Jalan 6/45 SP |Sungai P |012-1234489|6000| 6 10 2015 2 30 00
12375|02543|Lu Ka Jik |M|18 Jln 9/50 Gtown |Penang |013-5456790|8000| 8 10 2015 4 30 00
12380|08333|Wong Ji Ji |F|19 Jalan 1/60 KK |Sabah |014-5754301|1000| 12 10 2015 2 10 00
12385|01232|Chau JohnCena |M|20 Jalan 45/65 Ipoh|Perak |015-6778912|4500| 17 10 2015 4 40 00
12390|33452|Jessyna Lai |F|21 Jalan 12/34 Muar|Johor |016-1232023|7000| 27 10 2015 8 50 00
Last edited on
This loop is odd:
1
2
3
4
5
6
7
8
    //Read customer.txt
    while(!feof(fptr1)) {
        for(int i = 0; i < 10; i++) {
            fscanf(fptr1, "%[^|]|%[^|]|%[^|]|%c|%[^|]|%[^|]|%[^|]|%d|%d%d%d%d%d%d", 
                &customer[i].accNo, &customer[i].PIN, &customer[i].name, &customer[i].gender, 
                &customer[i].address, &customer[i].state, &customer[i].contact, 
                &customer[i].balance, &customer[i].lastTxn.date, &customer[i].lastTxn.month, &customer[i].lastTxn.year, 
                &customer[i].lastTxn.hr, &customer[i].lastTxn.min, &customer[i].lastTxn.sec);


You read 10 lines from the file, then do something with it, and keep repeating until that file is empty.

We would not expect that kind of double loop to process a file. I would expect you to read those files into collections and process them somehow, But that's not what you're doing.

BTW. I haven't checked the scanf format string. I assume it's correct.
Last edited on
You also forgot to declare a bunch of variables. First of all you should declare your file streams (fptr1 and fptr2) to have the type FILE*. Then you forgot to declare the variable atm, it should probably have the type int. Last of all, the variable customer and temp need to be declared. You seem to be using fields of some custom struct type, so you should define this type, and declare the variables to be of this type.

Second of all, you forgot a few includes. Looking at your current code, you will probably need to:
1
2
3
4
5
#include <cstdlib> //rand, srand
#include <cstdio> //printf, scanf
#include <cstring> //strcmp, strcpy
#include <ctime> //time
#include <windows.h> //GetLocalTime, SYSTEMTIME 


I also find it odd that you first use the windows API to get the current time, then later use the C time functions to seed your random number engine. I would probably stick with one of them and not use both, but that's a matter of preference.

Last but not least, the classic, common and dreadful declaration of void main(). The type of the main function is int. So your declaration should be int main(). The standard defines two ways to define the main function:

1
2
int main()
int main(int, char*[])


And some systems define additional parameters. void main() is explicitly disallowed and you should not use it, whether or not your compiler accepts it. Most compilers complain about it if you enable warnings though (which you probably should, it saves a lot of bugs).
Last edited on
I haven't tested it, but IIRC calls succeeding the first to fscanf will result in a newline character being the first extracted into customer[i].accNo, which probably isn't intended.
Topic archived. No new replies allowed.