By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,906 Members | 1,771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,906 IT Pros & Developers. It's quick & easy.

One extra line every time...

P: n/a
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[i].find(oldPath);
if(pos != string::npos)
lineItems[i].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;
}
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"Some Clown" <no***@nowhere.net> wrote in message
news:Zq********************@comcast.com...
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[i].find(oldPath);
if(pos != string::npos)
lineItems[i].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-lit....html#faq-15.5
Jul 22 '05 #2

P: n/a
"Sharad Kala" <no*****************@yahoo.com> wrote...

"Some Clown" <no***@nowhere.net> wrote in message
news:Zq********************@comcast.com...
[... 5 kilobytes of stuff removed ...]

http://www.parashift.com/c++-faq-lit....html#faq-15.5


Sharad,

I commend you for directing the OP to the FAQ. But perhaps
you will in the future refrain from quoting the entire message
when it's totally unnecessary. Thank you.
Jul 22 '05 #3

P: n/a
Some Clown wrote:
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 );
}

Jul 22 '05 #4

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:G5gZb.362178$na.549047@attbi_s04...
"Sharad Kala" <no*****************@yahoo.com> wrote...

"Some Clown" <no***@nowhere.net> wrote in message
news:Zq********************@comcast.com...
[... 5 kilobytes of stuff removed ...]

http://www.parashift.com/c++-faq-lit....html#faq-15.5


Sharad,

I commend you for directing the OP to the FAQ. But perhaps
you will in the future refrain from quoting the entire message
when it's totally unnecessary. Thank you.


Whatever you say Sir :-)
Will surely take care in future.
Jul 22 '05 #5

P: n/a
"Jeff Schwab" <je******@comcast.net> wrote in message
news:oI********************@comcast.com...
Some Clown wrote:
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.
Jul 22 '05 #6

P: n/a
"Some Clown" <no***@nowhere.net> wrote in message
news:Rv********************@comcast.com...
"Jeff Schwab" <je******@comcast.net> wrote in message
news:oI********************@comcast.com...
Some Clown wrote:
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.


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


Jul 22 '05 #7

P: n/a

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'));
Jul 22 '05 #8

P: n/a
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[i].find(oldPath);
if(pos != string::npos)
lineItems[i].replace(pos, oldPath.size(), newPath);
else
{
cout << "Line " << i+1 << " doesn't appear to contain "
<< oldPath << "!\n";
lineError++;
}
}
}
(snip)...
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.