473,761 Members | 8,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to print a filename from ifstream?

sam
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") ;

cout << "filename in ifstream: " << is << endl;

Sam.
Jul 23 '05 #1
10 18013
Don't you think is a little bit stupid ?
If you know the name of a stream i design-time simply print it like
cout<<"name";

If you don't know it you must have it in a variable haven't you ?
In this case use cout<<variable;

--
SirMike
the code is my strength
http://www.sirmike.grudziadz.com
Jul 23 '05 #2
sam wrote:
Can anyone tell me how to print a file name from ifstream?
Nope. Nobody can. ifstream does not retain that information.
[..]


V
Jul 23 '05 #3
I don't know if it will help you,
but if it okfor you you can do like that:
String FileName;
FileName="text. txt";
cout <<"filename ..."<<FileName< <endl;
but it is the same like SirMike said to you

Jul 23 '05 #4

"sam" <sam++@--.com> wrote in message news:d5******** **@news.hgc.com .hk...
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") ;

cout << "filename in ifstream: " << is << endl;

Sam.


That doesn't make sense. The only way a file name would find itself in an
ifstream is if the filename was read / parsed from the specified file.

Don't you think that the input file stream should jealously protect the
provided filename parameter from modification during file stream processing
(even when the parameter is required to be a constant, such as in this
case)? Shouldn't this fact pop out as logicly obvious in any object that is,
or will be, processing a stream?

How would you feel if i was able to change your parachute to an umbrella
between the time you boarded the plane, jumped and hit the ground? Do you
beleive that a programmer doesn't have the responsability to protect that
parachute?

Don't laugh. The consequences of not protecting the ifstream's parameter
would be worse. Not because the ifstream never gets to hit the ground, but
because the whole world that is in (the program) enters a state of
unexpected behaviour (UB).

Have you ever heard of variables?

try:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
std::string s_filename("tex t.txt");

std::ifstream ifs;
ifs.open(s_file name.c_str());
if (!ifs)
{
std::cout << "error while opening ";
std::cout << s_filename << std::endl;
}

std::cout << s_filename << std::endl;

return 0;
}
Jul 23 '05 #5

"codigo" <co****@codigo. trap.com> wrote in message
news:Kw******** ***********@new s20.bellglobal. com...

"sam" <sam++@--.com> wrote in message news:d5******** **@news.hgc.com .hk...
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") ;

cout << "filename in ifstream: " << is << endl;

Sam.
That doesn't make sense. The only way a file name would find itself in an
ifstream is if the filename was read / parsed from the specified file.


Well, that's true, given the actual design of the filestream. But it's
certainly *concievable* to envision a filestream class design that
maintained the filename as a member which could be queried. Of course, that
would mean using something like "is.name", not just "is", but it's possible.
Don't you think that the input file stream should jealously protect the
provided filename parameter from modification during file stream
processing
(even when the parameter is required to be a constant, such as in this
case)? Shouldn't this fact pop out as logicly obvious in any object that
is,
or will be, processing a stream?

How would you feel if i was able to change your parachute to an umbrella
between the time you boarded the plane, jumped and hit the ground? Do you
beleive that a programmer doesn't have the responsability to protect that
parachute?

Don't laugh. The consequences of not protecting the ifstream's parameter
would be worse. Not because the ifstream never gets to hit the ground, but
because the whole world that is in (the program) enters a state of
unexpected behaviour (UB).


Well, that's a bit over-dramatic, in my opinion. Nobody said anything about
changing the name while the stream is open, did they? And even if one were
able to attempt to change the variable while the file was open, that doesn't
mean that the world would come crashing down. It *might* screw up a given
file system if the filename were changed while the file was open, but the OS
could (and likely does) forbid that anyway, which would mean that even if
the filestream class allowed the attempt to change it, the actual action
would fail in that instance, most likely with an exception of some sort,
which it could catch as needed.

-Howard

Jul 23 '05 #6

"Howard" <al*****@hotmai l.com> wrote in message
news:lf******** *************@b gtnsc05-news.ops.worldn et.att.net...

"codigo" <co****@codigo. trap.com> wrote in message
news:Kw******** ***********@new s20.bellglobal. com...

"sam" <sam++@--.com> wrote in message news:d5******** **@news.hgc.com .hk...
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") ;

cout << "filename in ifstream: " << is << endl;

Sam.
That doesn't make sense. The only way a file name would find itself in an ifstream is if the filename was read / parsed from the specified file.


Well, that's true, given the actual design of the filestream. But it's
certainly *concievable* to envision a filestream class design that
maintained the filename as a member which could be queried. Of course,

that would mean using something like "is.name", not just "is", but it's possible.

Possible it certainly is, in fact its trivial. Would it be desireable,
probably not. The issue here is why provide access to a provided constant
string filename parameter? The OP can just as well pass a variable to the
ifstream and then manipulate the variable without bothering the ifstream or
object. I would think that efficiency is a primary concerns in the design of
such a mutating object.
Don't you think that the input file stream should jealously protect the
provided filename parameter from modification during file stream
processing
(even when the parameter is required to be a constant, such as in this
case)? Shouldn't this fact pop out as logicly obvious in any object that
is,
or will be, processing a stream?

