reading line by line from txt file

Pages: 12
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 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;
}
did u try like this

while(!datafile.eof())
{
getline(datafile,input,'$');
}
@vichu8888
This will not do what is intended as this is only 1 of several ways that a read can fail:
while(!datafile.eof())

eof() does not look to see if the last portion you read included the last byte of the file. It does not look to see if the next byte is after the end of the file.

eof() does, however, report whether the last read included bytes past the end of the file.


@blaa15 Can you show an example of content from your input file?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ifstream dataFile("LabtwoNames.txt"); //ifstream

////redundant...
//if (dataFile) 
//{
//getline(dataFile, input, '$');

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

getline(dataFile, input, '$'); //likely only need 1 of these
}
dataFile.close()


Last edited on
Can u give me the exact question and txt file please?
Im a beginner in C++, I havent encountered vector yet. So I have done something that what I understood so far.
The following code will read data from input file and display the results
the input file has following data:

$alex $jude $ang mo kio $sing $398423
$mathew $stewards $brisbane $aus $01239
$chris $aditya $second street $indonesia $2938


The output will be

alex
jude
ang mo kio
sing
398423

mathew
stewads
brisbane
aus
01239

chris
aditya
second street
indo
press any key to continue


And the code is
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

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


using namespace std;

int main()
{
	
	string input; 

	ifstream in;
	in.open("input.txt");

	if(!in)
	{
		cout<<"No input file"<<endl;
		system("pause");
		return 1;
	}
	getline(in,input,'$');
	while(!in.eof())
	{
		cout<<input<<endl;
		getline(in,input,'$');
	}

	system("pause");
	return 0;
}



Is this what you asking for?
@soranz
May I know the other ways to read the file and check the end of the file. Because i just started learning(2 months) and Im keen to know more stuffs :)

thank you
Free access to typing tutor courses for 6 months for everyone! Just register here: www.solotyping.com
@vichu8888 yes, but I am also trying to figure out how I can get that on the output in 3 lines per address instead of 6. I have tried several ways and it still isn't right. my input txt file is:

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
$
can u show sample output u expecting?.Because I coudlnt get a image of what you think.sorry for that.
is it like this
Tommy
Gunn
22 Winchester Drive
Calamara, AL
38880
yes, i finally figured it out that part, however i now have two extra commas at the beginning of the output screen. so it looks like this now:
,

,
Gunn, Tommy
22 Winchester Drive
Calamar, AL 38880
Great congrats for that
thanks, but how do I get rid of the two commas at the beginning that I don't need?
can u show the codes?

Did u check ur text file?
text file is the same

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
$

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()
{

//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[2]<<", "<< input[1]<<endl;
cout<< input[3]<< endl;
cout<< 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";


vector<C_People> person;

}
system("Pause");
return 0;
}
it is probably something small I am overlooking.
Yeah. Where would one look for output? Maybe look for cout.. oh wait. There's a cout. There's a comma. I wonder if that's related to the output?

Why are you trying to output stuff that hasn't been read in yet?

The only reason it outputs what you're expecting it to after the commas is because the loop condition is wrong.
Last edited on
working on it.will let u know once done,
That's because I don't know what I am doing. This program was hard and I get what its supposed to do, I just don't get where everything is supposed to go.
do I just need to turn in what I have? at least get some kind of grade on it and not a big fat 0.
Pages: 12