I need to print to data stream to an output file but I'm not sure how to catch the data.
Given example inputfile.txt has 20 data with a structure
Michael
000000
m
abc@123.com
50
60
70

#include <stdio.h>
#include <string.h>
void scanData(struct student data[]);
void avgMaths(struct student data[]);
void avgEnglish(struct student data[]);
void avgScience(struct student data[]);
void printMaths(struct student data[]);
void printEnglish(struct student data[]);
void printScience(struct student data[]);
void printName(struct student data[]);
void maxMaths(struct student data[]);

struct student
{
	char studName[256];
	int studID;
	char sex;
	char email[256];
	int mathsScore;
	int englishScore;
	int scienceScore;
};

int main(void)
{
	struct student data[30];
	for(;;)
	{
		int i=0;
		scanf("%d", &i);
		scanData(data);
		switch(i)
		{
		case 1:avgMaths(data);break;
		case 2:avgEnglish(data);break;
		case 3:avgScience(data);break;
		case 4:{avgMaths(data);avgEnglish(data);avgScience(data);}break;
		case 5:printMaths(data);break;
		case 6:printEnglish(data);break;
		case 7:printScience(data);break;
		case 8:{printMaths(data);printEnglish(data);printScience(data);}break;
		case 9:maxMaths(data);break;
		case 10:printName(data);break;
		default:printf("GG");
		}
	}
	return 0;
}
void scanData(struct student data[])
{
	FILE* input;
	int i=0;
	input=fopen("inputfile.txt", "r");
	while((fscanf(input,"%s %d %c %s %d %d %d", &data[i].studName,&data[i].studID,&data[i].sex,&data[i].email,&data[i].mathsScore,&data[i].englishScore,&data[i].scienceScore))==7)
	{
		i++;
	}
}

void avgMaths(struct student data[])
{
	int totalMaths=0;
	int avgMaths=0;

	for(int i=0;i<20;i++)
	{
		totalMaths=totalMaths+data[i].mathsScore;
		avgMaths=totalMaths/20;
	}
	printf("Average Score for Maths: %d\n", avgMaths);
}
void avgEnglish(struct student data[])
{
	int totalEnglish=0;
	int avgEnglish=0;

	for(int i=0;i<20;i++)
	{
		totalEnglish=totalEnglish+data[i].englishScore;
		avgEnglish=totalEnglish/20;
	}
	printf("Average Score For English: %d\n", avgEnglish);
}
void avgScience(struct student data[])
{
	int totalScience=0;
	int avgScience=0;
	for(int i=0;i<20;i++)
	{
		totalScience=totalScience+data[i].scienceScore;
		avgScience=totalScience/20;
	}
	printf("Average Score for Science: %d\n", avgScience);
}
void printMaths(struct student data[])
{
	for(int i=0;i<20;i++)
		printf("%d. %s : %d\n", i+1,data[i].studName,data[i].mathsScore);
}

void printEnglish(struct student data[])
{
	for(int i=0;i<20;i++)
		printf("%d. %s : %d\n", i+1,data[i].studName,data[i].englishScore);
}

void printScience(struct student data[])
{
	for(int i=0;i<20;i++)
		printf("%d. %s :%d\n", i+1,data[i].studName,data[i].scienceScore);
}

void printName(struct student data[])
{
	for(int i=0;i<20;i++)
		printf("%s\n",data[i].studName);
}
void maxMaths(struct student data[])
{
	int temp=data[0].mathsScore;
	for(int i=0;i<20;i++)
	if(temp<data[i].mathsScore)
		temp=data[i].mathsScore;
	printf("Maximum Score for Maths:%d", temp);
}

Open a file for writing (almost the same as you do for reading), and replace your printf() calls with fprintf() calls.

I need to print to data stream to an output file but I'm not sure how to catch the data.
Given example inputfile.txt has 20 data with a structure
Michael
000000
m
abc@123.com
50
60
70

