954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Remove reading data from main function

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);
	}
}
tyliang
Newbie Poster
12 posts since Aug 2009
Reputation Points: 10
Solved Threads: 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);
	}
}
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

tyliang
Newbie Poster
12 posts since Aug 2009
Reputation Points: 10
Solved Threads: 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?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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.

Hiroshe
Posting Whiz in Training
256 posts since Jun 2008
Reputation Points: 431
Solved Threads: 17
 
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.

tyliang
Newbie Poster
12 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You