Doubt with std::vector

R

robertoviperbr

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

Salt_Peter

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?
 
R

robertoviperbr

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 said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top