Reading data from files

Program objective is to read the student id from a file and there following answers which are two and false. I have provided a sample input of a file. The user will enter in an id and then enter in twenty true-false answers. Meanwhile, I have written a code that allws the user to enter in a studentid up to certain size and an answers that is char viariabe and will store in twenty. My problem is that what if there is more then one student. How can I read that???

1
2
3
4
5
6
7
8
9
10
  void ReadStudentid_TestAnswers(ifstream &infile, ofstream& outfile,char studentid[], int idsize,char answers[],int answersize)
{
	//Read data into infile 
	infile>>studentid[idsize];
	infile>>answers[answersize];

	//output data into outfile
	outfile<<"ID: "<<



Sample Input from file


abc54301 tftftftttftftffttft

Last edited on
use getline() to get the input file one line at a time... and put it in a loop. Your loop condition should check eof on your input file.
why can't I use infile directly. It will stop reading the id once a whitespace is found. Also, cant I not use parallel arrays here? Speaking of eof, I'm really not clear in that particular topic. I know once that file reaches end of file, it stops reading, but how can I implement it in my code.. Do I do while(infile.open)?
Hello?
Worked on my code for a little while and somehow I believe I got the data to be read by using "while file is open" or while(infile). Meanwhile, I still cant determine what is wrong with my code here. I have entered the following input in a file.
abcdefs tftftftftft
. In my output file I get the following in return

ID: ÌÌÌÌÌÌÌÌÌÌ
ID: ÌÌÌÌÌÌÌÌÌÌ
ID: ÌÌÌÌÌÌÌÌÌÌ
ID: ÌÌÌÌÌÌÌÌÌÌ
ID: ÌÌÌÌÌÌÌÌÌÌ
ID: ÌÌÌÌÌÌÌÌÌÌ
ID: ÌÌÌÌÌÌÌÌÌÌ
ID: ÌÌÌÌÌÌÌÌÌÌ
ID: ÌÌÌÌÌÌÌÌÌÌ


May anyone help me assist with this problem please :)
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



//Function for reading the student id
void idreader(char id[],int size, ofstream &out)
{
	int index=0;
	for(index=0;index<size;index++)
	{
		out<<id[index];
	
	}

}

//Function: Parrel array to to store student ids and test answers.
void ReadStudentid_TestAnswers(ifstream &infile, ofstream& outfile,char studentid[], int idsize,char answers[],int answersize)
{
	while (infile)
	{
	//Read data into infile 
	infile>>studentid[idsize];
	infile>>answers[answersize];

	//output data into outfile
	outfile<<"\nID: ";
	idreader(studentid,10,outfile);

	}

}

