Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem with ifstream--please help

problem with ifstream--please help

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++visual-studiodebuggingworkspace
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SnaKeBeD
    wrote on last edited by
    #1

    hai all, while i'm trying to read a text file using ifstream, my code is reading data when i run the code from my vc++ (VS 6.0) workspace in debug mode.My .txt file is in the workspace folder and i'm reading from there. My problem is even if the .txt file is not there,the code is not throwing any exception and doing some calculation. i like to know y is it so,also i have closed and cleared the file pointer still the problem exists ifstream ifs; ifs("sample.txt",ifstream::in); thanks in advance

    swaroop

    J D 2 Replies Last reply
    0
    • S SnaKeBeD

      hai all, while i'm trying to read a text file using ifstream, my code is reading data when i run the code from my vc++ (VS 6.0) workspace in debug mode.My .txt file is in the workspace folder and i'm reading from there. My problem is even if the .txt file is not there,the code is not throwing any exception and doing some calculation. i like to know y is it so,also i have closed and cleared the file pointer still the problem exists ifstream ifs; ifs("sample.txt",ifstream::in); thanks in advance

      swaroop

      J Offline
      J Offline
      jhwurmbach
      wrote on last edited by
      #2

      ifstream ifs;
      ifs.open( "sample.txt", ifstream::in);
      if( !ifs.is_open()) {
      //Cry out
      }


      Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
      George Orwell, "Keep the Aspidistra Flying", Opening words

      S 1 Reply Last reply
      0
      • J jhwurmbach

        ifstream ifs;
        ifs.open( "sample.txt", ifstream::in);
        if( !ifs.is_open()) {
        //Cry out
        }


        Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
        George Orwell, "Keep the Aspidistra Flying", Opening words

        S Offline
        S Offline
        SnaKeBeD
        wrote on last edited by
        #3

        hi still the same problem exists.The code is not checking whether the file is opened or not, its getting some values from some other location.

        swaroop

        J 1 Reply Last reply
        0
        • S SnaKeBeD

          hi still the same problem exists.The code is not checking whether the file is opened or not, its getting some values from some other location.

          swaroop

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #4

          swaroopkb wrote:

          still the same problem exists

          And what is the problem? You are vague.


          Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
          George Orwell, "Keep the Aspidistra Flying", Opening words

          S 1 Reply Last reply
          0
          • J jhwurmbach

            swaroopkb wrote:

            still the same problem exists

            And what is the problem? You are vague.


            Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
            George Orwell, "Keep the Aspidistra Flying", Opening words

            S Offline
            S Offline
            SnaKeBeD
            wrote on last edited by
            #5

            sorry for the troubles, could u please tell me how to close or destroy an ifstream object, i think thats the problem when the program run ifstream object is having the buff value, its not getting cleared. Even if the sample.txt file is not there the buff is showing the contents of sample.txt, could u please tell me y is it so ???:sigh:

            swaroop

            J M 2 Replies Last reply
            0
            • S SnaKeBeD

              sorry for the troubles, could u please tell me how to close or destroy an ifstream object, i think thats the problem when the program run ifstream object is having the buff value, its not getting cleared. Even if the sample.txt file is not there the buff is showing the contents of sample.txt, could u please tell me y is it so ???:sigh:

              swaroop

              J Offline
              J Offline
              jhwurmbach
              wrote on last edited by
              #6

              std::ifstream has a close()-function, which flushes all buffers and closes the file. It destructor is also calling close, so you could simply let the stream-object go out of scope. Hint: You can introduce a scope by simply opening a { writing your code and closing the scope with }


              Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
              George Orwell, "Keep the Aspidistra Flying", Opening words

              1 Reply Last reply
              0
              • S SnaKeBeD

                sorry for the troubles, could u please tell me how to close or destroy an ifstream object, i think thats the problem when the program run ifstream object is having the buff value, its not getting cleared. Even if the sample.txt file is not there the buff is showing the contents of sample.txt, could u please tell me y is it so ???:sigh:

                swaroop

                M Offline
                M Offline
                Maxwell Chen
                wrote on last edited by
                #7

                swaroopkb wrote:

                the buff value, its not getting cleared

                To clear the buffer of istream, use std::basic_istream::ignore.

                std::fstream f("a.txt", std::ios::in);
                std::cout << f.rdbuf();	// Display content: "ABCDEFG"
                std::cout << "\\n---\\n";
                std::streamsize n;
                f.seekg(0, std::ios::end);
                n = f.tellg();	// Get the file size.
                f.seekg(0, std::ios::beg);
                f.ignore(n);	// Flush buffer.
                std::cout << f.rdbuf();	// Displays nothing.
                

                Maxwell Chen

                1 Reply Last reply
                0
                • S SnaKeBeD

                  hai all, while i'm trying to read a text file using ifstream, my code is reading data when i run the code from my vc++ (VS 6.0) workspace in debug mode.My .txt file is in the workspace folder and i'm reading from there. My problem is even if the .txt file is not there,the code is not throwing any exception and doing some calculation. i like to know y is it so,also i have closed and cleared the file pointer still the problem exists ifstream ifs; ifs("sample.txt",ifstream::in); thanks in advance

                  swaroop

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  Does this work?

                  void main( void )
                  {
                  std::ifstream ifs;

                  ifs.open("\\\\sample.txt");
                  if (! ifs.is\_open())
                      std::cout << "File not opened\\n";
                  else
                      std::cout << "File opened\\n";
                  

                  }


                  "A good athlete is the result of a good and worthy opponent." - David Crow

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups