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

Recommended Answers

All 5 Replies

Try using fprintf. You need to start with some code. Here, we can provide help after you have made an effort.

Post what you have already written(if you have) or else, read about FILE I/O in C, and if you have doubts, you can post here.

Correct me if I'm wrong but shouldn't you use fscanf or fgetc since he needs to read a file?

commented: Thanks! +6

@zeroliken

Oops, I'm sorry. Yeah, fscanf :) Thanks for correcting.

@OP
Sorry about that!

EDIT: fscanf

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.