LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

Rep: Reputation: 0
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";
	}
 
Old 05-25-2009, 07:18 AM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,125

Rep: Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119
Quote:
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.

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.

Last edited by johnsfine; 05-25-2009 at 07:20 AM.
 
Old 05-25-2009, 09:56 PM   #3
nilly16
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.
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,

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

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

Reported for closure
 
  


Reply


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 11:06 AM
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 08:08 AM
Retreiving data from a 60G text file? blackout Linux - Server 1 02-09-2008 11:07 PM
inserting the data thru php in a text file suchi_s Programming 5 02-02-2005 04:28 AM
perl - writing a data structure to a file.. is it possible? paavaka Programming 4 07-14-2001 01:04 AM


All times are GMT -5. The time now is 05:54 AM.

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