S
Some Clown
Well... after much consultation with folks on this group, I was convinced
that using char[] arrays and such was bad. I had no clue due in no small
part to me being a complete newbie, and the fact that I've been studying an
outdated book. Anyhow... now I've been reading up on std::string, vector,
iterators, etc and have re-written my playlist editing program using these
new features. Thanks to those who've helped me along so far.
So here's the thing... everytime I read/write a file I end up with one extra
line in it. I'm thinking it's happening in the writeFile() function - one
extra '\n' at the end... but I'm not sure how to fix it. Any suggestions?
---------------- snip --------------------------
/* PlayEdit - Version 0.02
Copyright 2004 by Some Clown */
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
#include <iterator>
#include <io.h>
using namespace std;
static int lineError = 0;
class Playlist
{
private:
string fileName; // filename from main()
vector<string> lineItems; // Vector to hold lines from playlist file
string oldPath; // Old path in playlist (c:\somepath)
string newPath; // New path in playlist (f:\anotherpath)
public:
Playlist(string const& roldPath, string const& rnewPath,
string const& rfileName): fileName(rfileName),
oldPath(roldPath),
newPath(rnewPath){}
// Open playlist file and read it into vector
void getFile()
{
ifstream theFile(fileName.c_str());
if(!theFile)
{
cerr << "Error opening file!\n";
exit(1);
}
string fileLine;
while(theFile)
{
getline(theFile,fileLine);
lineItems.push_back(fileLine);
}
}
// Print contents of vector (lineItems - from playlist file)
void printFile()
{
ostream_iterator<string> out(cout, "\n");
copy(lineItems.begin(), lineItems.end(), out);
}
// Edit playlist
void editFile()
{
for(int i = 0; i < (lineItems.size() - 1); i++)
{
int pos=lineItems.find(oldPath);
if(pos != string::npos)
lineItems.replace(pos, oldPath.size(), newPath);
else
{
cout << "Line " << i+1 << " doesn't appear to contain "
<< oldPath << "!\n";
lineError++;
}
}
}
// Write vector (edited playlist) to file
void writeFile()
{
ofstream theFile(fileName.c_str());
ostream_iterator<string> out(theFile, "\n");
copy(lineItems.begin(), lineItems.end(), out);
cout << endl
<< lineItems.size() << " lines written to "
<< fileName << " (" << lineError << " errors found)\n";
}
};
int main(int argc, char* argv[]) // Need command line parameters in here
{
if(argc!=4) // Needs some work... but at least we can
{ // take filename as input to program
cerr << "\nYou are missing one of the three required options!\n";
return 0;
}
if(access(argv[3], 00)) // access returns 0 if the file can be accessed
{ // under the specified method (00)
cerr << "File does not exist";
return 0;
}
const string oldPath = argv[1];
const string newPath = argv[2];
const string fileName = argv[3]; // Pointer to passed filename (to hand
to CPlaylist class)
Playlist test(oldPath, newPath, fileName);
test.getFile();
// test.printFile();
test.editFile();
test.writeFile();
return 0;
}
that using char[] arrays and such was bad. I had no clue due in no small
part to me being a complete newbie, and the fact that I've been studying an
outdated book. Anyhow... now I've been reading up on std::string, vector,
iterators, etc and have re-written my playlist editing program using these
new features. Thanks to those who've helped me along so far.
So here's the thing... everytime I read/write a file I end up with one extra
line in it. I'm thinking it's happening in the writeFile() function - one
extra '\n' at the end... but I'm not sure how to fix it. Any suggestions?
---------------- snip --------------------------
/* PlayEdit - Version 0.02
Copyright 2004 by Some Clown */
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
#include <iterator>
#include <io.h>
using namespace std;
static int lineError = 0;
class Playlist
{
private:
string fileName; // filename from main()
vector<string> lineItems; // Vector to hold lines from playlist file
string oldPath; // Old path in playlist (c:\somepath)
string newPath; // New path in playlist (f:\anotherpath)
public:
Playlist(string const& roldPath, string const& rnewPath,
string const& rfileName): fileName(rfileName),
oldPath(roldPath),
newPath(rnewPath){}
// Open playlist file and read it into vector
void getFile()
{
ifstream theFile(fileName.c_str());
if(!theFile)
{
cerr << "Error opening file!\n";
exit(1);
}
string fileLine;
while(theFile)
{
getline(theFile,fileLine);
lineItems.push_back(fileLine);
}
}
// Print contents of vector (lineItems - from playlist file)
void printFile()
{
ostream_iterator<string> out(cout, "\n");
copy(lineItems.begin(), lineItems.end(), out);
}
// Edit playlist
void editFile()
{
for(int i = 0; i < (lineItems.size() - 1); i++)
{
int pos=lineItems.find(oldPath);
if(pos != string::npos)
lineItems.replace(pos, oldPath.size(), newPath);
else
{
cout << "Line " << i+1 << " doesn't appear to contain "
<< oldPath << "!\n";
lineError++;
}
}
}
// Write vector (edited playlist) to file
void writeFile()
{
ofstream theFile(fileName.c_str());
ostream_iterator<string> out(theFile, "\n");
copy(lineItems.begin(), lineItems.end(), out);
cout << endl
<< lineItems.size() << " lines written to "
<< fileName << " (" << lineError << " errors found)\n";
}
};
int main(int argc, char* argv[]) // Need command line parameters in here
{
if(argc!=4) // Needs some work... but at least we can
{ // take filename as input to program
cerr << "\nYou are missing one of the three required options!\n";
return 0;
}
if(access(argv[3], 00)) // access returns 0 if the file can be accessed
{ // under the specified method (00)
cerr << "File does not exist";
return 0;
}
const string oldPath = argv[1];
const string newPath = argv[2];
const string fileName = argv[3]; // Pointer to passed filename (to hand
to CPlaylist class)
Playlist test(oldPath, newPath, fileName);
test.getFile();
// test.printFile();
test.editFile();
test.writeFile();
return 0;
}