One extra line every time...

Discussion in 'C++' started by Some Clown, Feb 20, 2004.

  1. Some Clown

    Some Clown Guest

    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;
    }
     
    Some Clown, Feb 20, 2004
    #1
    1. Advertising

  2. Some Clown

    Sharad Kala Guest

    "Some Clown" <> wrote in message
    news:...
    > 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
     
    Sharad Kala, Feb 20, 2004
    #2
    1. Advertising

  3. "Sharad Kala" <> wrote...
    >
    > "Some Clown" <> wrote in message
    > news:...
    > > [... 5 kilobytes of stuff removed ...]

    > http://www.parashift.com/c -faq-lite/input-output.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.
     
    Victor Bazarov, Feb 20, 2004
    #3
  4. Some Clown

    Jeff Schwab Guest

    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 );
    }
     
    Jeff Schwab, Feb 20, 2004
    #4
  5. Some Clown

    Sharad Kala Guest

    "Victor Bazarov" <> wrote in message
    news:G5gZb.362178$na.549047@attbi_s04...
    > "Sharad Kala" <> wrote...
    > >
    > > "Some Clown" <> wrote in message
    > > news:...
    > > > [... 5 kilobytes of stuff removed ...]

    > > http://www.parashift.com/c -faq-lite/input-output.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.
     
    Sharad Kala, Feb 20, 2004
    #5
  6. Some Clown

    Some Clown Guest

    "Jeff Schwab" <> wrote in message
    news:eek:...
    > 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.
     
    Some Clown, Feb 20, 2004
    #6
  7. Some Clown

    Gary Guest

    "Some Clown" <> wrote in message
    news:...
    > "Jeff Schwab" <> wrote in message
    > news:eek:...
    > > 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
     
    Gary, Feb 20, 2004
    #7
  8. Some Clown

    Jorge Rivera Guest


    >
    > 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'));
     
    Jorge Rivera, Feb 21, 2004
    #8
  9. Some Clown

    Hung Tran Guest

    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)...
     
    Hung Tran, Feb 21, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.frontiernet.net
    Replies:
    6
    Views:
    1,153
    news.frontiernet.net
    Apr 16, 2004
  2. liking C lang.

    May output the char one after one every other second?

    liking C lang., Feb 16, 2007, in forum: C Programming
    Replies:
    16
    Views:
    571
    Keith Thompson
    Feb 17, 2007
  3. =?Utf-8?B?SXJ3YW5zeWFo?=
    Replies:
    4
    Views:
    2,481
    =?Utf-8?B?SXJ3YW5zeWFo?=
    Oct 30, 2007
  4. mathieu
    Replies:
    3
    Views:
    623
    Bo Persson
    Sep 4, 2009
  5. Merciadri Luca
    Replies:
    4
    Views:
    841
Loading...

Share This Page