read data

This is a discussion on read data within the C++ Programming forums, part of the General Programming Boards category; How can I modify the code to input the second columns of the following data file into array x[N] VELOCITY ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    65

    read data

    How can I modify the code to input the second columns of the following data file into array x[N]

    VELOCITY
    3 4 5
    4 5 6
    ...N

    x[] = 4, 5 ...N


    Code:
    int main(){
    
    	double x[N];
    
    	ifstream in("file.txt");
    
    	int i = 0;
    	while (i < N && in >> x[i])
    	{
    		i++;
    	}
    }
    Thanks

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Read in a line at a time (3 values), throw away (or do something else with) the two values you don't want and store the other (the 2nd column):
    Code:
    double temp1, temp2;
    while( i < N && in >> temp1 >> x[i] >> temp2 )
    {
        ++i;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read() is not reading the whole data
    By maven in forum C Programming
    Replies: 6
    Last Post: 02-18-2006, 06:15 AM
  2. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  3. read data from file
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 09:37 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Read data from file !!!
    By frankiepoon in forum C Programming
    Replies: 2
    Last Post: 10-14-2002, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21