How do I read a csv file into a structure?

V

Vasilis Serghi

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.
 
V

Vasilis Serghi

Vasilis Serghi said:
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.

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;
 
T

Thomas Matthews

Vasilis said:
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.

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
 
J

Joe Wright

Vasilis said:
Vasilis Serghi said:
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.

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?
 
V

Vasilis Serghi

Joe Wright said:
Vasilis said:
Vasilis Serghi said:
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.

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?

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top