473,769 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ifstream from stdin

How can I use a ifstream object to input from stdin?

Ex.
ifstream inf;
if (fn=="-")
{ what here to use inf as cin?
}
else
{ inf.open(fn.c_s tr,ios::in);
}
inf >...

Thanks.
Jul 12 '07 #1
4 8720
Paulo da Silva wrote:
How can I use a ifstream object to input from stdin?
On some systems, opening "/dev/stdin" might work, but the general solution
is to use a pointer:
>
Ex.
ifstream inf;
istream *inf;
if (fn=="-")
{ what here to use inf as cin?
inf = &cin;
}
else
{ inf.open(fn.c_s tr,ios::in);
inf = new ifstream(fn.c_s tr());
}
inf >...
*inf >...

....

if (inf == &cin)
delete inf;
--
rbh
Jul 12 '07 #2
Robert Bauck Hamar wrote:
Paulo da Silva wrote:
>How can I use a ifstream object to input from stdin?

On some systems, opening "/dev/stdin" might work, but the general solution
is to use a pointer:
>>
Ex.
ifstream inf;

istream *inf;
>if (fn=="-")
{ what here to use inf as cin?

inf = &cin;
>}
else
{ inf.open(fn.c_s tr,ios::in);

inf = new ifstream(fn.c_s tr());
>}
inf >...

*inf >...

...

if (inf == &cin)
Just a nit. I think, you want:

if ( inf != &cin )
delete inf;

Best

Kai-Uwe Bux

Jul 12 '07 #3
Robert Bauck Hamar escreveu:
Paulo da Silva wrote:
>How can I use a ifstream object to input from stdin?

On some systems, opening "/dev/stdin" might work, but the general solution
is to use a pointer:
>Ex.
ifstream inf;

istream *inf;
>if (fn=="-")
{ what here to use inf as cin?

inf = &cin;
>}
else
{ inf.open(fn.c_s tr,ios::in);

inf = new ifstream(fn.c_s tr());
>}
inf >...

*inf >...

...

if (inf == &cin)
!=
delete inf;

Thank you. This works ...
Jul 12 '07 #4
On Jul 12, 6:24 pm, Robert Bauck Hamar <roberth+n...@i fi.uio.no>
wrote:
Paulo da Silva wrote:
How can I use a ifstream object to input from stdin?
On some systems, opening "/dev/stdin" might work, but the general solution
is to use a pointer:
Or a reference.
Ex.
ifstream inf;
istream *inf;
if (fn=="-")
{ what here to use inf as cin?
inf = &cin;
}
else
{ inf.open(fn.c_s tr,ios::in);
inf = new ifstream(fn.c_s tr());
}
inf >...
*inf >...
...
if (inf == &cin)
delete inf;
More frequent, I think, is something along the lines of:

if ( argc == 1 ) {
process( std::cin ) ;
} else {
for ( int i = 1 ; i < argc ; ++ i ) {
std::ifstream in( argv[ i ] ) ;
if ( ! in ) {
std::cerr << "Could not open: " << argv[ i ] <<
std::endl ;
} else {
process( in ) ;
}
}
}

All the work is then done in "process( std::istream& )".

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13779
by: Dave Johnston | last post by:
Hi, I'm currently trying to create a wrapper that uses C functions but behaves like ifstream (from fstream.h) - this is because the platform I'm using (WinCE) doesn't support streams and this is the easiest way to take a huge project across onto it. Basically, I've hit a problem. I have no idea how the ifstream class handles directories. In the code I have (which I didn't write), there are several places whereby an ifstream stream is...
6
3660
by: Herv? LEBAIL | last post by:
Hi everybody, I'm writing a program which use the <string>, <vector> and <ifstream> classes. Given an array of string, i.e vector<string> file_names, example : file_names = "file1.txt" file_names = "file2.txt" etc ...
6
3394
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber" 10,20,30
4
3738
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream in("in.txt"); if(!in) {cout << "error\n";} in.close(); if(!in) {cout << "error\n";} in.close(); if(!in) {cout << "error\n";}
10
18014
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
3
14687
by: Ralf Goertz | last post by:
Hi, consider the following program #include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv){
1
4912
by: Xiaozhou.Yin | last post by:
Hi~ In the program,I first used the ifstream variable fin open the file and,open it again after called the fin.close().But the fin is fieled to open the file in the second time.The book I'm learning hadn't picked out this.Is it a common problem?Or just occurs in Vc++6.0? #include<fstream> #include<iostream> using namespace std; int count(ifstream& fin);
11
8146
by: adramolek | last post by:
So... I'm trying to get used to using C++ ifstream (or ofstream) instead of stdio (although I'm having second thoughts). Anyways, I want to be able to display a meaningful error message if ifstream fails to open a file, but nothing I read about ifstream says anything about a reliable place to get an error message. Using stdio I can simply do: FILE *f = fopen(filename, "rb"); if (!f) perror(filename);
5
7578
by: DrSchwartz | last post by:
Hi there, I have to read in from file, and then process it. It looks like this. #include <iostream> #include <string> #include <fstream> using namespace std; ... int main() {
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.