reading data fom files

This is a discussion on reading data fom files within the C++ Programming forums, part of the General Programming Boards category; Hi If I have a data file with numbers that I want to store in an array. What C++ finction ...

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

    reading data fom files

    Hi

    If I have a data file with numbers that I want to store in an array. What C++ finction can I use?

    ie.

    Data_file

    # velocity
    3
    4.5
    6.1
    -3
    ...N

    and i want to store these in array x[N]

    Code:
    int main(){
    
    double x[N];
    
    }
    Thanks

    Shuo

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    int main(){
    
    	double x[N];
    
    	ifstream in("file.txt");
    
    	int i = 0;
    	while (i < N && in >> x[i])
    	{
    		i++;
    	}
    }

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    what headers do i need for ifstream?

    I included

    #include <fstream>
    #include <iostream>

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Don't forget
    Code:
    using namespace std;
    or
    Code:
    std::ifstream
    For ifstream, the header fstream is required.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    How do I skip variable headers in the data file like

    Velocity
    .5
    4
    ,,,

    Density
    1.2
    0.3
    ...

  6. #6
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Do you mean how do you skip certain lines?

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    yes, I want to store them in arrays a[] and b[].

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One way is to use getline() to skip a line:
    Code:
    #include <string>
    .
    .
    std::string line;
    getline(in, line);

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    what about mutiple lines do i have to repeat the getline() each time?
    Thanks

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Yes. The function reads one line at a time.

  11. #11
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    Is there a way of skipping a defined number of lines?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    The easiest way is just reading a specified number of lines. Otherwise you can seek in the file, but it's by position, not lines, so it's not very efficient, since you have to manually check for line ends and count and stuff.
    There's no function to do what you want, at least none that I know of.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  2. Problem with reading files
    By glopv2 in forum C Programming
    Replies: 3
    Last Post: 08-01-2007, 12:05 AM
  3. Reading data from consecutively named files
    By a1pro in forum C Programming
    Replies: 10
    Last Post: 04-15-2005, 02:48 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Reading a line of data...
    By RedZippo in forum C++ Programming
    Replies: 10
    Last Post: 04-04-2004, 03:14 AM

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