how to print a filename from ifstream?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sam

    how to print a filename from ifstream?

    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.
  • SirMike

    #2
    Re: how to print a filename from ifstream?

    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

    Comment

    • Victor Bazarov

      #3
      Re: how to print a filename from ifstream?

      sam wrote:[color=blue]
      > Can anyone tell me how to print a file name from ifstream?[/color]

      Nope. Nobody can. ifstream does not retain that information.
      [color=blue]
      > [..][/color]

      V

      Comment

      • dkultasev@gmail.com

        #4
        Re: how to print a filename from ifstream?

        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

        Comment

        • codigo

          #5
          Re: how to print a filename from ifstream?


          "sam" <sam++@--.com> wrote in message news:d5db53$fro $1@news.hgc.com .hk...[color=blue]
          > 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.[/color]

          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;
          }


          Comment

          • Howard

            #6
            Re: how to print a filename from ifstream?


            "codigo" <codigo@codigo. trap.com> wrote in message
            news:Kwsee.6250 $VL3.628014@new s20.bellglobal. com...[color=blue]
            >
            > "sam" <sam++@--.com> wrote in message news:d5db53$fro $1@news.hgc.com .hk...[color=green]
            >> 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.[/color]
            >
            > 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.
            >[/color]

            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.
            [color=blue]
            > 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).
            >[/color]

            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



            Comment

            • codigo

              #7
              Re: how to print a filename from ifstream?


              "Howard" <alicebt@hotmai l.com> wrote in message
              news:lftee.7058 76$w62.510623@b gtnsc05-news.ops.worldn et.att.net...[color=blue]
              >
              > "codigo" <codigo@codigo. trap.com> wrote in message
              > news:Kwsee.6250 $VL3.628014@new s20.bellglobal. com...[color=green]
              > >
              > > "sam" <sam++@--.com> wrote in message[/color][/color]
              news:d5db53$fro $1@news.hgc.com .hk...[color=blue][color=green][color=darkred]
              > >> 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.[/color]
              > >
              > > That doesn't make sense. The only way a file name would find itself in[/color][/color]
              an[color=blue][color=green]
              > > ifstream is if the filename was read / parsed from the specified file.
              > >[/color]
              >
              > 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,[/color]
              that[color=blue]
              > would mean using something like "is.name", not just "is", but it's[/color]
              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.
              [color=blue]
              >[color=green]
              > > 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[/color][/color]
              you[color=blue][color=green]
              > > beleive that a programmer doesn't have the responsability to protect[/color][/color]
              that[color=blue][color=green]
              > > 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,[/color][/color]
              but[color=blue][color=green]
              > > because the whole world that is in (the program) enters a state of
              > > unexpected behaviour (UB).
              > >[/color]
              >
              > Well, that's a bit over-dramatic, in my opinion. Nobody said anything[/color]
              about[color=blue]
              > changing the name while the stream is open, did they? And even if one[/color]
              were[color=blue]
              > able to attempt to change the variable while the file was open, that[/color]
              doesn't[color=blue]
              > 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[/color]
              OS[color=blue]
              > 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
              >[/color]


              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?



              Comment

              • __PPS__

                #8
                Re: how to print a filename from ifstream?

                LOL

                Comment

                • __PPS__

                  #9
                  Re: how to print a filename from ifstream?

                  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.

                  Comment

                  • codigo

                    #10
                    Re: how to print a filename from ifstream?


                    "__PPS__" <block111@mail. ru> wrote in message
                    news:1115361450 .347115.5220@g1 4g2000cwa.googl egroups.com...[color=blue]
                    > 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.
                    >[/color]

                    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.


                    Comment

                    • Howard

                      #11
                      Re: how to print a filename from ifstream?


                      "codigo" <codigo@codigo. trap.com> wrote in message
                      news:l0Gee.1239 8$VL3.735616@ne ws20.bellglobal .com...[color=blue]
                      >
                      > "__PPS__" <block111@mail. ru> wrote in message
                      > news:1115361450 .347115.5220@g1 4g2000cwa.googl egroups.com...[color=green]
                      >> 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.
                      >>[/color]
                      >
                      > 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.
                      >
                      >[/color]

                      Yes, I see that, and you're right, that the calling code can store that
                      value itself. But that's a simple case. Lots of objects are created that
                      store the values passed to their constructors. Why not be able to query
                      that information later? Suppose you had a pool of such objects. Why store
                      the parameters passed to each object's constructor yourself, when the
                      obejcts themselves can store it and you can query them later. I'll admit
                      that's not so likely with streams (I can't think of a major use for a pool
                      of streams). But in the general case, it makes perfect sense to query an
                      object for information passed to its constructor. That was my point.

                      -Howard



                      Comment

                      Working...