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.
@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()
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
@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 :)
@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
$
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.