0

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);
	}
}
3
Contributors
5
Replies
7
Views
8 Years
Discussion Span
Last Post by tyliang
0

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);
	}
}
0

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?

1

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.

Votes + Comments
Good catch
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.