Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Doubt with std::vector

Reply
Thread Tools

Doubt with std::vector

 
 
robertoviperbr@gmail.com
Guest
Posts: n/a
 
      11-17-2006
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:osx = 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.

 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      11-17-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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:osx = 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?

 
Reply With Quote
 
 
 
 
robertoviperbr@gmail.com
Guest
Posts: n/a
 
      11-17-2006
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:
> (E-Mail Removed) 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:osx = 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?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
doubt Ajith Nair ASP .Net 6 03-29-2005 01:25 PM
doubt about namespace google manasa sreenivas via .NET 247 ASP .Net 1 05-15-2004 03:05 PM
Doubt to go .Net technology Igbal ASP .Net 1 11-17-2003 06:56 AM



Advertisments