read data from file into structure in C++ ?

Discussion in 'C++' started by tvn007@hotmail.com, Nov 11, 2005.

  1. Guest

    I wrote the code below to read data from file into structure using C.
    However, I would like to convert it to C++.
    Could someone please give me some hints. I am not that famaliar with
    C++

    Thanks in advance
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    struct test {

    char enabled,
    put[20];
    int type;
    char desc[20];
    } *results_ptr, Result[50];

    /////////////////////////////////////////////////////////
    void input (string& inputfile){
    const int size = 2048;
    char *ptr, buffer[size];
    ifstream in(inputfile.c_str());
    if (!in.is_open()) {
    cerr <<"Cannot open file" "" <<inputfile<<endl;
    exit (EXIT_FAILURE);
    }
    results_ptr = Result;
    while (in.getline (buffer, size)){
    ptr = strtok(buffer,"-,\t\n");
    if (*ptr == '#')
    continue;

    results_ptr->enabled = *ptr;
    ptr = strtok(NULL,"-,\t\n");

    while (*ptr == ' ') ptr++;
    (void)strcpy(results_ptr->put,ptr);
    ptr = strtok(NULL,"-,\t\n");

    while (*ptr == ' ') ptr++;
    results_ptr->type = atoi(ptr);
    ptr = strtok(NULL,",\t\n");

    while (*ptr == ' ') ptr++;
    (void)strcpy(results_ptr->desc,ptr);
    results_ptr++;
    }

    in.close();
    return ;
    }
    ///////////////////////// INPUT //////////////////////////////////////
    ##############################################
    N, banana, 1, yellow
    Y, grass, 2, green
     
    , Nov 11, 2005
    #1
    1. Advertising

  2. Ian Guest

    wrote:
    > I wrote the code below to read data from file into structure using C.
    > However, I would like to convert it to C++.
    > Could someone please give me some hints. I am not that famaliar with
    > C++
    >

    Converting from C code isn't a good idea, you will perpetuate the C
    idioms into you new version.

    Start form scratch, thinking C++ objects - streams and strings.

    Ian
     
    Ian, Nov 11, 2005
    #2
    1. Advertising

  3. wrote:
    > I wrote the code below to read data from file into structure using C.
    > However, I would like to convert it to C++.
    > Could someone please give me some hints. I am not that famaliar with
    > C++


    First thing you need is a string tokenizer that works on C++ strings (to
    replace strtok). You could write your own but that's a project in
    itself, so I'd recommend the one from boost

    http://www.boost.org/libs/tokenizer/index.html

    Completely free.

    john
     
    John Harrison, Nov 11, 2005
    #3
  4. Guest

    You can try using class and vector
     
    , Nov 11, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joan
    Replies:
    11
    Views:
    1,297
  2. Vasilis Serghi

    How do I read a csv file into a structure?

    Vasilis Serghi, Jan 19, 2004, in forum: C Programming
    Replies:
    5
    Views:
    1,096
    Vasilis Serghi
    Jan 23, 2004
  3. Replies:
    2
    Views:
    660
    John Harrison
    Nov 20, 2005
  4. meisterbartsch
    Replies:
    2
    Views:
    793
    meisterbartsch
    Jun 12, 2007
  5. A
    Replies:
    27
    Views:
    1,646
    Jorgen Grahn
    Apr 17, 2011
Loading...

Share This Page