none
How do I read from a data file? RRS feed

  • Question

  • I would like to read this file:

                                  Betelgeuse                              
         
                                   Novi, MI                               
                Location:  W 83°27'35.0", N42°30'23.0",   292m            
                  (Longitude referred to Greenwich meridian)              
         
                     Time Zone:  5h 00m west of Greenwich                 
         
          Date               Rise  Az.       Transit Alt.       Set  Az.
         (Zone)  
                              h  m   °         h  m  °          h  m   °
    2013 Jan 01 (Tue)        17:12  79        23:42 55S        06:15 281                 
    2013 Jan 02 (Wed)        17:08  79        23:38 55S        06:11 281                 
    2013 Jan 03 (Thu)        17:05  79        23:34 55S        06:07 281                 
    2013 Jan 04 (Fri)        17:01  79        23:30 55S        06:03 281                 
    2013 Jan 05 (Sat)        16:57  79        23:26 55S        05:59 281                 
    2013 Jan 06 (Sun)        16:53  79        23:22 55S        05:56 281                 
    2013 Jan 07 (Mon)        16:49  79        23:18 55S        05:52 281                 
    2013 Jan 08 (Tue)        16:45  79        23:14 55S        05:48 281                 
    2013 Jan 09 (Wed)        16:41  79        23:10 55S        05:44 281                 
    2013 Jan 10 (Thu)        16:37  79        23:06 55S        05:40 281                 
    2013 Jan 11 (Fri)        16:33  79        23:02 55S        05:36 281                 
    2013 Jan 12 (Sat)        16:29  79        22:59 55S        05:32 281                 
    2013 Jan 13 (Sun)        16:25  79        22:55 55S        05:28 281                 
    2013 Jan 14 (Mon)        16:21  79        22:51 55S        05:24 281                 
    2013 Jan 15 (Tue)        16:17  79        22:47 55S        05:20 281                 
    2013 Jan 16 (Wed)        16:13  79        22:43 55S        05:16 281                 
    2013 Jan 17 (Thu)        16:09  79        22:39 55S        05:12 281                 
    2013 Jan 18 (Fri)        16:06  79        22:35 55S        05:08 281                 
    2013 Jan 19 (Sat)        16:02  79        22:31 55S        05:04 281                 
    2013 Jan 20 (Sun)        15:58  79        22:27 55S        05:00 281                 
    2013 Jan 21 (Mon)        15:54  79        22:23 55S        04:57 281                 
    2013 Jan 22 (Tue)        15:50  79        22:19 55S        04:53 281                 
    2013 Jan 23 (Wed)        15:46  79        22:15 55S        04:49 281                 
    2013 Jan 24 (Thu)        15:42  79        22:11 55S        04:45 281                 
    2013 Jan 25 (Fri)        15:38  79        22:07 55S        04:41 281                 
    2013 Jan 26 (Sat)        15:34  79        22:04 55S        04:37 281                 
    2013 Jan 27 (Sun)        15:30  79        22:00 55S        04:33 281                 
    2013 Jan 28 (Mon)        15:26  79        21:56 55S        04:29 281                 
    2013 Jan 29 (Tue)        15:22  79        21:52 55S        04:25 281                 
    2013 Jan 30 (Wed)        15:18  79        21:48 55S        04:21 281                 
    2013 Jan 31 (Thu)        15:14  79        21:44 55S        04:17 281   

    ...

    ...

    I need the rise, transit and set times for the star betelgeuse read into an array...

    I have no idea which library functions I would use to perform this task.

    Given this data I would like to be able to enter a time over the month of January and be able to determine which time is closest whether it be the rise set or transit or set.

    At this point I do not need to know the year but I need the month and day and perhaps the day of the week and I can probably skip the 79 and 281 data...

    Any suggestions would be appreciated?

    Later I would like to expand the program to include the 15 brightest stars visible from this location.

    This is what I have so far:

    #include<iostream>
    #include<fstream>
    
    using namespace std;
    
    	int risehh;
    	int risemm;
    
    	char ch;
    
    int main()
    {
    	ifstream inData;
    	inData.open("Betelgeuse2013.dat");
    
    	if (!inData)
    	{
    		cout << "The input file does not exist. "
    			<< "The program terminates!!!" << endl;
    		return 1;
    	}
    
    	inData.read
    	
    	inData.close();
    
    	cout << risehh << ch << risemm;
    	
    	return 0;
    }
    


    MGM

    Monday, March 25, 2013 12:40 AM

Answers

  • I need the rise, transit and set times for the star betelgeuse read into an array...

    I have no idea which library functions I would use to perform this task.

    Basically the same functions you have used in your prior
    threads to read a file and extract data from it. There
    isn't a separate function for every new data format or
    layout in a file. The functions available are the member
    functions and member operators of the stream classes such
    as ifstream, stringstream, etc. Those are the basic tools
    you use to manipulate your data, regardless of how it is
    formatted. It's *your* task and responsibility as the
    programmer to craft the needed logic using these basic
    tools.

    - Wayne

    • Marked as answer by NK-7 Monday, March 25, 2013 8:48 AM
    Monday, March 25, 2013 1:25 AM

All replies

  • I need the rise, transit and set times for the star betelgeuse read into an array...

    I have no idea which library functions I would use to perform this task.

    Basically the same functions you have used in your prior
    threads to read a file and extract data from it. There
    isn't a separate function for every new data format or
    layout in a file. The functions available are the member
    functions and member operators of the stream classes such
    as ifstream, stringstream, etc. Those are the basic tools
    you use to manipulate your data, regardless of how it is
    formatted. It's *your* task and responsibility as the
    programmer to craft the needed logic using these basic
    tools.

    - Wayne

    • Marked as answer by NK-7 Monday, March 25, 2013 8:48 AM
    Monday, March 25, 2013 1:25 AM
  • I need the rise, transit and set times for the star betelgeuse read into an array...

    I have no idea which library functions I would use to perform this task.

    Basically the same functions you have used in your prior
    threads to read a file and extract data from it. There
    isn't a separate function for every new data format or
    layout in a file. The functions available are the member
    functions and member operators of the stream classes such
    as ifstream, stringstream, etc. Those are the basic tools
    you use to manipulate your data, regardless of how it is
    formatted. It's *your* task and responsibility as the
    programmer to craft the needed logic using these basic
    tools.

    - Wayne

    I suppose I should review what I have been taught before I ask similar questions.

    I have been out of the C++ loop for several months and I am back at it

    Thanks Wayne!


    MGM

    Monday, March 25, 2013 1:38 AM
  • If you are ABSOLUTELY CERTAIN that your data is well formatted, then you know what each line contains and where in the line the data is located.  After skipping the first 11 lines, each line contains a date in positions 1-11, a weekday in 13-17, a time in 26-30, an azimuth in 32-34, a time in 43-47, an altitude in 49-50, a direction in 51, a time in 60-64, and an azimuth in 66-68.  You can use the various substring functions to extract the data (remembering the functions are zero-based), convert each datum to a usable form (depends on what you intend to do with it), and store the result in an array of struct.

    If the data is not well formatted, you can to use some of the scanning functions to "find" each datum in the line before extracting it.

    Monday, March 25, 2013 2:08 PM