Reading lines from txt file

I am trying to read data from a .txt doc line by line with delimiters. we also have to populate a vector with the info. I have tried everything I know to do, but no matter what I do it says "ERROR: Cannot open file". I saved the info into a new txt file and now my output is showing only the zipcodes of the people.Can someone please help me? I have been trying to do this for 3 days. all help is appreciated. here is my code.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

class C_People
{
public:
string firstName;
string lastName;
string address;
string city;
string state;
string zipcode;
};

int main()
{
C_People person;

string input;

fstream da
Um... I think something got cut off. The actual meat of your program where it's supposed to open your text file seems to be missing, and you're also missing a semicolon and a closing bracket for main. That would be helpful to see. :)

-Albatross
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

class C_People
{
public:
string firstName;
string lastName;
string address;
string city;
string state;
string zipcode;
};

int main()
{
C_People person;

string input;

fstream dataFile("LabtwoNames.txt", ios::in);

if (dataFile)
{
getline(dataFile, input, '$');

while (dataFile)
{
cout << input << endl;

getline(dataFile, input, '$');
getline(dataFile, input, '$');
getline(dataFile, input, '$');
getline(dataFile, input, '$');
getline(dataFile, input, '$');
getline(dataFile, input, '$');

}

dataFile.close();
}
else
{
cout << "ERROR: Cannot open file.\n";
}
system("Pause");
return 0;
}


lol thanks for pointing that out.
If you use Visual C++ 2010 Express edition you should create, add the file you want to read in "VisualStudio 2010\Projects\ NameOfYourProject\ NameOfYourProject" where "NameOfYourProject" is the project you work on
I am actually using bloodshed because I can't get visual studio to work for some reason.
please add an example of the content inside the LabtwoNames.txt file.
Last edited on
Start File
$Tommy$Gunn$22 Winchester Drive$Calamar$AL$38880
$Bobby$Smither$117 Drover Drive$Budville$NC$28711
$William$Hammet$45 East West Street$Springfield$NC$28881
$Shenna$North$10 Tenth Street$Canopener$AL$31110
$Robert$Stillskin$300 Square Circle$Picklestown$NC$27887
$Walter$Mellon$99 Main Street$Blueville$AL$33990
$
is this what you needed?
It looks like in your while loop you are just overwriting the input string instead of adding to it, that would be why the variable input would only hold the last part of the line in the txt file (which is the zipcode). I suggest using an array of strings to hold the inputs, or to use a different variable for each input.
Do you have much experience with classes? Using class objects to hold that information would be your best option.
For right now here's a working example of an array, plus the fix to your file-reading issues. I'm going to be late for work so I didn't have time to set up a double-array but that's what you would need if you wanted to get this fully working. input[0][0], input[0][1], input[0][2] etc.
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

class C_People
{
public:
string firstName;
string lastName;
string address;
string city;
string state;
string zipcode;
};

int main()
{
C_People person;

string input[7];

fstream dataFile("LabtwoNames.txt", ios::in);

if (dataFile.is_open())
{
getline(dataFile, input[0], '$');
cout<<input[0]<<endl;
while (dataFile.good())
{
cout <<input[1]<< input[2]<< input[3] << input[4]<< input[5]<< input[6] << endl;

getline(dataFile, input[1], '$');
getline(dataFile, input[2], '$');
getline(dataFile, input[3], '$');
getline(dataFile, input[4], '$');
getline(dataFile, input[5], '$');
getline(dataFile, input[6], '$');

}

dataFile.close();
}
else
{
cout << "ERROR: Cannot open file.\n";
}
system("Pause");
return 0;
}  


I'm actually not too great with classes myself, if anyone has a way to get the info from the file into variables for several objects using a loop I'd love to see it.
Last edited on
Thank you! Actually, I don't feel very confident with program writing at all. and I think we started learning about classes a few weeks ago in my class.

now about the array, I do know that arrays and vectors are similar, right? since I have to incorporate a vector in here at the end, how would I go about doing that?

I really appreciate your help.
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

class C_People
{
public:
string firstName;
string lastName;
string address;
string city;
string state;
string zipcode;
};

int main()
{
vector<C_People> person;

string input[7];

fstream dataFile("LabtwoNames.txt", ios::in);

if (dataFile.is_open())
{
getline(dataFile, input[0], '$');
cout<<input[0]<<endl;
while (dataFile.good())
{
//cout <<input[1]<< input[2]<< input[3] << input[4]<< input[5]<< input[6] << endl;

	person.resize(person.size() +1);
	getline(dataFile, input[1], '$');
	person[person.size() - 1 ].firstName = input[1];
	getline(dataFile, input[2], '$');
	person[person.size() - 1 ].lastName = input[2];
	getline(dataFile, input[3], '$');
	person[person.size() - 1 ].address = input[3];
	getline(dataFile, input[4], '$');
	person[person.size() - 1 ].city = input[4];
	getline(dataFile, input[5], '$');
	person[person.size() - 1 ].state = input[5];
	getline(dataFile, input[6], '$');
	person[person.size() - 1 ].zipcode = input[6];

	cout <<person[person.size() - 1 ].firstName<< person[person.size() - 1 ].lastName<< person[person.size() - 1 ].address <<person[person.size() - 1 ].city<< person[person.size() - 1 ].state<< person[person.size() - 1 ].zipcode << endl;
}

dataFile.close();
}
else
{
cout << "ERROR: Cannot open file.\n";
}
system("Pause");
return 0;
}  
OK, I'm going to have to look up a couple of things there, but if you just did what I think you did gtkano, then you just helped me with several programs that I've been having issues with. Thank you!

Edit; Yep, that's it! follow gtkano's code, it does what you are wanting. Thank you gtkano!!!

I didn't even know that you could make objects with arrays.

@blaa15 - gtkano made each class object so that it contains an array in the name, it is better than my suggestion of using a double arrays because it, well, it's better structured. I suggest writing cout<<person[1].firstName <<person[1].lastName<<person[1].address<<endl; after the dataFile.close() to show how you can look up the information after the file is closed now.

OMG Thank you gtkano!!!
Last edited on
Topic archived. No new replies allowed.