473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

One extra line every time...

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
8 2274

"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
"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
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

"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
"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
"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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joshua Beall | last post by:
Hi all, I have the following message string: $message = <<<EOT Personal Information: Name: {$_POST} Address: {$_POST} EOT;
5
by: Brian | last post by:
Hello group, http://people.umass.edu/btrembla/ (html 4.01/strict) uses a conventional menu marked up as a list, and css http://people.umass.edu/btrembla/css/brian.css...
10
by: lkrubner | last post by:
I killed last night and a good chunk of today trying to figure out this one particular attempt to get a class and initialize it. My code is using a class method called getObject to include() a file...
2
by: michael | last post by:
On my ASP.NET pages, it seems that _after_ any web or user control - an extra (small) line break or space is inserted automatically right after the Control. This is unwanted since it messes up...
5
by: dw | last post by:
hello - first, let me state that i am an Asp.Net rookie. here is the situation: i have a page that looks good in the vs.net designer, but when the page renders there are extra amounts...
2
by: ricky | last post by:
Can anybody help with the function to get rid of extra characters in the file. I want to remove the string from the file.So i read from input file and pass the string say "john" if found dnt write...
19
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
This is an example of the data; 2007/07/27 11:00:03 ARES_INDICATION 010.050.016.002 404.2.01 (6511) RX 74 bytes 2007/07/27 11:00:03 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48...
6
by: jeffg | last post by:
I have created a site that uses a Doctype of XHTML Transitional and a character set of utf-8, in case any of this matters. I have validated every page and cleaned up all errors. IE displays the...
2
by: tom0550 | last post by:
Around December 2008 our MS Access 2003 application started inserting extra line feeds everytime the RTF edit window was opened and saved. It was working fine up to that time. We are using the MS...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.