473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How do I read a csv file into a structure?

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

Nov 14 '05 #1
5 16620

"Vasilis Serghi" <vs*****@jaguar.com> wrote in message
news:bu*********@eccws12.dearborn.ford.com...
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.)

Nov 14 '05 #2
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
Nov 14 '05 #3
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[i].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

Nov 14 '05 #4
Vasilis Serghi wrote:

"Vasilis Serghi" <vs*****@jaguar.com> wrote in message
news:bu*********@eccws12.dearborn.ford.com...
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 ---
Nov 14 '05 #5
"Joe Wright" <jo********@earthlink.net> wrote in message
news:40***********@earthlink.net...
Vasilis Serghi wrote:

"Vasilis Serghi" <vs*****@jaguar.com> wrote in message
news:bu*********@eccws12.dearborn.ford.com...
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.)


Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Van Altena via .NET 247 | last post by:
I'm trying to figure out how to read a formatted binary file intoa structure definition in C#. I've tried using the"StructLayout" attribute with both LayoutKind.Explicit andLayoutKind.Sequential...
5
by: Alejandro Lapeyre | last post by:
I am writing a class to wrap a RIFF file, so i have to read and decode some structured data on the file. For example, in the begining of file is a header: Public Structure RiffHeader Public...
3
by: Dave Coate | last post by:
Hello again, I am going to re-post a question. I got some excellent suggestions from Rob and Mattias on this but their ideas did not solve the problem. Here is the original post: ...
2
by: Eric | last post by:
Hi, I need to send data from a file into a structure. In Vb6 this was done like so. public Type Struct1 v1 as string v2 as string end type
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
0
by: brendon | last post by:
I need to read in a jpeg or bmp file in to memory and then save these files in to a resource file for distribution in a form other than jpeg or bmp. This requires only one file to be distributed, the...
11
by: waffle.horn | last post by:
Hi, if this makes sense i want to create a function that can be called so that it reads a single line from a file, then after using the information destroys it. Such that when the function is...
9
by: Hollywood | last post by:
Hello members of the comp.lang.c++, My log file is made of a set of 1000 following lines kind: 21/09/07 13:49:56,MW.SET.D_IGLS,2.000000 21/09/07 13:49:56,MW.SET.GNP_NT,7.000000 ..... ...
6
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.