How would you feel if i was able to change your parachute to an umbrella
between the time you boarded the plane, jumped and hit the ground? Do you beleive that a programmer doesn't have the responsability to protect that parachute?

Don't laugh. The consequences of not protecting the ifstream's parameter
would be worse. Not because the ifstream never gets to hit the ground, but because the whole world that is in (the program) enters a state of
unexpected behaviour (UB).

Well, that's a bit over-dramatic, in my opinion. Nobody said anything

about changing the name while the stream is open, did they? And even if one were able to attempt to change the variable while the file was open, that doesn't mean that the world would come crashing down. It *might* screw up a given
file system if the filename were changed while the file was open, but the OS could (and likely does) forbid that anyway, which would mean that even if
the filestream class allowed the attempt to change it, the actual action
would fail in that instance, most likely with an exception of some sort,
which it could catch as needed.

-Howard

Which brings us back to the crux of the problem. I can understand checking a
file stream's error condition bits, but why a *supplied* parameter?

#include <iostream>
#include <fstream>
#include <string>

class FileStream
{
const std::string m_filename;
std::ifstream m_ifs;
std::string m_sdata;
public:
FileStream(std: :string s) : m_filename(s), m_ifs(), m_sdata("") { }
~FileStream() { }
std::string getFilename() const
{
return m_filename;
}
const std::string getData()
{
m_ifs.open(m_fi lename.c_str()) ;
if (!m_ifs)
{
m_sdata = "error opening file " + m_filename;
return m_sdata;
}
std::getline(m_ ifs, m_sdata);
return m_sdata;
}
};

int main()
{
std::string s("data.dat") ;
FileStream fs(s);

std::cout << fs.getFilename( ) << std::endl;
std::cout << fs.getData();

return 0;
}

output:
data.dat
the only data string

whats wrong with...

int main()
{
std::string s("data.dat") ;
FileStream fs(s);

std::cout << s << std::endl;
std::cout << fs.getData();

return 0;
}

and forego having to provide the getFilename() member function?

Jul 23 '05 #7
LOL

Jul 23 '05 #8
I didn't check, but I strongly believe that implementations of
std::fstream do not store name of the opened file as private member.
They just pass it over to the os (when you call open or use appropriate
constructor) and os then returns some sort of specific
handler/descriptor which is stored.

Jul 23 '05 #9

"__PPS__" <bl******@mail. ru> wrote in message
news:11******** ************@g1 4g2000cwa.googl egroups.com...
I didn't check, but I strongly believe that implementations of
std::fstream do not store name of the opened file as private member.
They just pass it over to the os (when you call open or use appropriate
constructor) and os then returns some sort of specific
handler/descriptor which is stored.


It doesn't store the filename on those implementations i've used. My
arguement was that providing such a mechanism just doesn't make sense. Even
in a class with an ifstream member.

Look at the OP's code. He's passing a constant string to the ifs instead of
storing the string for later use.
Jul 23 '05 #10

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

Similar topics

11
3247
by: hokiegal99 | last post by:
How would I determine if a filename is greater than a certain number of characters and then truncate it to that number? For example a file named XXXXXXXXX.txt would become XXXXXX fname = files if fname > 6 print fname Thanks!!!
7
3238
by: Anton Ishmurzin | last post by:
Greetings All, I think everybodyknows the answer already. But i am quite a newbie in c++. I've got the following line in my code: ifstream ini_file_in("filename.dat", ios::in); But, the gcc (GCC) 3.2 spits out some stuff about
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
12
11665
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I currently have as a test for using std::ifstream::read(). Is there anything wrong with the way I'm getting the file? #include <vector> #include <iomanip> #include <fstream> #include <iostream>
1
2074
by: chatmili | last post by:
I am very frustrated after searching for three days. My problem seems to be so hard that no one really had an answer for it. I have seen postings with similar problem but they were either ignored or the codes/ direction didn't work. I would extremely appreciate for someone who can help me. I have more than 20,000 records for which I created a report. My goal is to print each page of the report into seperate PDF files. I want to suppress...
0
1745
by: MadCrazyNewbie | last post by:
Hi Group, I have the following code, but not sure if this is write, I would like to be able with the View code, for the document to be opened eg. C:\test.Doc and with the PRint button for it to Autmatically Print. The code I have so far is: Private Sub btnViewDoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewDoc.Click
3
1728
beacon
by: beacon | last post by:
Hi, I'm doing a homework assignment, but I have done most of the work and am only looking for some tutoring to get past this small portion of the program I have been tasked to write. The program says that a filename of "numbers.txt" will be defaulted to if the user presses enter when prompted. I set this up as a string and had no problem getting the default value to work properly. My problem occurs when the user tries to create a file with...
11
8145
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);
66
8190
by: happyse27 | last post by:
Hi All, my html code is sno 1) and perl code is sno 2). a) I tried to print $filename and it cant print out the value, only blank was displayed, and the file could not be uploaded. And it didnt display the html after the perl script executed. Using perl 5.1 and apache 2.2.9 version(apache installed and run without any errors and no warning, perl tested fine) b) Also, when i clicked the html code to submit the upload of the...
0
9554
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
9376
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
10136
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
9923
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
8813
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...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
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
5266
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...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.