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. 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
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
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 ...
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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 ...
|
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
|
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";}
|
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");
| |
by: Ralf Goertz |
last post by:
Hi,
consider the following program
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv){
|
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);
|
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);
|
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()
{
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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();...
|
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...
|
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
| |
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...
| |