Reading info from txt

Pages: 12
hello there, probably you're going mad with these kind of doubts but i really appreciate some help.
I'm trying to get information from a txt file which looks like this
1
2
3
titulo,cantor,autor, album, genre, year
t1,c1,rrr, ff, gg , 676
t2,c2,fffd

(random stuff)

But Im not sure if Im doing something wrong or If I got my txt file in the wrong directory.

Any suggestions?

Regards

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 Musica
{
public:
string title;
string arthist;
string author;
string album;
string genre;
string year;
};

int main()
{
Musica person;

string input[7];

fstream dataFile("musica.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;
}  
getline(dataFile, input[1], '$');
getline(dataFile, input[2], '$');
getline(dataFile, input[3], '$');
getline(dataFile, input[4], '$');
getline(dataFile, input[5], '$');
getline(dataFile, input[6], '$');

you can use "for(i=0;i<7;i++)"
also this line:cout <<input[1]<< input[2]<< input[3] << input[4]<< input[5]<< input[6] << endl;

What's more, why you use a loop? And you should getline first and then use "cout".
As I said before, I'm kinda newbie. I though this was the easiest way and it seems to be working. it just cant get the txt file and I dunno why
”while“ meas loop. If you use it, the code will work all the time if the file stream is good. So you don't need it. And computer should read the file before display it.
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
  #include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

class Musica
{
public:
string title;
string arthist;
string author;
string album;
string genre;
string year;
};

int main()
{
Musica person;

string input[7];

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

if (dataFile.is_open())
{
getline(dataFile, input[0], '$');
cout<<input[0]<<endl;
if(dateFile.good==false)
{
cout<<"file stream is not good!"<<endl;
system("pause");
return 0;//stop the program
}
//get date first
getline(dataFile, input[1], '$');
getline(dataFile, input[2], '$');
getline(dataFile, input[3], '$');
getline(dataFile, input[4], '$');
getline(dataFile, input[5], '$');
getline(dataFile, input[6], '$');
//display
	cout <<input[1]<< input[2]<< input[3] << input[4]<< input[5]<< input[6] << endl;

//close it
dataFile.close();
}
else
{
cout << "ERROR: Cannot open file.\n";
}
system("Pause");
return 0;
}  
Thanks for help but it is actually working the way I posted. I understood your advises and I really appreciate it.

I'm having some issues though because Im suposed to create a music library and theres a first line with

title || Singer || Author || Album || Genre || Year

And i'd like to get from every line the correct info. I got this

No Cars Go, Arcade Fire, Win Butler, Neon Bible, Indie Rock, 2007

is there any chance to do it? I think the commas might be useful. i'm not sure though
bump
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

class Musica
{
public:
    string title;
    string artist;
    string author;
    string album;
    string genre;
    string year;
};

int main()
{
    Musica person;
    vector<Musica> music;
    ifstream dataFile("musica.txt");

    if (!dataFile.is_open())
    {
        cout << "ERROR: Cannot open file.\n";
        cin.get();
        return 1;
    }

    string line;

    getline(dataFile, line);
    cout << "Header\n" << line << "\n----\n"<< endl;

    const char comma = ',';
    while (getline(dataFile, line))
    {
        istringstream ss(line);

        getline(ss, person.title, comma);
        getline(ss, person.artist, comma);
        getline(ss, person.author, comma);
        getline(ss, person.album, comma);
        getline(ss, person.genre, comma);
        getline(ss, person.year, comma);

        music.push_back(person);
    }

    dataFile.close();

    for (int i=0; i<music.size(); i++)
    {
        cout << "Title:  " << music[i].title << endl;
        cout << "Artist: " << music[i].artist << endl;
        cout << "Author: " << music[i].author << endl;
        cout << "Album:  " << music[i].album << endl;
        cout << "Genre:  " << music[i].genre << endl;
        cout << "Year:   " << music[i].year << endl;

        cout << "\n------------------\n" << endl;
    }

    cin.get();
    return 0;
}
Last edited on
Your program works if the file is in the right folder, so maybe you need to tell it where the file is if it's not in the folder with the program.
Thank you so much Chervil , finally got it working. Still trying to understand some points but I really appreciate your help.

Best regards
One last question. I want do add the ID field, before "Title". How come, its not working?


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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

class Musica
{
public:
	string ID;
    string title;
    string artist;
    string author;
    string album;
    string genre;
    string year;
	
};

int main()
{
    Musica person;
    vector<Musica> music;
    ifstream dataFile("musica.txt");

    if (!dataFile.is_open())
    {
        cout << "ERROR: Cannot open file.\n";
        cin.get();
        return 1;
    }

    string line;

    getline(dataFile, line);
    cout << "Header\n" << line << "\n----\n"<< endl;

    const char comma = ',';
    while (getline(dataFile, line))
    {
        istringstream ss(line);
		//cout << "passei por aqui" << endl;
		getline(ss, person.ID, comma);
        getline(ss, person.title, comma);
        getline(ss, person.artist, comma);
        getline(ss, person.author, comma);
        getline(ss, person.album, comma);
        getline(ss, person.genre, comma);
        getline(ss, person.year, comma);

        music.push_back(person);
    }

    dataFile.close();

    for (int i=0; i<music.size(); i++)
    {
		//cout << music[i].ID << endl;
        cout << "ID:  " << music[i].ID << endl;
		cout << "Title:  " << music[i].title << endl;
        cout << "Artist: " << music[i].artist << endl;
        cout << "Author: " << music[i].author << endl;
        cout << "Album:  " << music[i].album << endl;
        cout << "Genre:  " << music[i].genre << endl;
        cout << "Year:   " << music[i].year << endl;

        cout << "\n------------------\n" << endl;
    }

    cin.get();
    return 0;
}
Did you also change the contents of the file musica.txt to match?
1
2
3
4
ID, Title, Artist, Autor, Album, Genre, Year
0001, Idioteque, Radiohead, Thom Yorke, Kid A, Electronic, 2000
0002, High Hopes, Kodaline, Stephen Garrigan, In a Perfect World, Alternative Rock, 2013
0003, Home, Edward Sharpe & the Magnetic Zeros, Edward Shape & the Magnetic Zeros, Here, Indie Folk, 2012


Did this. Not working though
Could you please explain what is meant by "Not working though"?
I copy+pasted your code and it worked for me with no problems.
Thanks for your help, Chervil, btw.

Well, it isnt working actually.

This is happening:
http://imgur.com/hO3Nk25

My playlist:

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
ID, Title, Artist, Autor, Album, Genre, Year
ABCD, Idioteque, Radiohead, Thom Yorke, Kid A, Electronic, 2000
 EFGH, High Hopes, Kodaline, Stephen Garrigan, In a Perfect World, Alternative Rock, 2013
 IJKL, Home, Edward Sharpe & the Magnetic Zeros, Edward Shape & the Magnetic Zeros, Here, Indie Folk, 2012
 Sleeping Lessons, The Shins, James Mercer, Wincing the Night Away, Indie Rock, 2007
 Big Jet Plane, Angus & Julia Stone, Angus Stone, Down The Way, Indie Folk, 2012
 Laura, Bat For Lashes, Natasha Khan, The Haunted man, Indie Rock, 2012
 No Cars Go, Arcade Fire, Win Butler, Neon Bible, Indie Rock, 2007
 Little Bird, Lisa Hannigan, Lisa Hannigan, Passenger, 
 The clock, Thom Yorke, Thom Yorke, The Eraser, Alternative, 2006
 Disorder, Joy Division, Ian Curtis, Unknown Pleasures, Alternative, 1979
 VCR, The xx, Jamie xx, xx, Indie, 2010
 Pictures of You, The Cure, Robert Smith, Disintegration, Alternative Rock, 1990
 There is a light that never goes out, The Smiths, Morrissey,  The Queen Is Dead, Alternative Rock, 1992
 Especially Me, Low, Alan Sparhawk, C'mon, Indie Rock, 2010
 Wrath of God, Crystal Castles, Alice Glass, III, Electronic, 2010
 Lover You shouldn't come over, Jeff Buckley, Jeff Buckley, Grace, Alternative Rock, 1994
 Ingenue, Atoms for Peace, Thom Yorke, Amok, Electronic Rock, 2013
 After the Golden Rush, Neil Young, Neil Young, Deja Vu, Folk Rock, 1970
 Arab hertz Club, 80 kidz, 80 kidz, This is my shit, Electronic, 2009
 Sleeping Sickness, City and Colour, Dallas Green,  Bring Me Your Love, Alternative Rock, 2008
 Something, The Beatles, Paul George Harrison, Abbey Road, Rock, 1969
 Re:stacks, Bon Iver, Justin Vernon, For Emma, Forever Ago, Folk Rock, For Emma Forever Ago
 Asleep, The Smiths, Morrissey, The World Won't Listen, Alternative Rock, 1987
 Hoppípolla, Sigur Ros, Jónsi, Takk, Post Rock, 2005
 I'm Happy she said, M83, Nicholas Fromageau, Oblivion, Electronic, 2005
 The Rip, Portishead, Beth Gibbons, Third, Trip hop, 2008
 Sorrow, The National, Brice Dessner, High Violet, 2010
 The memory is cruel, Russian Red, Lourdes Hernández, Fuerteventura, Folk Indie, 2010
 Radio Cure, Wilco, Jeff Tweedy, Yankee Hotel Foxtrot, Indie Rock, 2001
 All your light, Portugal the Man, John Gourley, In the Mountain in the Cloud, Psychadelic Rock, 2011
 No surprises, Radiohead, Thom Yorke, OK Computer, Alternative, 1997
 Get lucky, Daft Punk, Daft Punk, Random Access Memories, Electronic, 2013
 I'm a Loser, The Beatles, John Lennon, Beatles For Sale, Folk Rock, 1964 


It isnt getting the ID at all. Any clue?


When I try that data, it is successful as far as this:
ID, Title, Artist, Autor, Album, Genre, Year
ABCD, Idioteque, Radiohead, Thom Yorke, Kid A, Electronic, 2000
 EFGH, High Hopes, Kodaline, Stephen Garrigan, In a Perfect World, Alternative Rock, 2013
 IJKL, Home, Edward Sharpe & the Magnetic Zeros, Edward Shape & the Magnetic Zeros, Here, Indie Folk, 2012

The next line is this. It doesn't have an ID.
Sleeping Lessons, The Shins, James Mercer, Wincing the Night Away, Indie Rock, 2007
So weird. When I run it, it doesnt show those musics at all. Like the program ignores the data with ID's. So weird.

Well, there is a lot of data. If you run it with the whole file, the first items may scroll off the top of the screen and disappear.

Either way, the solution is the same, you need to add the ID to all of the lines where it is missing.
Last edited on
Indeed. I tried to delete every music without ID and let the data like this

1
2
3
4
5
 ID, Title, Artist, Autor, Album, Genre, Year
 ABCD, Idioteque, Radiohead, Thom Yorke, Kid A, Electronic, 2000
 EFGH, High Hopes, Kodaline, Stephen Garrigan, In a Perfect World, Alternative Rock, 2013
 IJKL, Home, Edward Sharpe & the Magnetic Zeros, Edward Shape & the Magnetic Zeros, Here, Indie Folk, 2012


it's working properly now. Any idea how I can make everything appear?
Well, you may be able to configure the console to have a larger buffer, in order to display all of the output.

Alternatively, you could redirect the output from the console to a file. Do this on the command line, there is no need to change the program.

A third alternative would be to change the code and have the program open an output file, and change the cout statements to send the output to that file.

One more possibility would be to add a "pause" after every few records have been output, to enable the user to view the results a page at a time.
Last edited on
Working as intended, changed the window size :)

I hope I'm not messing you, Chervil your help was pretty much amazing. I gotta do this and I started this programme 1 month later and theres a lot that I need to learn.

I need to create the class User and stuff like that but I think I can do it on my own.
Just have one more question. I'll need to create the like and dislike option. Could you please tell me how do I make a counter for that option?

Best regards
Pages: 12