#include <stdio.h>
#include <string.h>
void scanData(struct student data[]);
void avgMaths(struct student data[]);
void avgEnglish(struct student data[]);
void avgScience(struct student data[]);
void printMaths(struct student data[]);
void printEnglish(struct student data[]);
void printScience(struct student data[]);
void printName(struct student data[]);
void maxMaths(struct student data[]);

struct student
{
	char studName[256];
	int studID;
	char sex;
	char email[256];
	int mathsScore;
	int englishScore;
	int scienceScore;
};

int main(void)
{
	struct student data[30];
	for(;;)
	{
		int i=0;
		scanf("%d", &i);
		scanData(data);
		switch(i)
		{
		case 1:avgMaths(data);break;
		case 2:avgEnglish(data);break;
		case 3:avgScience(data);break;
		case 4:{avgMaths(data);avgEnglish(data);avgScience(data);}break;
		case 5:printMaths(data);break;
		case 6:printEnglish(data);break;
		case 7:printScience(data);break;
		case 8:{printMaths(data);printEnglish(data);printScience(data);}break;
		case 9:maxMaths(data);break;
		case 10:printName(data);break;
		default:printf("GG");
		}
	}
	return 0;
}
void scanData(struct student data[])
{
	FILE* input;
	int i=0;
	input=fopen("inputfile.txt", "r");
	while((fscanf(input,"%s %d %c %s %d %d %d", &data[i].studName,&data[i].studID,&data[i].sex,&data[i].email,&data[i].mathsScore,&data[i].englishScore,&data[i].scienceScore))==7)
	{
		i++;
	}
}

void avgMaths(struct student data[])
{
	int totalMaths=0;
	int avgMaths=0;

	for(int i=0;i<20;i++)
	{
		totalMaths=totalMaths+data[i].mathsScore;
		avgMaths=totalMaths/20;
	}
	printf("Average Score for Maths: %d\n", avgMaths);
}
void avgEnglish(struct student data[])
{
	int totalEnglish=0;
	int avgEnglish=0;

	for(int i=0;i<20;i++)
	{
		totalEnglish=totalEnglish+data[i].englishScore;
		avgEnglish=totalEnglish/20;
	}
	printf("Average Score For English: %d\n", avgEnglish);
}
void avgScience(struct student data[])
{
	int totalScience=0;
	int avgScience=0;
	for(int i=0;i<20;i++)
	{
		totalScience=totalScience+data[i].scienceScore;
		avgScience=totalScience/20;
	}
	printf("Average Score for Science: %d\n", avgScience);
}
void printMaths(struct student data[])
{
	for(int i=0;i<20;i++)
		printf("%d. %s : %d\n", i+1,data[i].studName,data[i].mathsScore);
}

void printEnglish(struct student data[])
{
	for(int i=0;i<20;i++)
		printf("%d. %s : %d\n", i+1,data[i].studName,data[i].englishScore);
}

void printScience(struct student data[])
{
	for(int i=0;i<20;i++)
		printf("%d. %s :%d\n", i+1,data[i].studName,data[i].scienceScore);
}

void printName(struct student data[])
{
	for(int i=0;i<20;i++)
		printf("%s\n",data[i].studName);
}
void maxMaths(struct student data[])
{
	int temp=data[0].mathsScore;
	for(int i=0;i<20;i++)
	if(temp<data[i].mathsScore)
		temp=data[i].mathsScore;
	printf("Maximum Score for Maths:%d", temp);
}

Create your file pointer globally, by adding it before main.

ie:

// file to write to
FILE* outfile;

Than in whatever function you want to print to the file.

ie your maxMaths function:

void maxMaths(struct student data[])
{
        outfile = fopen("you file name.txt", "w");
	int temp=data[0].mathsScore;
	for(int i=0;i<20;i++)
	if(temp<data[i].mathsScore)
		temp=data[i].mathsScore;
	// printf("Maximum Score for Maths:%d", temp);
        fprintf( outfile, "Maximum Score for Maths:%d", temp);
}
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.