Results 1 to 3 of 3

Math Help - C++ input

  1. #1
    Member
    Joined
    Jan 2008
    Posts
    154

    C++ input

    Code:
      #include <iostream>
     #include <fstream>
     #include <math.h>
    
     using namespace std;
     int main()
     {
       
        char str[2000];
        fstream file_op("C:\\IndusCommodities\\PriceFiles\\AllMarkets-1.txt",ios::in);
        if (!file_op.is_open())
        {
      
                  cerr << "Unable to open the file" << endl;
                  return -1;
    
              }
           while(!file_op.getline(str, sizeof(str)))
           {
                   cout <<str;
             }
      
              file_op.close();
      
              cout <<endl;
       
      
              return 0;
      
          }
    I am trying to read in a text file and print out the result. The program compiles and executes fine, but nothing comes on the screen.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor Reckoner's Avatar
    Joined
    May 2008
    From
    Baltimore, MD (USA)
    Posts
    1,024
    Thanks
    75
    Awards
    1

    Smile

    Quote Originally Posted by heathrowjohnny View Post
    I am trying to read in a text file and print out the result. The program compiles and executes fine, but nothing comes on the screen.
    The main problem is that basic_ios::operator! will return true when the stream is bad, and false otherwise. So your while condition is backwards.

    Other things:

    Don't use a statically-allocated char array for things like this. Although the second parameter of getline will prevent a buffer overflow, it would be much better to use C++ style strings here (you will need to use the global getline function, defined in <string>). Also, keep in mind it is usually poor style to dump the entire std namespace like that, but for a tiny program like this there isn't a problem. And it is not necessary to call file_op.close at the end, because the fstream destructor will do it automatically.

    I would rewrite it as:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      fstream file_op("C:\\IndusCommodities\\PriceFiles\\AllMarkets-1.txt", ios::in);
    
      if(!file_op.is_open())
      {
        cerr << "Unable to open the file" << endl;
        return -1;
      }
    
      string line;
      while(file_op && !file_op.eof())
      {
        getline(file_op, line);
        cout << line << '\n';
      }
    
      return 0;
    }
    <math.h> isn't needed here, but if you are going to use it, I suggest using <cmath> instead, since everything will be placed in the std namespace that way.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Member
    Joined
    Jan 2008
    Posts
    154
    thanks. I was using <math.h> because I will need it later. Yeah I am just learning C++ but know some Java.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. how to input this on a ti-89?
    Posted in the Calculus Forum
    Replies: 1
    Last Post: November 1st 2010, 09:07 AM
  2. input
    Posted in the Math Topics Forum
    Replies: 1
    Last Post: March 24th 2009, 11:54 AM
  3. Input (m,n) output...
    Posted in the Algebra Forum
    Replies: 2
    Last Post: February 24th 2009, 12:25 PM
  4. Tables and input
    Posted in the Pre-Calculus Forum
    Replies: 1
    Last Post: January 24th 2009, 07:26 PM
  5. input log into TI83
    Posted in the Pre-Calculus Forum
    Replies: 1
    Last Post: October 21st 2008, 07:25 PM

Search Tags


/mathhelpforum @mathhelpforum