LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   C++ - Store a text file in a Data Structure (http://www.linuxquestions.org/questions/programming-9/c-store-a-text-file-in-a-data-structure-728340/)

nilly16 05-25-2009 07:01 AM

C++ - Store a text file in a Data Structure
 
Hi :)

I am writting a program that reads a text file (music.txt) & stores it in a Data Structure.

I am a novice learning over the internet so I this is something I have never done.

How do I do this?

I hope that I explained this well but here is a little more info incase not
Quote:

Write a program that reads the data from the
music.txt file and store it in a data structure. To implement this data structure you must use an array of songs and each song must be represented by a struct with appropriate fields.
So far all I can do is open to file to read it (very simple I know) but so far is it correct?

Any advice on how to do this would be extremely helpful :)

Code:

#include <iostream>
#include <fstream>
#include <strstream>

using namespace std;

int main()
{
        ifstream fin("music.txt");


        if (fin.fail()) {
                cout << "Can't open file!\n";
        }


johnsfine 05-25-2009 07:18 AM

Quote:

Originally Posted by nilly16 (Post 3551903)
I am a novice learning over the internet

But you quoted from a very specific assignment, and apparently you don't understand the assignment well enough to even quote the important parts.

If that is from some online tutorial (what I would call "learning over the internet") a URL would make it easier for us to find and explain the parts you aren't understanding.

Quote:

So far all I can do is open to file to read it
To get any further, you need to know something about the structure of the text in the file and the structure of the information represented by that text.

The assignment tells you the top level of the result you want: An array, where each element of the array is a struct and each of those structs represents one song.

Below that, you haven't given us any clue what kind of information is available for each song.

nilly16 05-25-2009 09:56 PM

C++ Open + Store a Text files
 
Thanks for ur reply

Example of music.txt layout; its a list of songs aranged in the form of Artist, Song, Length(seconds). All up about 60 songs.
Quote:

AC/DC,Dirty Deeds Done Dirt Cheap,303,
Alison Moyet,All Cried Out,410,
Allan Browne Quintet,Cyclosporin,291,
The Angels,Take A Long Line,180,

pixellany 05-26-2009 07:42 AM

One of three very similar threads on the same homework assignment. See the canned respons in one of the others.

Reported for closure


All times are GMT -5. The time now is 06:11 AM.