Getting Data from Two Different Text Files and Storing into Structs

closed account (9GEqko23)
My C++ program won't get the data from the text file and store it into a struct. It compiles without any errors but it won't do what it's supposed to do.

Also note that I have a header file and a main file.

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
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
#include <iostream>
#include <fstream>
#include "part.h"

using namespace std;

struct Part
{
	int id;
	string name;
	int supplier[3];
};

struct Supplier
{
	int id;
	string name;
	string address;
};

bool openFiles()
{
	ifstream partFile;
	ifstream supplierFile;

	Part parts[50];
	Supplier suppliers[50];

	int cnt = 0;
	int cnt2 = 0;
	string temp;

	//Opens File and Checks
	partFile.open("parts.txt");
	supplierFile.open("suppliers.txt");

	if(partFile.fail() || supplierFile.fail())
	{
		cout << "Error: Files Not Found" << endl;
		return false;
	}

	else
	{
		cout << "Opening Files Successful" << endl;
		
		//Storing from Files into Structs
		while (!partFile.eof())
		{ 
			partFile >> parts[cnt].id;
			getline(partFile, parts[cnt].name);
			for (int i = 0; i < 3; i++)
			{
			partFile >> parts[cnt].supplier[i];
			}

			cnt++;
		}

		partFile.close();

		while (!supplierFile.eof())
		{
			supplierFile >> suppliers[cnt2].id;
			getline(supplierFile, suppliers[cnt2].name);
			getline(supplierFile, suppliers[cnt2].address);
			
			cnt2++;
		}

		supplierFile.close();

		//Output to Check if the data is in the structs
		for (int i = 0; i < cnt; i++)
		{
			cout << parts[i].id << endl;
			cout << parts[i].name << endl;
			for (int j = 0; j < 3; j++)
			{
			cout << parts[i].supplier[j];
			}
		}
		return true;
	}
}
Last edited on
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.
closed account (9GEqko23)
Sorry about that. Thanks.
One issue is that you are looping on EOF, which is wrong because EOF flag is not set until the first failed read attempt. You should loop on the input operation itself, but with your current scenario you can just change to a do-while loop. Don't increment the counters until you verify that the input succeeded.
Last edited on
closed account (9GEqko23)
I did a do while loop but it still doesn't work.

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
do {
			char temp[50];
 
			partFile.getline(temp, 50, '\t');
			parts[cnt].id = atoi(temp);
			partFile.getline(parts[cnt].name, 50, '\t');
			for (int j = 0; cnt < 3; j++)
			{
			partFile >> parts[cnt].supplier[j];
			}

			cnt++;
		} while (!partFile.eof());

		partFile.close();

		do {
			supplierFile >> suppliers[cnt2].id;
			supplierFile.getline(suppliers[cnt2].name, 50, '\t');
			supplierFile.getline(suppliers[cnt2].address, 50, '\t');
			
			cnt2++;
		} while (!supplierFile.eof());

		supplierFile.close();
Also there is a getline after formatted input problem.
http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction
Again, I hope anyone will actually read this thing
Topic archived. No new replies allowed.