Help a beginner with Console ifstream file handling (0/1) - Programmer

This is a discussion on Help a beginner with Console ifstream file handling (0/1) - Programmer ; I'm trying to open a file to read some stock market data in the metastock format. I have attached a sample of the data file (data.txt) and my app source (mstockdata2.cpp). I can't figure out how to get it to ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Help a beginner with Console ifstream file handling (0/1)

  1. Help a beginner with Console ifstream file handling (0/1)

    I'm trying to open a file to read some stock market data in the
    metastock format. I have attached a sample of the data file
    (data.txt) and my app source (mstockdata2.cpp). I can't figure out
    how to get it to read the correct bytes into the struct that I have
    defined in accordance to the Metastock data format.

    Can someone help a rookie out?

    lurkerboy



  2. Re: Help a beginner with Console ifstream file handling (0/1)

    On Sun, 27 Jul 2003 14:43:29 -0500, lurkerboy
    wrote:

    >I'm trying to open a file to read some stock market data in the
    >metastock format. I have attached a sample of the data file
    >(data.txt) and my app source (mstockdata2.cpp). I can't figure out
    >how to get it to read the correct bytes into the struct that I have
    >defined in accordance to the Metastock data format.
    >
    >Can someone help a rookie out?
    >
    >lurkerboy
    >

    my attachments did not go through but here is the source code:


    #include "stdafx.h"
    #include "stdlib.h"
    #include
    #include
    #include

    typedef unsigned char u_char;
    typedef unsigned short u_short;
    typedef unsigned long u_long;

    struct rec_1 {
    u_short num_files; /* number of files master contains */
    u_short file_num; /* next file number to use (highest F#
    used) */
    char zeroes[49];
    };


    int main(int argc, char* argv[])
    {
    struct rec_1 record1;

    //open the file
    ifstream myfile("C:\sf\data.txt", ios::in | ios::binary |
    ios::nocreate);
    if(! myfile)
    {
    cerr << "Failed to open file.\n";
    exit(EXIT_FAILURE);
    }

    //start reading data
    //1st record from Master
    cout<<"\nThe size of record1 in bytes is "<< sizeof(record1);
    myfile.read((char*) &record1,sizeof(record1));
    //here is the problem I need to read properly from the file
    first

    cout<<"\nThe number of files in Master is "<<
    record1.num_files;
    cout<<"\nThe next file number to use in Master is "<<
    record1.file_num;
    cout<<"\nThe rest of record 1 is {{" << record1.zeroes
    <<"}}\n";

    //close file
    myfile.close();

    //exit
    return 0;
    }



+ Reply to Thread