One extra line every time...

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;
}
 
S

Sharad Kala

Some Clown said:
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;
}

http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5
 
J

Jeff Schwab

Some said:
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 --------------------------
while(theFile)
{
getline(theFile,fileLine);
lineItems.push_back(fileLine);
}

Whoops! That should be:

while( getline( theFile, fileLine ) )
{
lineItems.push_back( fileLine );
}
 
S

Some Clown

Jeff Schwab said:
Some said:
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 --------------------------
while(theFile)
{
getline(theFile,fileLine);
lineItems.push_back(fileLine);
}

Whoops! That should be:

while( getline( theFile, fileLine ) )
{
lineItems.push_back( fileLine );
}

Indeed! Thank you very much... that certainly fixed it right up. And to
think... I was about to go off and figure out a way to strip out 'n' number
of newlines... lol. I'll get this down yet.
 
G

Gary

Some Clown said:
Jeff Schwab said:
Some said:
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

Whoops! That should be:

while( getline( theFile, fileLine ) )
{
lineItems.push_back( fileLine );
}

Indeed! Thank you very much... that certainly fixed it right up. And to
think... I was about to go off and figure out a way to strip out 'n' number
of newlines... lol. I'll get this down yet.

Be sure to check if the getline bug isn't on your system. Are you using MS
VC++?
***quote from http://www.dinkumware.com/vc_fixes.html
Fix to <istream>
The header <istream> contains a definition for member function
basic_istream::getline. It has a lookahead problem -- typing a delimiter to
end the input doesn't return control until you type yet another character.
Change the code as indicated by the comment:

else if (_C == _Di)
{++_Chcount;
rdbuf()->snextc(); // replace snextc with sbumpc
break; }Note that V6.0 replaces snextc with stossc. It is an
adequate fix, but you can also apply the above patch safely.

***end quote

There's a similar bug in <string>.
 
J

Jorge Rivera

Indeed! Thank you very much... that certainly fixed it right up. And to
think... I was about to go off and figure out a way to strip out 'n' number
of newlines... lol. I'll get this down yet.

In case you ever need it (You may want to add some checks....)
fileLine = fileLine.substr(0, fileLine.find_last_not_of('n'));
 
H

Hung Tran

To make your code complete, you need to change the condition in the
editFile() function too. I commented out the line that need to be
fixed.
(snip)...
// Edit playlist
void editFile()
{
// this line is wrong!
// for(int i = 0; i < (lineItems.size() - 1); i++)
for(int i = 0; i < lineItems.size(); i++) // this is correct.
{
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++;
}
}
}
(snip)...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top