Reading from files argument help

Hi, I am trying to write a program for uni that will read two different .txt files and output some data about them. I am fairly confident I can do this I just cant figure out what arguments to pass to my functions to read the files properly?

students.txt is a long file, with one word on each line. If someone can point me in the right direction on how to properly pass the correct arguments across I am confident I can right the code in the functions to output the data.

students.txt example:
1 //number of entrys
Sarah
Jones
1111
y

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;

struct Student {
	string fName;
	string sName;
	int id;
	char paper;
};

struct Courses {
	int identifier;
	string title;
	int credits;
	int students;
};	

void assignInfo(); //assignment info
void readStudents(?);  //not sure what to pass to function

int main() {
	assignInfo();
	readStudents(Student.fName);
}

void assignInfo() {
	cout << "************************" << endl;
	cout << "* 159.234 Assignment   *" << endl;
	cout << "* 11079601 Name, Name  *" << endl;
	cout << "*       S2 2012        *" << endl;
	cout << "************************\n" << endl;
}

void readStudents(Student) {
	ifstream inFile("students.txt");
	if (!inFile) {
		cout << "Data file 'students.txt' not found" << endl;
		exit(EXIT_FAILURE);
	} else {
		int num=0, i=0;
		inFile >> num;
		for (i=0; i<num; i++) {
			inFile >> Student.fName >> Student.lname >> Student.id >> Student.paper;
		}
		
	}
}
Last edited on
The typical way is to call something like getline in a loop to read from the ifstream and store the result into an array. It's up to u. The way u'r going u'll have to create a bunch of Student instances and pass each one into readStudents which could take a lot for memory.

Line 26 doesn't make sense because u'r passing a structure and not an instance of it. This makes more sense, for example:

1
2
3
Student newstudent;
newstudent.fname = "Jar Jar";
readStudents(newstudent);


Last edited on
thanks for the reply, that newstudent does make a lot more sense but would that run through my for loop to however many students was in the .txt? or would I have to create more instances like newstudent1 etc? (which would kind of defeat the purpose of the loop) lol. I think I am just a little confused on where they are supposed to get stored, if they should even get stored?

I am only supposed to read the file once and yeah I think I would have used a getline but we are not supposed to use functions that we have not yet covered. Just so annoying I cant even try and write the rest of the code because its not even reading the .txt in
Last edited on
Hi,

Never mind all the above code, I have spent hours re writing and re re writing my program.. Good news is that I have made much progress and it looks completely different.

Is there any way I can read from a .txt file until I reach a certain character? I want to read the top 3 lines but after that there could be unknown amount of lines (if any) until the -1.

i.e:
123123
Programming
10
11111
22222
44444
-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void initializeCoursesList(Courses coursesAll[], int & size) {
	ifstream inFile("courses.txt");
	if (!inFile) {
		cout << "Data file 'courses.txt' was not found" << endl;
		exit(EXIT_FAILURE);
	} else {
		inFile >> size;
		if (size < MAX_COURSES) {
			for (int i=0; i<size; ++i) {
				inFile >> coursesAll[i].identifier >> coursesAll[i].title >> 
coursesAll[i].credits >> coursesAll[i].numstudents;
				if (coursesAll[i].numstudents < 0) {
					cout << coursesAll[i].identifier << ", " << coursesAll[i].title 
<< ", credits " << coursesAll[i].credits << ", 0 students" << endl;
				}
			}
		}
	}
}


I need another loop or something to read the numstudents until it hits -1. Something like

