Problem with input from file

Hi all,

I have a problem with getting data from the txt file outside, I can't get the data. The compiler say there is no error.

Here is my code

Function.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
#include <iostream>
using namespace std;
#include <string>
#include <fstream>
struct student 
{
	char fullname[100];
	char birthday[15];
	char mail[100];
	char address[128];
};
//Input data from file
void inputdata(student &s, ifstream &in);

//Input list of student's data
void inputlist(student a[], ifstream &in);

//Output student
void outputdata(student &s, ofstream &in);


Function.cpp
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
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
#include"Function.h"

//Input data for a student
void inputdata(student &s, ifstream &in)
{
	in.getline(s.fullname,100);
	in.getline(s.birthday,15);
	in.getline(s.mail,100);
	in.getline(s.address,128);
}

//Input a list of students
void inputlist(student a[], ifstream &in)
{
	in.open("input.txt");
	int n, i;
	in >> n;
	for(i=0;i<n;++i)
	{
		inputdata(a[i], in);
	}
	in.close();
}

//Output data of a student 
void outputdata(student &s, ofstream &out)
{
	out.open("output.txt");
	out << "Name: " << s.fullname;
	out << "Birthday: " << s.birthday;
	out << "Mail: " << s.mail;
	out << "Address: " << s.address;
}


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;
#include"Function.h"
void main()
{
	student a[100];
	ifstream in;
	ofstream out;
	inputlist(a, in);
	cout << a[0].fullname;
	outputdata(a[0], out);
}


The txt file is like :
1
2
3
4
5
1
name
DOB
mail
address


Please help, I have tried to figure out the problem for more than 1 hour.
Then get a better compiler, as main must return int.

¿how is your program failling? ¿Does the file open?
Main is void, it must return int too ?

I don't know if the file open :D. I'm just a newbie.

I use Visual Studio 2010 btw.
main can't be void.

1
2
if( not in.is_open() )
  cerr << "The file did not open\n";
does it crash? If that is the case then it is because you are trying to hold wrong type of value in a variable. Eg: If you have written as a string and read as a integer. then it might be a problem. Sometimes due to errors blurry thing gets written to a file. That might also be the reason why it is reading wrong type of variable.
It doesn't crash. It runs well, that why I'm so mad. Just when I cout the result to see, there is nothing there.


Nothing comes up, seems that the file has been opened.
Last edited on
One of my friends show me the problem, thanks you guys anyway. You are the best, keep it up.
Last edited on
Topic archived. No new replies allowed.