Problem with fstream. File will not save Data.

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <iomanip>
#include <string>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;

struct ClientInfo // struct data
{
	string  LastName;
	string  FirstName;
	string  Account;
	string  Month; 
	string  Day; 
	string  Year;
} ;


void Datain(ifstream &inData,ClientInfo person[],int);

void PrintData(ofstream &outData, ClientInfo person[],int);

void insertionSort(ClientInfo person[],int index);

const int NOA=2000;

int main()


{
	ClientInfo person[NOA];
	ifstream inData;
	ofstream outData;
	int t;

	char file[16];
	cout<< "Please enter Filename :";// open file to read from
	cin>>file;
	inData.open(file);

	char ofile[16];
	cout<<"Please enter Filename to save :";// open file to save data
	cin>>ofile;
	outData.open (ofile);
	system("cls");

	while (!inData.fail())// to loop till there is no more Data.
	{
		Datain(inData,person, t);
		if(!inData.fail())
			PrintData(outData,person, t);
	}
    inData.close();
	outData.close(); 

	return 0;
}

void Datain(ifstream &inData,ClientInfo person[], int)
{
int i;

inData>>person[i].LastName>>person[i].FirstName>>person[i].Account>>person[i].Month>>person[i].Day>>person[i].Year;

// to read in info from file and place data in vector.

}


void insertionSort(ClientInfo person[],int)
{
	int i, j;
	string index;
	for (i=1; i < NOA; i++)
	{//sorting data that program recieves and places everything in order.

		index = person[i].LastName;
		j = i;
		while ((j > 0) && (person[j-1].LastName >index))
		{
			person[j].LastName = person[j-1].LastName;
			person[j].FirstName = person[j-1].FirstName;
			person[j].Account = person[j-1].Account;
			person[j].Month = person[j-1].Month;
			person[j].Day = person[j-1].Day;
			person[j].Year = person[j-1].Year;
			j = j - 1;
		}
		person[j].LastName = index;
	}
}




void PrintData(ofstream &outData, ClientInfo person[],int)
// to print file last name, first name, account number, month, day, year.
// on each "page", a page has 60 lines.  
// With header "ClientInfo".  
// "Last,first name, AccountNumber,Month,Day, Year of last Transaction".

{

	int i;
for (i=0; i < NOA; i++)
{
  outData<<"Customer Client info\n";
  outData<<"Name"<<setw(6)<<"AccountNumber"<<setw(5)<<"Month"<<setw(5)<<"Day"<<setw(5)<<"Year of last Transaction\n";
  outData<<person[i].LastName<<setw(1)<<person[i].FirstName<<setw(5)<<person[i].Account<<setw(5)<<person[i].Month<<setw(5)<<person[i].Day<<setw(5)<<person[i].Year;
  outData.flush();
  
}
  

                                     
}
Last edited on
Your problems are as follows:

Line 36 int t;

1
2
3
4
5
6
7
	
while (!inData.fail())// to loop till there is no more Data.
{
    Datain(inData,person, t);
    if(!inData.fail())
    PrintData(outData,person, t);
}


1
2
3
4
5
6
void Datain(ifstream &inData,ClientInfo person[], int)
{
    int i;
    inData>>person[i].LastName>>person[i].FirstName>>person[i].Account>>person[i].Month>>person[i].Day>>person[i].Year;
    // to read in info from file and place data in vector.
}


You do not initialise the variable t, and you pass it to the Datain function.
You do not use it in the Datain function - instead you create another
local variable i, which you use uninitialised (ie random value).
So your Datain function is not working at all.

It also doesn't help that you haven't named your function variables properly
For example you have:
void Datain(ifstream &inData,ClientInfo person[],int); Your have an unamed int. Not illegal but - you can't use it directly.

You code should look like this:
int t=0; //initialise the index variable

1
2
3
4
5
6
7
8
	
while (!inData.fail())// to loop till there is no more Data.
{
    Datain(inData,person, t);
    if(!inData.fail())
    PrintData(outData,person, t);
    t++; //update the index
}


1
2
3
4
5
6
void Datain(ifstream &inData,ClientInfo person[], int t)
{
    //int i; //Don't need this.
    inData>>person[t].LastName>>person[t].FirstName>>person[t].Account>>person[t].Month>>person[t].Day>>person[t].Year;
    // to read in info from file and place data in vector.
}


Small mistakes which should be easy to sort out.


thanks
Topic archived. No new replies allowed.