Standard Output

B

ben

Hi guyz,
I want to know how to send output to screen(i.e. standard
o/p)in UNIX using streams.

I am reading a file char. by char. and i want to print it ultimately on
the screen.I know how to achieve this using UNIX system APIs(make use
of read and write functions)But i want to do so using C++ streams.

bye,
ben
 
J

Jack Klein

Hi guyz,
I want to know how to send output to screen(i.e. standard
o/p)in UNIX using streams.

I am reading a file char. by char. and i want to print it ultimately on
the screen.I know how to achieve this using UNIX system APIs(make use
of read and write functions)But i want to do so using C++ streams.

bye,
ben

Either you want to use UNIX system APIs, or you want to use C++
streams. You can do one or the other, you can't do both.

If you want to use UNIX system APIs, ask about them in
They are completely off-topic here.

If you want to use a C++ stream to send data to the standard output,
which might or might not be a video display device, open your C++
reference book and look up "cout".
 
W

WittyGuy

Hi Ben,
If you want to just read-out the existing file you can use C++
filestream (illustrated in the following prg though error handling is
not present here).

/*** Sample File stream handling program ***/
#include <iostream>
#include<fstream>

using namespace std;

int main() {
char buffer[100];
fstream in_file("read.txt",ios::in);
while(!in_file.eof()) {
in_file.getline(buffer, 100);
cout<<buffer<<endl;
}
}
 
S

Shezan Baig

WittyGuy said:
Hi Ben,
If you want to just read-out the existing file you can use C++
filestream (illustrated in the following prg though error handling is
not present here).

/*** Sample File stream handling program ***/
#include <iostream>
#include<fstream>

using namespace std;

int main() {
char buffer[100];
fstream in_file("read.txt",ios::in);
while(!in_file.eof()) {
in_file.getline(buffer, 100);
cout<<buffer<<endl;
}
}



Or, to put it simply:

fstream in_file("read.txt", ios::in);
cout << in_file.rdbuf();

Hope this helps,
-shez-
 
A

abecedarian

Shezan said:
Or, to put it simply:

fstream in_file("read.txt", ios::in);
cout << in_file.rdbuf();

.... omitting any error handling, of course. Who cares?
 
B

ben

WittyGuy said:
Hi Ben,
If you want to just read-out the existing file you can use C++
filestream (illustrated in the following prg though error handling is
not present here).

/*** Sample File stream handling program ***/
#include <iostream>
#include<fstream>

using namespace std;

int main() {
char buffer[100];
fstream in_file("read.txt",ios::in);
while(!in_file.eof()) {
in_file.getline(buffer, 100);
cout<<buffer<<endl;
}
}

Thanks for the help.You solved my problem.
bye,
ben
 
R

red floyd

ben said:
WittyGuy said:
Hi Ben,
If you want to just read-out the existing file you can use C++
filestream (illustrated in the following prg though error handling is
not present here).

/*** Sample File stream handling program ***/
#include <iostream>
#include<fstream>

using namespace std;

int main() {
char buffer[100];
fstream in_file("read.txt",ios::in);
while(!in_file.eof()) {
in_file.getline(buffer, 100);
cout<<buffer<<endl;
}
}

No, no, no!!! See the FAQ for what's wrong with your file handling.
istream::eof() doesn't return true if you're about to hit EOF, it
returns true if you already have hit EOF!

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
string buffer;
fstream in_file("read.txt", ios::in);
while (getline(in_file, buffer))
cout << buffer << endl;
return EXIT_SUCCESS;
}
 

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
474,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top