Thread: read file without ifstream

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    30

    read file without ifstream

    I have an example code for reading a file and working with the values in c++ but I don't understand how it works:
    Code:
    #include <iostream>
    #include<fstream>
    using namespace std;
    
    
    class v3d{
    
    
      double x,y,z;
    public:
       v3d(double a=0,double b=0,double c=0){x=a; y=b; z=c;}
       void  operator+=(v3d a);
       friend ostream& operator<<(ostream& out, const v3d &s);
       friend v3d  operator*(double a, v3d b);
    };
    
    
    v3d  operator*(double a, v3d b){
         return v3d(a*b.x,a*b.y,a*b.z);
         }
    
    
    void v3d::operator+=(v3d a){
     x+=a.x;y+=a.y;z+=a.z;
    }
    
    
    ostream& operator<<(ostream& out, const v3d &s)
    {
     out << s.x << "   " << s.y << "   " << s.z;
     return out;
    }
    
    
    
    
    int main(void){
      int i;
      v3d s(0,0), v(2,2), F(0,-10);
      double dt=1e-2,m=1.;
      for (i=0;i<40;i++){
         v+=1./m*dt*F;
         s+=dt*v; 
         cout << i*dt << "   " << s << "  " << v << endl; 
         }
    }
    This programm reads a file called "move.dat". I don't understand how this works. There is not even the name of the file and still it works. Can somebody pleas explain me where and how the reading is done?

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Seems you don't have the complete code. There is no fstream or ifstream declared for reading from a file, neither is there a reading from the console.
    It just produces the following output from hardcoded input.
    Code:
    0   0.02   0.019   0  2   1.9   0
    0.01   0.04   0.037   0  2   1.8   0
    0.02   0.06   0.054   0  2   1.7   0
    0.03   0.08   0.07   0  2   1.6   0
    0.04   0.1   0.085   0  2   1.5   0
    0.05   0.12   0.099   0  2   1.4   0
    0.06   0.14   0.112   0  2   1.3   0
    0.07   0.16   0.124   0  2   1.2   0
    0.08   0.18   0.135   0  2   1.1   0
    0.09   0.2   0.145   0  2   1   0
    0.1   0.22   0.154   0  2   0.9   0
    0.11   0.24   0.162   0  2   0.8   0
    0.12   0.26   0.169   0  2   0.7   0
    0.13   0.28   0.175   0  2   0.6   0
    0.14   0.3   0.18   0  2   0.5   0
    0.15   0.32   0.184   0  2   0.4   0
    0.16   0.34   0.187   0  2   0.3   0
    0.17   0.36   0.189   0  2   0.2   0
    0.18   0.38   0.19   0  2   0.1   0
    0.19   0.4   0.19   0  2   -6.38378e-16   0
    0.2   0.42   0.189   0  2   -0.1   0
    0.21   0.44   0.187   0  2   -0.2   0
    0.22   0.46   0.184   0  2   -0.3   0
    0.23   0.48   0.18   0  2   -0.4   0
    0.24   0.5   0.175   0  2   -0.5   0
    0.25   0.52   0.169   0  2   -0.6   0
    0.26   0.54   0.162   0  2   -0.7   0
    0.27   0.56   0.154   0  2   -0.8   0
    0.28   0.58   0.145   0  2   -0.9   0
    0.29   0.6   0.135   0  2   -1   0
    0.3   0.62   0.124   0  2   -1.1   0
    0.31   0.64   0.112   0  2   -1.2   0
    0.32   0.66   0.099   0  2   -1.3   0
    0.33   0.68   0.085   0  2   -1.4   0
    0.34   0.7   0.07   0  2   -1.5   0
    0.35   0.72   0.054   0  2   -1.6   0
    0.36   0.74   0.037   0  2   -1.7   0
    0.37   0.76   0.019   0  2   -1.8   0
    0.38   0.78   -2.15106e-16   0  2   -1.9   0
    0.39   0.8   -0.02   0  2   -2   0

  3. #3
    Guest
    Guest
    You probably got mixed up with another executable. This code does not do file I/O.

    p.s. You should really start to work in your formatting. The first week learning C++ that's excusable, but by now your code should be consistently formatted (even if you took it from somewhere else).

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    30
    I was wondering that there is no ifstream too, but the strange thing is, that this are the numbers from my file. So I have no idea why you are getting this values without having the file.

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Quote Originally Posted by Gamma_123 View Post
    So I have no idea why you are getting this values without having the file.
    The numbers are calculated and print in this loop:
    Code:
    for (i=0;i<40;i++)
    {  
      v+=1./m*dt*F;
      s+=dt*v; 
      cout << i*dt << "   " << s << "  " << v << endl; 
    }

  6. #6
    Registered User
    Join Date
    Nov 2017
    Posts
    30
    I just saw it. Thanks for your help anyway. I should check my sources better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CPP: cannot read with ifstream?
    By nacho4d in forum Linux Programming
    Replies: 2
    Last Post: 02-24-2010, 02:52 PM
  2. [question] ifstream read file, thanks!
    By userpingz in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2009, 06:38 PM
  3. Getting size read with ifstream and read()
    By nickname_changed in forum C++ Programming
    Replies: 13
    Last Post: 08-03-2003, 06:05 AM
  4. Help with ifstream read plz
    By dp_goose in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2002, 06:35 AM
  5. why can't ifstream read spaces or enters
    By bobish in forum C++ Programming
    Replies: 11
    Last Post: 06-13-2002, 04:23 PM

Tags for this Thread