istrean Vs ifstream

M

malla

If I want to use istream rather than ifstream, the following program
does not work. any suggestions anyone? I want to read my data in any
given stream (and not just through a file or something). I changed my
previous code to the following, which is not
working:

#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;


class SensorAnalysis{


private:
public:
SensorAnalysis::SensorAnalysis();
void SensorAnalysis::rBuff(istream &buff);



};


SensorAnalysis::SensorAnalysis(){


}


void SensorAnalysis::rBuff(istream &buff){
cout<< "here\n";
if(buff){
string str;
while(getline(buff, str)) {
cout << str << endl;
}
buff.close();
}
else{
cout<<"retard\n";
}


}


int main()
{
SensorAnalysis sense;
istream file("1.txt");
sense.rBuff(file);
return 0;
}
 
E

Eric Pruneau

malla said:
If I want to use istream rather than ifstream, the following program
does not work. any suggestions anyone? I want to read my data in any
given stream (and not just through a file or something). I changed my
previous code to the following, which is not
working:


That is the second time you ask this question...

read the answer I gave you the first time...


eric
 
M

mlimber

malla said:
If I want to use istream rather than ifstream, the following program
does not work. any suggestions anyone?

You'll have to be more descriptive. In what way does it "not work"?
Does it compile? (Answer: no.)
I want to read my data in any
given stream (and not just through a file or something). I changed my
previous code to the following, which is not
working:

#include <iostream>
#include <sstream>

Not used.
#include <istream>
Redundant.

#include <fstream>

Not used.
#include <string>
using namespace std;


class SensorAnalysis{


private:
public:
SensorAnalysis::SensorAnalysis();
void SensorAnalysis::rBuff(istream &buff);



};


SensorAnalysis::SensorAnalysis(){


}


void SensorAnalysis::rBuff(istream &buff){
cout<< "here\n";
if(buff){
string str;
while(getline(buff, str)) {
cout << str << endl;
}
buff.close();

First, why open it outside this function but close it inside? Prefer to
do both in one place. Second, istream does not have a close member
function.
}
else{
cout<<"retard\n";

Prefer the more sensitive:

cout << "silly\n";
}


}


int main()
{
SensorAnalysis sense;
istream file("1.txt");

Did you mean ifstream? Probably since this won't compile.
sense.rBuff(file);
return 0;
}

Cheers! --M
 
T

Thomas J. Gritzan

malla said:
If I want to use istream rather than ifstream,

Why do you want to use istream? If you want to open a file, you want to
use ifstream.
the following program
does not work. any suggestions anyone? I want to read my data in any
given stream (and not just through a file or something). I changed my
previous code to the following, which is not
working:

ifstream actually _is_ an istream.

Read about inheritance in your favorite C++ book.

[...]
void SensorAnalysis::rBuff(istream &buff){
cout<< "here\n";
if(buff){
string str;
while(getline(buff, str)) {
cout << str << endl;
}
buff.close();

You can't close an istream object. Remove this line.
}
else{
cout<<"retard\n";
}


}


int main()
{
SensorAnalysis sense;
istream file("1.txt");

You can't open a file with istream. You have to use ifstream.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top