I have one file kristina.txt which holds data in the format below for one person with OCD, im supposed to open files from names the user enters for patients with OCD and then perform calculations on the number of times they do certain repetitions.
January 1 Monday brushed_teeth 9
January 1 Monday combed_hair 5
January 1 Monday stepped_up_bottom_step 20
From the first file comp.txt is used to display all possible compulsions for patients.
The code below prints all the text from comp.txt, but kristina.txt prints bogus numbers.
Also the section of code where the user enters the file name to open it i turned into a comment for now because it did not work, therefore i put the actual file name for now (kristina)
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char compulsions[30];
int patients=1;
char month[10], day[10],comp[30];
int date, reps;
printf("Welcome to Track Me");
FILE *ptr;
// open the file ".txt" for reading
ptr = fopen("comp.txt", "r");
if (ptr==NULL ){
printf("File could not be opened");
}
fscanf(ptr,"%s",compulsions);
printf("List of ALL 15 Compulsions\n");
while(!feof(ptr)){
printf("%s\n",compulsions);
fscanf(ptr,"%s",compulsions);
}
fclose(ptr);
//system("cls");
fflush;
//to search for patient file
//printf("Enter Patients Name to access file");
//scanf("%s",name);
FILE *search;
search = fopen("kristina.txt","r");
if (search==NULL ){
printf("File could not be opened");
}
fscanf(search,"%s,%d, %s, %s,%d",month,&date,day,comp,&reps);
while(!feof(search)){
char buffer[60];
fgets(buffer, 60, search);
printf("%s, %d, %s, %s, %d",buffer);
fscanf(search,"%s, %d, %s, %s, %d", month, &date, &day, comp, &reps);
}
fclose(search);
}