can someone help me? i am new to C, i meed to read file which looks like this
Month Date Day Activity Repetitions
January 1 Monday brushed_teeth 9
January 1 Monday combed_hair 5
Then i need to do calculations in the reps and month
can someone help me? i am new to C, i meed to read file which looks like this
Month Date Day Activity Repetitions
January 1 Monday brushed_teeth 9
January 1 Monday combed_hair 5
Then i need to do calculations in the reps and month
Correct me if I'm wrong but shouldn't you use fscanf or fgetc since he needs to read a file?
Hey thanks for the replies. I got my file to read, however, i need to ask the user for the month and compare it to the month in the file if they are not the same exit the program
#define txt 200
char month[txt];
char date[txt];
char day[txt];
char activity[txt];
char reps1[txt];
FILE *ptr;
char fileName[30];
char Month[txt];//used to get month for nurse
int result;
//////row 2////////////
int date1[txt];//get date from file
int reps[txt];// get repetitions from file
char month1[txt]; //get month from file
char day1[txt];//get day from file
char activ1[txt];// ativitity from file
int all= 100;
int i;
printf("Please enter file Name (filename.txt):\n" );
scanf("%s", fileName);
////////////////////////////
//printf("please enter month");
//scanf("%s", Month);
//if(strcmp(month1, month) ==1)
//{printf("the same");}
printf( "Enter Month: " );
scanf( "%s", Month );
if (strcmp(month1, Month) !=0)
{printf("%s", month1);}
//////////////////////////
ptr = fopen(fileName,"r");
if (ptr != NULL)
{
printf("\n");
printf("\tPatient summary\n");
fscanf(ptr,"%s %s %s %s %s", month, date, day, activity, reps1);
printf ("%s %s %s %s %s\n", month, date, day, activity, reps1);
while ((!feof (ptr)) && (i<all))
{
fscanf(ptr, "%s %d %s %s %d", month1, &date1[i], day1, activ1, &reps[i]);
printf("%s %d %s %s %d\n", month1, date1[i], day1, activ1, reps[i]);
i++;
fclose;
}
}
else
{
printf("File not found");
}
}
You should get the input from the user and then run a loop, which reads the file and gets you the month for each entry. Then, compare the two. Use
if(strcmp(string1, string2)==0)
to check if the two strings are the same.
In your commented code, you have checked the value returned by strcmp to be equal to 1, which makes a little sense. See usage of strcmp().
Also in such cases, it is better to use Random-access Files. Learn about them.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.