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

Doubt with std::vector

P: n/a
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[i] << " ";
}
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 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

ro************@gmail.com 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[i] << " ";
}
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 '06 #2

P: n/a
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:
ro************@gmail.com 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[i] << " ";
}
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 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.