1
2
if (coursesAll[i].numstudents >=0) {
  //A loop that reads inFile until the -1 
Last edited on
[SOLVED]

after hours of work...
hey man
how did you sloved it in fact i m struggling with it

its first time i have use structures and i dont know which argu for both student and course function should i pass in order to read and out put some information


like if i wanna output the student info for who have N in thire record


could you plz help me with this



sloved
i thought mine was solved but its not... I'm having problems storing the info. I want to store certain lines from the txt into an array so I can access the info later but just cant figure out how to do this..

1
2
3
4
for (int i=0; i<size; ++i) {
				inFile >> studentsAll[i].fName >> studentsAll[i].lName >> studentsAll[i].id >> studentsAll[i].fees;
				studentsAll[i].id = studentsAll[i].list;
				cout << studentsAll[i].list << endl;


Can someone please tell me why I can't get the studentsAll[i].id to store its values in the array list? I've tried for hours and have run out of ideas.
What is happening or being stored into studentsAll[i].id instead ?

line 2 extracts into studentsAll[i].id
but then line 3 is storing whatever is in studentsAll[i].list into studentsAll[i].idagain. is this desired? what is being stored into studentsAll[i].list ?
Last edited on
Sorry i typed that out the wrong way.. line 2 is reading in 4 lines of a txt file, then i want to put what studentsAll[i].id reads into an array or a list of arrays so i can re read them later?

line 3 was supposed to read
 
studentsAll[i].list = studentsAll[i].id


but when I compile the prog I just get 3 empty spaces where I try and output the list to make sure I have stored them correctly.
Here's a nice way to organize your data. Encapsulation and safety is always good. It also promotes organization, so you'd have to go out of your way to get lost.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <list>

typedef class school
{
private:
	//types
	typedef struct course
	{
		//data
		unsigned int identifier;
		std::string title;
		unsigned int credits;
		unsigned int students;

		//course methods go here
	} course_t;
	typedef struct student
	{
		std::string fName;
		std::string sName;
		unsigned int id;
		char paper;
		std::list<const course_t*> courses;

		//student methods go here
	} student_t;
	
	//data
	std::list<course_t> courses;
	std::list<student_t> students;

	//school internal methods go here
public:

	//school external methods go here
	/*sampleMethod*/ const course_t * GetCourseType();
	/*sampleMethod*/ void EnrollNewStudent(std::string &fN, std::string &sN, unsigned int id, unsigned char paper, course_t * type);
} school_t;
Thanks for the help my codes changed quite a lot, find it hard to get the private and public and what's available around my head. ^It looks a bit confusing but I think its similar to what I have, but i'm still very confused on how to actually store values so they can be accessed later? Here is my whole program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;

const int MAX_STUDENTS=1000; //max students at uni
const int MAX_COURSES=200;   //max courses available

struct Student {
	string fName; //students first name
	string lName; //students last name
	string id;    //students ID number
	char fees;    //students fees 'Y' paid 'N' not paid
	string list;
};

struct Courses {
	int identifier;  //course identifier
	string title;    //course title
	int credits;     //credits on offer
	int numstudents; //number of students currently enrolled
	int enrolledstudents[MAX_STUDENTS];
};	

void assignInfo(); //assignment info
void initializeStudentList(Student studentsAll[], int & size);
//void initializeCoursesList(Courses coursesAll[], int & size);

int main() {
	assignInfo();
	cout << "Students not enrolled in any paper: " << endl;
	
	
	Student studentsList[MAX_STUDENTS];
	int numOfStudents=0; //initialize to 0
	initializeStudentList(studentsList, numOfStudents);
	
	//cout << "\n" << "Courses with no students enrolled: " << endl;
	//Courses coursesList[MAX_COURSES];
	//int numOfCourses=0; //initialize to 0
	//initializeCoursesList(coursesList, numOfCourses);
	//cout << "\n" << "Courses with NON-EXISTENT students enrolled: " << endl;
	
}

void assignInfo() {
	cout << "************************" << endl;
	cout << "* 159.234 Assignment 1        *" << endl;
	cout << "* 11079601 N, N                    *" << endl;
	cout << "*       S2 2012                        *" << endl;
	cout << "************************\n" << endl;
}

void initializeStudentList (Student studentsAll[], int & size) {
	ifstream inFile("students.txt");
	if (!inFile) {
		cout << "Data file 'students.txt' was not found" << endl;
		exit(EXIT_FAILURE);
	} else {
		inFile >> size;
		if (size <= MAX_STUDENTS) {
			for (int i=0; i<size; ++i) {
				inFile >> studentsAll[i].fName >> studentsAll[i].lName >> studentsAll[i].id >> studentsAll[i].fees;
				studentsAll[i].id = studentsAll[i].list;
				cout << studentsAll[i].list << endl;
				if (studentsAll[i].fees == 'N') {
					cout << studentsAll[i].lName << ", " << studentsAll[i].fName << ": ID " << studentsAll[i].id << ", paid fees  N" << endl;
				}
			}
		}
	}
}

/*
void initializeCoursesList(Courses coursesAll[], int & size) {
	ifstream inFile("courses.txt");
	if (!inFile) { //to check the file opens correctly
		cout << "Data file 'courses.txt' was not found" << endl;
		exit(EXIT_FAILURE);
	} else {
		inFile >> size;
		if (size < MAX_COURSES) {
			for (int i=0; i<size; ++i) {
				inFile >> coursesAll[i].identifier >> coursesAll[i].title >> coursesAll[i].credits >> coursesAll[i].numstudents;
				if (coursesAll[i].numstudents == -1) {
					cout << coursesAll[i].identifier << ", " << coursesAll[i].title << ", credits " << coursesAll[i].credits << ", 0 students" << endl;
				}
				//while (coursesAll[i].numstudents != -1) {
					//Courses.enrolledstudents[MAX_STUDENTS] = x;
				//}
			}
		}
	}
}
*/


So I am reading the students text, but I want to save all the student.ids I read in to access them later on in the program without re reading the file.

Thanks for your help
but I want to save all the student.ids I read in to access them later on in the program without re reading the file.


U can use pointers or keyword static to accomplish this...
Topic archived. No new replies allowed.