0

I am trying to scan in this information:

00abcabc:abc123
01defdef:def456
02hijhij:hij789

into two arrays using this 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; i < numRecords; i++){printf("%s, %s\n", username[i], password[i]);}

    }

printf("complete");

The program compiles and runs but doesn't display anything. I believe I have isolated the fault to the while loop but I am stuck from there. Thanks!

3
  • 1
    Try checking the return from fscanf Commented Jan 31, 2014 at 18:46
  • @Dmitri How would one go about this? Commented Jan 31, 2014 at 18:47
  • Change your while condition to verify that it returns 2, eg. (fscanf(...) == 2) rather than just (fscanf(...)). That'll only succeed if you read 2 strings. Commented Jan 31, 2014 at 18:52

4 Answers 4

1

It appears you made the assumption that fscanf will return 0 on a failure. This appears to be incorrect, please check some documentation such as here. Search for the keyword return on the webpage.

It turns out you should run the loop while fscanf does not return -1. That should fix the problem.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem lies with:

fscanf(dataLogin, "%s:%s\n", username[i], password[i])

%s:%s will not divide 00abcabc:abc123 in the two strings you want. From the man pages of fscanf, for %s: "The input string stops at white space or at the maximum field width, whichever occurs first." This means that the first %s will read the whole line: 00abcabc:abc123 and the second will read the second line etc. You need to find another way to divide each line in two strings.

edit: Leonardo's answer is indeed the reason your while loop doesn't terminate, but you will encounter this problem here even if you fix it

Comments

1

//if it is fixed that the first part(A) in input file(A:B) is of length 8 and second part(B) is of length 7 then you can use the following changed code. Using feof() will also solve the problem of detecting end of file..

#include <stdio.h>

int main()
{
  FILE *dataLogin;
  char ch;
  int i=0, numRecords;
  char username[100][10]={'\0'}, password[100][10]={'\0'};
  dataLogin = fopen("login.dat", "r");
  if (dataLogin == NULL){
    printf("Error");
  } else {
    while (!feof(dataLogin)){fscanf(dataLogin, "%8s:%7s", username[i], password[i]);i++; 
  }
  fclose(dataLogin);
  numRecords = i;
  for(i = 0; i < numRecords; i++){
    printf("%s, %s\n", username[i], password[i]);
  }
}

printf("complete");
}

Comments

0

Try with:

while (fscanf(dataLogin, "%[^:]:%s\n", username[i], password[i]) == 2){i++;}

scanf returns the number of field it red, so it will be 2 until the end of the file. And you also need to change the "%s:%s\n" to "%[^:]:%s\n".

1 Comment

Thank you everyone! I ended up using the code from @fede1024.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.