Doubt with std::vector

Discussion in 'C++' started by robertoviperbr@gmail.com, Nov 17, 2006.

  1. Guest

    Hello to everybody.
    I have a doubt see the code:

    #include <vector>
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <sstream>
    #include <fstream>
    #include <iomanip>
    #include <iterator>

    using namespace std;

    class Test
    {
    private:
    vector<int>Number;
    string szFileNameInput;
    string szFileNameOutPut;
    static long posx;
    ...
    public:
    Test() : Number(15),szFileNameInput(""),szFileNameOutPut(""){};
    ~Test(){};
    int ReadFileInput(const string& file, vector<int>& ref);
    int WriteFileOutPut(const string& file, vector<int>& ref);
    };

    long Test::posx = 0;


    This code opens an archive with 126 lines with disordered numbers, for
    example:
    01 12 45 88 99 35 66

    and I am trying to make:

    int Test::ReadFileInput(const string& file,vector<int>& ref)
    {
    try
    {
    ifstream in(file.c_str());
    if(!in) throw string("Error 1: ");
    string temp;
    in.seekg(input,ios::beg);
    getline(in,temp,'\n');
    input = in.tellg();
    in.close();
    in.clear();
    istringstream is(temp);
    istream_iterator<int>ini(is),fim;
    vector<int>num(ini,fim);
    copy(num.begin(),num.end(),ref.begin());
    sort(ref.begin(),ref.end());
    temp.clear();
    return 0;
    }
    catch(string msg)
    {
    cout<< msg << file<< endl;
    cin.get();
    exit(1);
    }
    }

    int Test::WriteFileOutput(const string& file,vector<int>& ref)
    {
    try
    {
    ofstream off(file.c_str(),ios::app);
    if(!off) throw string("Error 2: ");
    for(unsigned int i = 0; i < NumerosPorSorteio; i++)
    {
    off << setfill('0') << setw(2) << ref << " ";
    }
    off << endl;
    off.close();
    ref.clear();
    return 0;
    }
    catch(string msg)
    {
    cout<< msg << file << endl;
    cin.get();
    exit(1);
    }
    }

    But in no line value "00" exists but it appears after the line number
    50.
    What I am making of made a mistake?

    Thanks a lot for help.
    , Nov 17, 2006
    #1
    1. Advertising

  2. Salt_Peter Guest

    wrote:
    > Hello to everybody.
    > I have a doubt see the code:
    >
    > #include <vector>
    > #include <iostream>
    > #include <string>
    > #include <algorithm>
    > #include <sstream>
    > #include <fstream>
    > #include <iomanip>
    > #include <iterator>
    >
    > using namespace std;
    >
    > class Test
    > {
    > private:
    > vector<int>Number;
    > string szFileNameInput;
    > string szFileNameOutPut;
    > static long posx;
    > ...
    > public:
    > Test() : Number(15),szFileNameInput(""),szFileNameOutPut(""){};
    > ~Test(){};
    > int ReadFileInput(const string& file, vector<int>& ref);
    > int WriteFileOutPut(const string& file, vector<int>& ref);
    > };
    >
    > long Test::posx = 0;
    >
    >
    > This code opens an archive with 126 lines with disordered numbers, for
    > example:
    > 01 12 45 88 99 35 66
    >
    > and I am trying to make:
    >
    > int Test::ReadFileInput(const string& file,vector<int>& ref)
    > {
    > try
    > {
    > ifstream in(file.c_str());
    > if(!in) throw string("Error 1: ");
    > string temp;
    > in.seekg(input,ios::beg);
    > getline(in,temp,'\n');
    > input = in.tellg();
    > in.close();
    > in.clear();
    > istringstream is(temp);
    > istream_iterator<int>ini(is),fim;
    > vector<int>num(ini,fim);
    > copy(num.begin(),num.end(),ref.begin());
    > sort(ref.begin(),ref.end());
    > temp.clear();
    > return 0;
    > }
    > catch(string msg)
    > {
    > cout<< msg << file<< endl;
    > cin.get();
    > exit(1);
    > }
    > }
    >
    > int Test::WriteFileOutput(const string& file,vector<int>& ref)
    > {
    > try
    > {
    > ofstream off(file.c_str(),ios::app);
    > if(!off) throw string("Error 2: ");
    > for(unsigned int i = 0; i < NumerosPorSorteio; i++)
    > {
    > off << setfill('0') << setw(2) << ref << " ";
    > }
    > off << endl;
    > off.close();
    > ref.clear();
    > return 0;
    > }
    > catch(string msg)
    > {
    > cout<< msg << file << endl;
    > cin.get();
    > exit(1);
    > }
    > }
    >
    > But in no line value "00" exists but it appears after the line number
    > 50.
    > What I am making of made a mistake?
    >


    Does it make sense to you that newsgroup readers need to be asking
    questions like:
    What is NumerosPorSorteio?
    In ReadFileInput(...), why is input undefined?
    Why is vector Number a member of the class? With a size of 15?
    Why are you sorting a vector when a std::set orders elements
    automatically?
    In WriteFileOutput(...), why are you appending to file?
    Since you are passing by reference the filenames to be read and
    written, why are you storing them in the class?
    Salt_Peter, Nov 17, 2006
    #2
    1. Advertising

  3. Guest

    This was only one simple example of the problem that I am passing, the
    fact is that NumerosPorSorteio was written in my native
    language(pt_BR), it means Numbers for drawing, only this. Yes, I am
    passing the names of the archives inside of another function so that
    the user declares them. In WriteFileOutput() I it adds each interaction
    of reading of the vector for an archive of exit already declared by the
    user. I go to see its suggestion on the method set() to see where I am
    made a mistake, remembering that still I am learning C++, then errors
    are common.
    I thank the help.

    Salt_Peter wrote:
    > wrote:
    > > Hello to everybody.
    > > I have a doubt see the code:
    > >
    > > #include <vector>
    > > #include <iostream>
    > > #include <string>
    > > #include <algorithm>
    > > #include <sstream>
    > > #include <fstream>
    > > #include <iomanip>
    > > #include <iterator>
    > >
    > > using namespace std;
    > >
    > > class Test
    > > {
    > > private:
    > > vector<int>Number;
    > > string szFileNameInput;
    > > string szFileNameOutPut;
    > > static long posx;
    > > ...
    > > public:
    > > Test() : Number(15),szFileNameInput(""),szFileNameOutPut(""){};
    > > ~Test(){};
    > > int ReadFileInput(const string& file, vector<int>& ref);
    > > int WriteFileOutPut(const string& file, vector<int>& ref);
    > > };
    > >
    > > long Test::posx = 0;
    > >
    > >
    > > This code opens an archive with 126 lines with disordered numbers, for
    > > example:
    > > 01 12 45 88 99 35 66
    > >
    > > and I am trying to make:
    > >
    > > int Test::ReadFileInput(const string& file,vector<int>& ref)
    > > {
    > > try
    > > {
    > > ifstream in(file.c_str());
    > > if(!in) throw string("Error 1: ");
    > > string temp;
    > > in.seekg(input,ios::beg);
    > > getline(in,temp,'\n');
    > > input = in.tellg();
    > > in.close();
    > > in.clear();
    > > istringstream is(temp);
    > > istream_iterator<int>ini(is),fim;
    > > vector<int>num(ini,fim);
    > > copy(num.begin(),num.end(),ref.begin());
    > > sort(ref.begin(),ref.end());
    > > temp.clear();
    > > return 0;
    > > }
    > > catch(string msg)
    > > {
    > > cout<< msg << file<< endl;
    > > cin.get();
    > > exit(1);
    > > }
    > > }
    > >
    > > int Test::WriteFileOutput(const string& file,vector<int>& ref)
    > > {
    > > try
    > > {
    > > ofstream off(file.c_str(),ios::app);
    > > if(!off) throw string("Error 2: ");
    > > for(unsigned int i = 0; i < NumerosPorSorteio; i++)
    > > {
    > > off << setfill('0') << setw(2) << ref << " ";
    > > }
    > > off << endl;
    > > off.close();
    > > ref.clear();
    > > return 0;
    > > }
    > > catch(string msg)
    > > {
    > > cout<< msg << file << endl;
    > > cin.get();
    > > exit(1);
    > > }
    > > }
    > >
    > > But in no line value "00" exists but it appears after the line number
    > > 50.
    > > What I am making of made a mistake?
    > >

    >
    > Does it make sense to you that newsgroup readers need to be asking
    > questions like:
    > What is NumerosPorSorteio?
    > In ReadFileInput(...), why is input undefined?
    > Why is vector Number a member of the class? With a size of 15?
    > Why are you sorting a vector when a std::set orders elements
    > automatically?
    > In WriteFileOutput(...), why are you appending to file?
    > Since you are passing by reference the filenames to be read and
    > written, why are you storing them in the class?
    , Nov 17, 2006
    #3
    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. Peter Jansson
    Replies:
    5
    Views:
    6,294
    Ivan Vecerina
    Mar 17, 2005
  2. Anonymous
    Replies:
    20
    Views:
    4,288
    Pete Becker
    Mar 30, 2005
  3. Jason Heyes
    Replies:
    8
    Views:
    717
    Andrew Koenig
    Jan 15, 2006
  4. Replies:
    8
    Views:
    1,912
    Csaba
    Feb 18, 2006
  5. Rune Allnor
    Replies:
    4
    Views:
    940
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page