c - Saving data from a file into arrays -
i trying scan in information:
00abcabc:abc123 01defdef:def456 02hijhij:hij789
into 2 arrays using code:
file *datalogin; int i=0, numrecords; char username[100][10], password[100][10]; datalogin = fopen("login.dat", "r"); if (datalogin == null){printf("error");} else { while (fscanf(datalogin, "%s:%s\n", username[i], password[i])){i++;} fclose(datalogin); numrecords = i; for(i = 0; < numrecords; i++){printf("%s, %s\n", username[i], password[i]);} } printf("complete");
the program compiles , runs doesn't display anything. believe have isolated fault while loop stuck there. thanks!
try with:
while (fscanf(datalogin, "%[^:]:%s\n", username[i], password[i]) == 2){i++;}
scanf returns number of field red, 2 until end of file. , need change "%s:%s\n"
"%[^:]:%s\n"
.
Comments
Post a Comment