How do I read a csv file into a structure?

Discussion in 'C Programming' started by Vasilis Serghi, Jan 19, 2004.

  1. Hi all, I am fairly new to C programming and I have come to a stand still. I
    am trying to create a program that will accept a user input and give the
    user an output depending on the contents of a csv file. So far I have
    successfully managed to capture the user input, open the csv file, create a
    structure that matches the file data, but I cannot get the data into the
    structure to do a search on.

    This is some of what I have done so far:-

    #define NUM_OF_ROWS 437
    #define NUM_OF_COLUMNS 3
    FILE *errorFile;
    struct csvFileData {
    char dtcDesc[NUM_OF_ROWS];
    int errorNum[NUM_OF_ROWS];
    char dtcNum[NUM_OF_ROWS];
    }dtcInfo;

    if ((errorFile = fopen("Error_Loc.csv","r")) == NULL)
    {
    fprintf(stderr,"\nError opening \"Error_Loc.csv\" file. \
    \nMake sure the file is in the same folder as this program.
    \nProgram will now exit. ");
    system("PAUSE");
    exit(1);
    }

    fclose(errorFile);

    I assume that as I didn't get the error for fprintf, then the file was
    opened and closed ok.

    My question is, how do I get the contents of the errorFile into my
    structure?

    If the user selects 14 as an error number for example, how can I do the
    search on the structure to give me back the dtcInfo.dtcDec and
    dtcInfo.dtcNum that corresponds to the error number requested?
    How can I initialise the structure with zeros for the int and spaces for
    char?

    Again sorry if this is basic stuff. Any help is greatly appreciated.

    --
    Vas

    (These are my own thoughts and views and not the views of my employer.)
     
    Vasilis Serghi, Jan 19, 2004
    #1
    1. Advertising

  2. "Vasilis Serghi" <> wrote in message
    news:buh83h$...
    > Hi all, I am fairly new to C programming and I have come to a stand still.

    I
    > am trying to create a program that will accept a user input and give the
    > user an output depending on the contents of a csv file. So far I have
    > successfully managed to capture the user input, open the csv file, create

    a
    > structure that matches the file data, but I cannot get the data into the
    > structure to do a search on.
    >
    > This is some of what I have done so far:-
    >
    > #define NUM_OF_ROWS 437
    > #define NUM_OF_COLUMNS 3
    > FILE *errorFile;
    > struct csvFileData {
    > char dtcDesc[NUM_OF_ROWS];
    > int errorNum[NUM_OF_ROWS];
    > char dtcNum[NUM_OF_ROWS];
    > }dtcInfo;
    >
    > if ((errorFile = fopen("Error_Loc.csv","r")) == NULL)
    > {
    > fprintf(stderr,"\nError opening \"Error_Loc.csv\" file. \
    > \nMake sure the file is in the same folder as this program.
    > \nProgram will now exit. ");
    > system("PAUSE");
    > exit(1);
    > }
    >
    > fclose(errorFile);
    >
    > I assume that as I didn't get the error for fprintf, then the file was
    > opened and closed ok.
    >
    > My question is, how do I get the contents of the errorFile into my
    > structure?
    >
    > If the user selects 14 as an error number for example, how can I do the
    > search on the structure to give me back the dtcInfo.dtcDec and
    > dtcInfo.dtcNum that corresponds to the error number requested?
    > How can I initialise the structure with zeros for the int and spaces for
    > char?
    >
    > Again sorry if this is basic stuff. Any help is greatly appreciated.
    >
    > --
    > Vas
    >
    > (These are my own thoughts and views and not the views of my employer.)
    >
    >
    >


    I should also add that the csv file is a 437x3 array. It contains mixed data
    hence the structure route. And I have also changed the following lines.

    struct csvFileData
    {
    int errorNum[NUM_OF_ROWS][NUM_OF_COLUMNS];
    char dtcDesc[NUM_OF_ROWS][NUM_OF_COLUMNS];
    char dtcNum[NUM_OF_ROWS][NUM_OF_COLUMNS];
    }dtcInfo;

    --
    Vas

    (These are my own thoughts and views and not the views of my employer.)
     
    Vasilis Serghi, Jan 19, 2004
    #2
    1. Advertising

  3. On Mon, 19 Jan 2004 13:36:32 -0500, Vasilis Serghi wrote:
    >
    > My question is, how do I get the contents of the errorFile into my
    > structure?


    Try this and look at the tcase/tests shipped with the lib:

    http://www.ioplex.com/~miallen/libmba/dl/src/csv.c

    Mike
     
    Michael B Allen, Jan 20, 2004
    #3
  4. Vasilis Serghi wrote:
    > Hi all, I am fairly new to C programming and I have come to a stand still. I
    > am trying to create a program that will accept a user input and give the
    > user an output depending on the contents of a csv file. So far I have
    > successfully managed to capture the user input, open the csv file, create a
    > structure that matches the file data, but I cannot get the data into the
    > structure to do a search on.
    >
    > This is some of what I have done so far:-
    >
    > #define NUM_OF_ROWS 437
    > #define NUM_OF_COLUMNS 3
    > FILE *errorFile;
    > struct csvFileData {
    > char dtcDesc[NUM_OF_ROWS];
    > int errorNum[NUM_OF_ROWS];
    > char dtcNum[NUM_OF_ROWS];
    > }dtcInfo;
    >
    > if ((errorFile = fopen("Error_Loc.csv","r")) == NULL)
    > {
    > fprintf(stderr,"\nError opening \"Error_Loc.csv\" file. \
    > \nMake sure the file is in the same folder as this program.
    > \nProgram will now exit. ");
    > system("PAUSE");
    > exit(1);
    > }
    >
    > fclose(errorFile);
    >
    > I assume that as I didn't get the error for fprintf, then the file was
    > opened and closed ok.
    >
    > My question is, how do I get the contents of the errorFile into my
    > structure?
    >
    > If the user selects 14 as an error number for example, how can I do the
    > search on the structure to give me back the dtcInfo.dtcDec and
    > dtcInfo.dtcNum that corresponds to the error number requested?
    > How can I initialise the structure with zeros for the int and spaces for
    > char?
    >
    > Again sorry if this is basic stuff. Any help is greatly appreciated.
    >
    > --
    > Vas
    >
    > (These are my own thoughts and views and not the views of my employer.)


    Personally, I would think of this as fields and records. Each column
    is a field and each row as a record. Your file represents a container
    of records (just in a different format).

    struct File_Record
    {
    char * dtc_description;
    int error_number; /* should this be unsigned??? */
    char dtc_number; /* why char and not int? */
    };

    struct File_Record array_of_records[NUM_ROWS];

    I didn't specify the dtc description as a fixed length of characters
    since I didn't know the maximum width. I have left it as a pointer
    so that room for the text can be allocated during run-time.

    As far as searching for an error number, all you have to do is
    to look at array_of_records.error_number for all i, 0 .. NUM_ROWS.

    I suggest you use a dynamic container, such as a linked list, since
    the number of rows may vary.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Jan 20, 2004
    #4
  5. Vasilis Serghi

    Joe Wright Guest

    Vasilis Serghi wrote:
    >
    > "Vasilis Serghi" <> wrote in message
    > news:buh83h$...
    > > Hi all, I am fairly new to C programming and I have come to a stand still.

    > I
    > > am trying to create a program that will accept a user input and give the
    > > user an output depending on the contents of a csv file. So far I have
    > > successfully managed to capture the user input, open the csv file, create

    > a
    > > structure that matches the file data, but I cannot get the data into the
    > > structure to do a search on.
    > >
    > > This is some of what I have done so far:-
    > >
    > > #define NUM_OF_ROWS 437
    > > #define NUM_OF_COLUMNS 3
    > > FILE *errorFile;
    > > struct csvFileData {
    > > char dtcDesc[NUM_OF_ROWS];
    > > int errorNum[NUM_OF_ROWS];
    > > char dtcNum[NUM_OF_ROWS];
    > > }dtcInfo;
    > >
    > > if ((errorFile = fopen("Error_Loc.csv","r")) == NULL)
    > > {
    > > fprintf(stderr,"\nError opening \"Error_Loc.csv\" file. \
    > > \nMake sure the file is in the same folder as this program.
    > > \nProgram will now exit. ");
    > > system("PAUSE");
    > > exit(1);
    > > }
    > >
    > > fclose(errorFile);
    > >
    > > I assume that as I didn't get the error for fprintf, then the file was
    > > opened and closed ok.
    > >
    > > My question is, how do I get the contents of the errorFile into my
    > > structure?
    > >
    > > If the user selects 14 as an error number for example, how can I do the
    > > search on the structure to give me back the dtcInfo.dtcDec and
    > > dtcInfo.dtcNum that corresponds to the error number requested?
    > > How can I initialise the structure with zeros for the int and spaces for
    > > char?
    > >
    > > Again sorry if this is basic stuff. Any help is greatly appreciated.
    > >
    > > --
    > > Vas
    > >
    > > (These are my own thoughts and views and not the views of my employer.)
    > >
    > >
    > >

    >
    > I should also add that the csv file is a 437x3 array. It contains mixed data
    > hence the structure route. And I have also changed the following lines.
    >
    > struct csvFileData
    > {
    > int errorNum[NUM_OF_ROWS][NUM_OF_COLUMNS];
    > char dtcDesc[NUM_OF_ROWS][NUM_OF_COLUMNS];
    > char dtcNum[NUM_OF_ROWS][NUM_OF_COLUMNS];
    > }dtcInfo;
    >

    We might have a problem with terms. A .csv file is a text file
    representing a two dimensional table of rows of columns. The first row
    (line) is usually the field name. For example:

    custno,first,last,addr,city,state,zip
    1,John,Kerry,1 Main St,Des Moines,IA,
    2,Joe,Wright,28th Rd,Arlington,VA,22206

    Does your .csv file look anything like this?
    --
    Joe Wright http://www.jw-wright.com
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Jan 20, 2004
    #5
  6. "Joe Wright" <> wrote in message
    news:...
    > Vasilis Serghi wrote:
    > >
    > > "Vasilis Serghi" <> wrote in message
    > > news:buh83h$...
    > > > Hi all, I am fairly new to C programming and I have come to a stand

    still.
    > > I
    > > > am trying to create a program that will accept a user input and give

    the
    > > > user an output depending on the contents of a csv file. So far I have
    > > > successfully managed to capture the user input, open the csv file,

    create
    > > a
    > > > structure that matches the file data, but I cannot get the data into

    the
    > > > structure to do a search on.
    > > >
    > > > This is some of what I have done so far:-
    > > >
    > > > #define NUM_OF_ROWS 437
    > > > #define NUM_OF_COLUMNS 3
    > > > FILE *errorFile;
    > > > struct csvFileData {
    > > > char dtcDesc[NUM_OF_ROWS];
    > > > int errorNum[NUM_OF_ROWS];
    > > > char dtcNum[NUM_OF_ROWS];
    > > > }dtcInfo;
    > > >
    > > > if ((errorFile = fopen("Error_Loc.csv","r")) == NULL)
    > > > {
    > > > fprintf(stderr,"\nError opening \"Error_Loc.csv\" file. \
    > > > \nMake sure the file is in the same folder as this program.
    > > > \nProgram will now exit. ");
    > > > system("PAUSE");
    > > > exit(1);
    > > > }
    > > >
    > > > fclose(errorFile);
    > > >
    > > > I assume that as I didn't get the error for fprintf, then the file was
    > > > opened and closed ok.
    > > >
    > > > My question is, how do I get the contents of the errorFile into my
    > > > structure?
    > > >
    > > > If the user selects 14 as an error number for example, how can I do

    the
    > > > search on the structure to give me back the dtcInfo.dtcDec and
    > > > dtcInfo.dtcNum that corresponds to the error number requested?
    > > > How can I initialise the structure with zeros for the int and spaces

    for
    > > > char?
    > > >
    > > > Again sorry if this is basic stuff. Any help is greatly appreciated.
    > > >
    > > > --
    > > > Vas
    > > >
    > > > (These are my own thoughts and views and not the views of my

    employer.)
    > > >
    > > >
    > > >

    > >
    > > I should also add that the csv file is a 437x3 array. It contains mixed

    data
    > > hence the structure route. And I have also changed the following lines.
    > >
    > > struct csvFileData
    > > {
    > > int errorNum[NUM_OF_ROWS][NUM_OF_COLUMNS];
    > > char dtcDesc[NUM_OF_ROWS][NUM_OF_COLUMNS];
    > > char dtcNum[NUM_OF_ROWS][NUM_OF_COLUMNS];
    > > }dtcInfo;
    > >

    > We might have a problem with terms. A .csv file is a text file
    > representing a two dimensional table of rows of columns. The first row
    > (line) is usually the field name. For example:
    >
    > custno,first,last,addr,city,state,zip
    > 1,John,Kerry,1 Main St,Des Moines,IA,
    > 2,Joe,Wright,28th Rd,Arlington,VA,22206
    >
    > Does your .csv file look anything like this?
    > --
    > Joe Wright http://www.jw-wright.com
    > "Everything should be made as simple as possible, but not simpler."
    > --- Albert Einstein ---


    Yes it looks like that. Since then I have been wondering whether it will be
    easier to just scan the file for the information I want when its needed, or
    load the file into memory.


    --
    Vas

    (These are my own thoughts and views and not the views of my employer.)
     
    Vasilis Serghi, Jan 23, 2004
    #6
    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. Tintin92
    Replies:
    1
    Views:
    1,779
    Andrew Thompson
    Feb 14, 2007
  2. meisterbartsch
    Replies:
    2
    Views:
    802
    meisterbartsch
    Jun 12, 2007
  3. jliu66
    Replies:
    0
    Views:
    542
    jliu66
    Oct 19, 2007
  4. Li Chen
    Replies:
    18
    Views:
    701
    Azmi Farih
    Mar 23, 2010
  5. Sacha Rook

    csv read clean up and write out to csv

    Sacha Rook, Nov 2, 2012, in forum: Python
    Replies:
    2
    Views:
    237
    Hans Mulder
    Nov 2, 2012
Loading...

Share This Page