12345678910111213141516171819202122
#include <stdio.h> #include <stdlib.h> int main() { int money; char words[8] = {0}; FILE *fptr; if((fptr = fopen("credicash.txt", "r")) == NULL) { perror("Error! opening file"); exit(1); } while(EOF != fscanf(fptr, "%s %d", words, &money)) { printf("%s \n%d \n", words, money); //calculation here. } }
1234567891011121314151617181920212223242526272829303132
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char line[500]; double cash = 0, credits = 0; FILE *fin = fopen("credicash.txt", "r"); if (fin == NULL) { perror("fopen"); exit(EXIT_FAILURE); } fscanf(fin, "%s", line); // read "Credit:" // Sum the credits while (fscanf(fin, "%s", line) == 1) { if (strcmp(line, "Cash:") == 0) // stop when we find "Cash:" break; credits += atof(line); } // Sum the cash while (fscanf(fin, "%s", line) == 1) cash += atof(line); printf("Credits: %9.2f\n", credits); printf("Cash : %9.2f\n", cash); return 0; }