это только выборка b из билалхана
это потому, что break
останавливает ваш l oop while
Кажется, каждое поле только одним словом, включая адрес, в этом случае простое использование fscanf
решит вашу проблему, например:
#include <stdio.h>
#define MAX_LEN 256
int main()
{
FILE * fp = fopen("file.txt","r");
int option, rank;
char word[MAX_LEN];
static const char * const msg[] = {
"Name",
"Date of Birth",
"ID Card Number",
"Phone Number",
"Address",
"Account",
"Fixing Year",
"Amount"
};
if (fp == NULL) {
perror("cannot read file.txt");
return -1;
}
for (option = 1; option <= sizeof(msg)/sizeof(char *); ++option)
printf("%d. Your %s:\n", option, msg[option-1]);
fputs("Select your choice to update: ", stdout);
if ((scanf("%d", &option) != 1) ||
(option < 1) || (option > sizeof(msg)/sizeof(char *))) {
puts("invalid choice");
fclose(fp);
return -1;
}
/* the expected word is at rank option, other must be bypasses */
for (rank = 0; rank != option; ++rank) {
if (fscanf(fp, "%255s", word) != 1) {
puts("invalid input file");
fclose(fp);
return -1;
}
}
printf("Your %s: %s\n", msg[option - 1], word);
fclose(fp);
return 0;
}
Компиляция и выполнение:
pi@raspberrypi:/tmp $ gcc -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
cannot read file.txt: No such file or directory
pi@raspberrypi:/tmp $ echo "bilalkhan 02/02/1995 709734234 243322223 address current 2019 324324" > file.txt
pi@raspberrypi:/tmp $ ./a.out
1. Your Name:
2. Your Date of Birth:
3. Your ID Card Number:
4. Your Phone Number:
5. Your Address:
6. Your Account:
7. Your Fixing Year:
8. Your Amount:
Select your choice to update: 0
invalid choice
pi@raspberrypi:/tmp $ ./a.out
1. Your Name:
2. Your Date of Birth:
3. Your ID Card Number:
4. Your Phone Number:
5. Your Address:
6. Your Account:
7. Your Fixing Year:
8. Your Amount:
Select your choice to update: 9
invalid choice
pi@raspberrypi:/tmp $ ./a.out
1. Your Name:
2. Your Date of Birth:
3. Your ID Card Number:
4. Your Phone Number:
5. Your Address:
6. Your Account:
7. Your Fixing Year:
8. Your Amount:
Select your choice to update: 1
Your Name: bilalkhan
pi@raspberrypi:/tmp $ ./a.out
1. Your Name:
2. Your Date of Birth:
3. Your ID Card Number:
4. Your Phone Number:
5. Your Address:
6. Your Account:
7. Your Fixing Year:
8. Your Amount:
Select your choice to update: 2
Your Date of Birth: 02/02/1995
pi@raspberrypi:/tmp $ ./a.out
1. Your Name:
2. Your Date of Birth:
3. Your ID Card Number:
4. Your Phone Number:
5. Your Address:
6. Your Account:
7. Your Fixing Year:
8. Your Amount:
Select your choice to update: 8
Your Amount: 324324
pi@raspberrypi:/tmp $