S
Some Clown
Greetings,
I am very, very new to C++ and am working my way through a book entitled,
"Beginning Visual C++" by Ivor Horton. Decent book except for the guy's
almost fetish-like love for complex math examples. I spend half my time
deciphering the math before I can even begin to get into the sematics of the
language in his examples. That aside though... I do have a couple of
questions, and they don't relate to Visual C++ specifically.
I've gotten through the first few chapters (9 or so) and am in the middle of
learning 'classes' right now. I thought I'd take a slight pause and try to
apply some of what I've learned (or not as the case may be) to a silly
little program. I am trying to write a program to take a text file playlist
(*.m3u) with a bunch of filenames/paths seperated by newlines and edit it.
It will eventually except command line switches, though I'm far from that
stage right now. It just needs to edit the file and save it under a
different name. so C:\somefile would become F:\somefile (or something like
that).
I have a member function called getFile() and another one called printFile()
that both work as expected with one major exception. In printfile() when I
try to loop through my lineItems[] array I get jibberish (probably because
I'm going past the filled in data). I can specify specifically lineItems[5]
or something and get what's expected, and the commented out loop in
getFile() works as well.
So my question might be, "How to do figure out how much of my array is
filled up and either truncate or get a count of items, so that I don't run
past the end?" I've included source code below, and I'm hoping some of you
experienced folks can give me some pointers. I'd also take pointers on
anything else wrong with my code, though keep in mind, I'm so new to this I
may not understand what you're saying [is wrong]. This is definitely a work
in progress, but that being said, I'm having great fun with it!
Thanks in advance!
---------------------- code --------------------------------------
// Playlist Edit - Version 0.1
// Copyright 2004 by Some Clown
#include <fstream.h>
#include <iostream>
const int MAX = 80; // Maximum number of characters in filename
const int MAX2 = 80; // Maximum number of songs in a playlist
const int maxPath = 200; // Maximum length of path in playlist file
class CPlaylist
{
private:
char* pfileName; // Our filename as passed from main()
char lineItems[MAX2][maxPath];
public:
// Default Constructor
CPlaylist(char* pfn): pfileName(pfn){}
// Default Destructor
~CPlaylist()
{
}
// Function to open a playlist file and read it into an array
void getFile()
{
ifstream theFile(pfileName, ios::nocreate);
if(!theFile)
{
cout << endl
<< "Error opening file!"
<< endl;
exit(1);
}
for(int i = 0; ((i <=MAX2) && (!theFile.eof())); i++)
{
theFile.getline(lineItems, maxPath);
// cout << endl // just for testing
// << lineItems;
}
theFile.close(); // Might need to move this somewhere else once all of the
other
// functions are complete. Or put in some error checking to see
// if file is already closed or not.
}
// Function to edit a playlist file
void editFile()
{
}
// Function to print contents of array containing playlist
void printFile()
{
for(int i = 0; i <=MAX2; i++)
{
cout << endl
<< lineItems;
}
}
};
void main()
{
char fileName[MAX];
cout << endl
<< "Please enter a playlist name: ";
cin.getline(fileName, MAX);
CPlaylist test(fileName);
test.getFile();
test.printFile();
return;
}
I am very, very new to C++ and am working my way through a book entitled,
"Beginning Visual C++" by Ivor Horton. Decent book except for the guy's
almost fetish-like love for complex math examples. I spend half my time
deciphering the math before I can even begin to get into the sematics of the
language in his examples. That aside though... I do have a couple of
questions, and they don't relate to Visual C++ specifically.
I've gotten through the first few chapters (9 or so) and am in the middle of
learning 'classes' right now. I thought I'd take a slight pause and try to
apply some of what I've learned (or not as the case may be) to a silly
little program. I am trying to write a program to take a text file playlist
(*.m3u) with a bunch of filenames/paths seperated by newlines and edit it.
It will eventually except command line switches, though I'm far from that
stage right now. It just needs to edit the file and save it under a
different name. so C:\somefile would become F:\somefile (or something like
that).
I have a member function called getFile() and another one called printFile()
that both work as expected with one major exception. In printfile() when I
try to loop through my lineItems[] array I get jibberish (probably because
I'm going past the filled in data). I can specify specifically lineItems[5]
or something and get what's expected, and the commented out loop in
getFile() works as well.
So my question might be, "How to do figure out how much of my array is
filled up and either truncate or get a count of items, so that I don't run
past the end?" I've included source code below, and I'm hoping some of you
experienced folks can give me some pointers. I'd also take pointers on
anything else wrong with my code, though keep in mind, I'm so new to this I
may not understand what you're saying [is wrong]. This is definitely a work
in progress, but that being said, I'm having great fun with it!
Thanks in advance!
---------------------- code --------------------------------------
// Playlist Edit - Version 0.1
// Copyright 2004 by Some Clown
#include <fstream.h>
#include <iostream>
const int MAX = 80; // Maximum number of characters in filename
const int MAX2 = 80; // Maximum number of songs in a playlist
const int maxPath = 200; // Maximum length of path in playlist file
class CPlaylist
{
private:
char* pfileName; // Our filename as passed from main()
char lineItems[MAX2][maxPath];
public:
// Default Constructor
CPlaylist(char* pfn): pfileName(pfn){}
// Default Destructor
~CPlaylist()
{
}
// Function to open a playlist file and read it into an array
void getFile()
{
ifstream theFile(pfileName, ios::nocreate);
if(!theFile)
{
cout << endl
<< "Error opening file!"
<< endl;
exit(1);
}
for(int i = 0; ((i <=MAX2) && (!theFile.eof())); i++)
{
theFile.getline(lineItems, maxPath);
// cout << endl // just for testing
// << lineItems;
}
theFile.close(); // Might need to move this somewhere else once all of the
other
// functions are complete. Or put in some error checking to see
// if file is already closed or not.
}
// Function to edit a playlist file
void editFile()
{
}
// Function to print contents of array containing playlist
void printFile()
{
for(int i = 0; i <=MAX2; i++)
{
cout << endl
<< lineItems;
}
}
};
void main()
{
char fileName[MAX];
cout << endl
<< "Please enter a playlist name: ";
cin.getline(fileName, MAX);
CPlaylist test(fileName);
test.getFile();
test.printFile();
return;
}