Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-25-2009, 06:01 AM   #1
LQ Newbie
Registered: May 2009
Posts: 5

Rep: Reputation: 0
C++ - Store a text file in a Data Structure


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
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

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

using namespace std;

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

	if ( {
		cout << "Can't open file!\n";
Old 05-25-2009, 06:18 AM   #2
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
Originally Posted by nilly16 View Post
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.

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.

Last edited by johnsfine; 05-25-2009 at 06:20 AM.
Old 05-25-2009, 08:56 PM   #3
LQ Newbie
Registered: May 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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.
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,

Last edited by nilly16; 05-25-2009 at 09:02 PM.
Old 05-26-2009, 06:42 AM   #4
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

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

Reported for closure


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
small-endian to big-endian conversion of data to store in a structure NancyT Programming 2 11-26-2008 10:06 AM
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 07:08 AM
Retreiving data from a 60G text file? blackout Linux - Server 1 02-09-2008 10:07 PM
inserting the data thru php in a text file suchi_s Programming 5 02-02-2005 03:28 AM
perl - writing a data structure to a file.. is it possible? paavaka Programming 4 07-14-2001 12:04 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:19 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration