Thread: Reading a File

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    44

    Reading a File

    Hello,
    I just created a file called test.txt , and this file include this info
    Event Task Days
    1 15 3
    2 27 6
    3 36 4
    4 15 5
    5 18 4

    How could I read this information and just make a calculation to add up the number of days from that text file to out on the screen?
    I appreciate your help Thank you

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    assume c++, use std::ifstream. You will need to do some error checking to make sure the file is open.
    Code:
    #include <fstream>
    using namespace std;
    int main()
    {
      ifstream in("filename.txt");
      int n;
      while( in >> n)
      {
         // do something with n
      }
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    Thanks a lot
    I was just trying to play a little bit to get to know more fstream.

    This the what I came up with
    Code:
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    
    {
      ifstream in("cpData.txt");
      int n1,n2,n3, line1, line2;
      int sum1(0),sum2(0),sum3(0);
    
      cout <<"This File contain the following Info :\n" << endl;	
      cout << "Colum 1   Colum 2   Colum 3 " << endl;
      while( in >> n1 >> n2 >> n3)
    	
      {
    	  if ( n1 < n2 )
    	  {
    		 line1 = n2;
    	  }
    	  if ( n2 > n3)
    	  {
    		line2 = n3;
    	  }
    	  sum1+= n1;
    	  sum2+= n2;
    	  sum3+= n3;
    	  cout << setw(4) << n1 << setw(9)<< n2 << setw(9) << n3 << endl; 	
      }	
    	cout <<"\n Total Sum For each Colum "<< endl;
    	cout <<" Colum 1 -> " << sum1 << endl;
    	cout <<" Colum 2 -> " << sum2 << endl;
    	cout <<" Colum 3 -> " << sum3 << endl;
    	return 0;
    }
    Thank you

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    int sum1(0),sum2(0),sum3(0);
    Although the above works, you will more frequently see this
    Code:
    int sum1 = 0, sum2 = 0, sum3 = 0;
    or this
    Code:
    int sum1 = 0;
    int sum2 = 0;
    int sum3 = 0;
    and you need to initialze other variables similarily -- there is a big chance that they will be used before being initialized if you don't.
    Last edited by Ancient Dragon; 03-05-2006 at 08:46 PM.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    44

    Thanks

    OK Thanks a lot for the help
    Nice forum by the way!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM