samedi 28 février 2015

How to Read and Compare string in a file then get the number/s beside it in C



This is the Algorithm:



  1. Open the file.

  2. Read a string from the file.

  3. Compare the string.

    • If the string matches, get the number/s after '=', then exit the loop.



  4. Check the end of the file.

    • If it's not yet the end of the file, go back to step 2.

    • If the end of the file is met exit the loop, and go to step 5.



  5. The string is not in the file.


This is the program i made:



FILE *fp = fopen("Converter.txt", "r");

while( fgets(line, sizeof(line), fp) != NULL ){
if(strcmp(line,a)){
printf("There is such file");
sscanf(line,"%*[^=]=%f", &num);
printf("\n\n%.2f",num);
}else{
printf("NULL\n");
continue;
}break;
}


The problem is it only scan the first line and it doesn't scan the number beside the string that it compares.


Thanks.




Aucun commentaire:

Enregistrer un commentaire