I want to sub-function the reading and printing data.
But it says data corrupted.
Sorry for my poor english.

Example of inputfile.txt

Michael
000000
m
michael@abc.com
75
82
90
#include <stdio.h>
#include <string.h>
void readData();
void printData();


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

int main(void)
{
	struct student data[20];
	readData();
	printData();

	return 0;
}

void readData()
{
	struct student data[20];
	int i=0;
	FILE* input;
	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 printData()
{
	readData();
	struct student data[20];
	for(int i=0;i<20;i++)
	{
		printf("%d.%s\n%.6d\n%c\n%s\n%d\n%d\n%d\n\n",i+1, data[i].studName,data[i].studID,data[i].sex,data[i].email,data[i].mathsScore,data[i].englishScore,data[i].scienceScore);
	}
}

Like so

#include <stdio.h>
#include <string.h>

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

void readData(struct student data[]);
void printData(struct student data[]);

int main(void)
{
	struct student data[20];
	readData(data); // pass parameter
	printData(data);

	return 0;
}

void readData(struct student data[])
{
	// now a parameter struct student data[20];
	int i=0;
	FILE* input;
	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 printData(struct student data[])
{
	// already called it once, readData();
	// a parameter struct student data[20];
	for(int i=0;i<20;i++)
	{
		printf("%d.%s\n%.6d\n%c\n%s\n%d\n%d\n%d\n\n",i+1, data[i].studName,data[i].studID,data[i].sex,data[i].email,data[i].mathsScore,data[i].englishScore,data[i].scienceScore);
	}
}

I still have error. When I debug it shows error [468].

You need to post some context.

What is error 468, (I've no idea, each compiler is different).

Which line is that on?

Does it happen when you compile the program or run it?

How do you fit a a name and an email address in char's? You probably want: char studName[256]; and char email[256]; . To be safe against buffer overflow's, use fgets() to get the strings.
Good luck.

commented: Good catch +36

How do you fit a a name and an email address in char's? You probably want: char studName[256]; and char email[256]; . To be safe against buffer overflow's, use fgets() to get the strings.
Good luck.

Thank you. Never realize have to put array to fit the email.

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.