There are a couple of problems here.
1
2
3
4
5
	while (infile)
	{
	//Read data into infile 
	infile>>studentid[idsize];
	infile>>answers[answersize];

One, studentid[idsize] is a single character located in memory just past the end of the studentid array. That is, it doesn't read a string, it just reads a character. And unfortunately it stores it in a memory location which is not owned by your program, causing potential corruption of some other data with unpredictable results.
It should be simply infile>>studentid;
The same applies to the answers array.

The second problem is the while loop. It is testing the status of the file before reading from it. But what the program really needs to know is what is the status after attempting to reading from the file.

Thus the code should be changed like this, solving both issues:
1
2
3
4
5
6
7
    // Read data from infile
    while (infile >> studentid >> answers)
    {
        // output data into outfile
        outfile<<"\nID: ";
        idreader(studentid,10,outfile);
    }

But there we encounter a second problem. What is the value '10' passed as a parameter to idreader()? Apparently it tells that function that it needs to output 10 characters from the studentid[] array , even though the actual data is only 7 characters long. "abcdefs".
It would be simpler and safer to simply put:
1
2
3
4
5
6
    // Read data from infile
    while (infile >> studentid >> answers)
    {
        // output data to outfile
        outfile<<"\nID: " << studentid;
    }
Last edited on
Chervil, you just overwhelmed me with this information :O.

First. In my book, it has an example of char list[100][10]. Using this in an example would mean a list of 100 names that will store each name up to 15 characters. Well in my code. I'm using a similar approach. In each line I want to store in a certain number of characters or studentid. I'm viewing this code to store up to ten characters for student id.Also, I'm aware I can use a string, but my book wants me to use an array for this. If possible?

Actually, I think I might have got an idea. Can I make a void function, that inputs character value from the user under 10 character. If space is found, I will break the loop.

1
2
3
4
5
while (infile)
	{
	//Read data into infile 
	infile>>studentid[idsize];
	infile>>answers[answersize];
Last edited on
My second problem that you stated. I have been thought this way that when your reading a file. This while(infile) means from my understanding while the file is open, perform whatever is underneath it until end of file is found.

1
2
3
4
5
6
7
// Read data from infile
    while (infile >> studentid >> answers)
    {
        // output data into outfile
        outfile<<"\nID: ";
        idreader(studentid,10,outfile);
    }
Ok chervil! Spent some time improving on my code a little bit and this is what I came up with as of now. Like earlier you said,if the actual data or id has only 7 characters instead of 10 then it will output weird results. Well I have my idandanswerreader function to a point that if the null character is found, it will get out of the loop. Meanwhile, this is the problem im facing now. When I have
this as my input.

hellosfwl tftftftft
fahmankha tftffffff


I only get this as my output.

ID: fahmankha		Answers: tftffffff 
. I only get one id instead of the entire list :(
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
//Function for reading the student id
void idandanswerreader(char id[],int size, ofstream &out)
{
	int index=0;
	for(index=0;index<size;index++)
	{
		out<<id[index];
		if (id[index]=='\0')
			break;
	
	}

}

//Function: Parrel array to to store student ids and test answers.
void ReadStudentid_TestAnswers(ifstream &infile, ofstream& outfile,char studentid[], int idsize,char answers[],int answersize)
{
	while (infile>>studentid>>answers)
	{
	//Read data into infile 
	studentid[idsize];
	answers[answersize];
	infile>>studentid;
	infile>>answers;
	

	//output data into outfile
	outfile<<"\nID: ";
	outfile<<studentid;

	//output the answers on the file
	outfile<<"		Answers: ";
	idandanswerreader(answers,10,outfile);
	}

}
Although I think using separate functions is a very good idea, in order to keep things simple, here's an example with everything in main().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream infile("input.txt");
    
    char studentid[20];
    char answers[30];

    while (infile >> studentid >> answers)
    {
        cout << "Student id: " << studentid 
             << "  Answers: "  << answers << endl;
    }
}

Output:
Student id: hellosfwl  Answers: tftftftft
Student id: fahmankha  Answers: tftffffff
Cherivil, I got my code working properly now. Instead of putting infile >> studentid >> answers) I just did while(infile) and it worked properly, thus showing every input on the output file. Unfortunately, my character can hold up to ten characters. Suppose I entered in 12. Why is my output showing 12 characters instead of 10 characters including the null characters. I thought if the user enters in more then ten characters the the rest of characters will be discarded.

im using this function here to show the output for the user input from the input file.
1
2
3
4
5
6
7
8
9
10
11
12
void idandanswerreader(char id[],int size, ofstream &out)
{
	int index=0;
	for(index=0;index<size;index++)
	{
		out<<id[index];
		if (id[index]=='\0')
			break;
	
	}

}


here is my other function that reads from the input file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void ReadStudentid_TestAnswers(ifstream &infile, ofstream& outfile,char studentid[], int idsize,char answers[],int answersize)
{
	while (infile)
	{
	//Read data into infile 
	studentid[idsize];
	answers[answersize];
	infile>>studentid;
	infile>>answers;
	

	//output data into outfile
	outfile<<"\nID: ";
	outfile<<studentid;

	//output the answers on the file
	outfile<<"		Answers: ";
	idandanswerreader(answers,10,outfile);
	}

}


Do you think I should make a void function similar to this...

1
2
3
void enterid(char list[],int size)
for (int row=0;row<size;row++)
cin>>list[row];


If I can use the method too, then what is wrong with my previous code, or how can I fix that particular code. Apologize if im bugging you too much : D
ID: fahmankhansaad	


Forgot to show you my output. So this was the output I got. Once again, My output is suppose to show only up to 9characters. Not more then 9.
I seem to have not quite understood the purpose to this project.
So far I've got that there is a file containing on each row a student ID and a list of t/f answers.

At first I thought the studentid would be fixed at exactly 9 characters. Or was it exactly 10 characters.
Like this:
abc54301 tftftftttftftffttft

well, there studentid is 8 characters.
then this:
hellosfwl tftftftft
fahmankha tftffffff

thats 9 characters.
Now I see
ID: fahmankhansaad
that's 14 characters.

So the thing I don't understand what the input data will really look like. Will it really vary so much. If the data is longer than 9 characters should it be printed out as an error: invalid input or what? I'm just lost here trying to understand the requirements.

I'm struggling to give any sort of coherent advice as it just feels like the question keeps on changing. Perhaps you could clarify the requirements, maybe by posting the actual question which you were given.

Sorry for inconsistency and not being clear. Your correct about the student I'd being fixed at 9 character or it can be less then 9.; however the student I'd can no longer be more then 9 characters. Suppose a user accidentky entered in more then 9 characters. In my char id[10] I only want it to store upto 9 characters. In my case over here, in the input file, the student entered in

anc54301 tftftftft
Fahman tftftftft
Fahmankhansaad tftftftft


This is the input I had stored in the Input file. For the answer[10] I'm just storing true and false questions up to 9 rather then 20. Assuming this is the final input file, the output I'm getting is


Id: anc54391 answers: tftftftft
ID:Fahman Answers: tftftftft
ID: Fahmankhansaad Answers:  tftftftft



I get the correct output for the first two students, but for the last one I'm getting an incorrect output for the id. As i said before, it can onky store up to 9 characters, so why is it showing more then 9 character here on my output file. I hope I clarified it enough for you understand :)

Topic archived. No new replies allowed.