473,388 Members | 1,230 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,388 software developers and data experts.

Reading structures in c

I'm new to programming, looking for help in c.
I've got a simple structure:
struct list {
char name[10];
char sname[15];
int year, month, day;
};


And a text file:
john smith 1980 01 10
gabriel anderson 1975 11 15
john anderson 1991 03 11


Could you tell me how to read this file to an array of lists?
Dec 5 '07 #1
2 2033
Meetee
931 Expert Mod 512MB
I'm new to programming, looking for help in c.
I've got a simple structure:
struct list {
char name[10];
char sname[15];
int year, month, day;
};


And a text file:
john smith 1980 01 10
gabriel anderson 1975 11 15
john anderson 1991 03 11


Could you tell me how to read this file to an array of lists?
You can read file using fread and store the data into struct by using struct object like struct list data;

You need to read more about struct and files

Regards
Dec 5 '07 #2
looker
18
I'm new to programming, looking for help in c.
I've got a simple structure:
struct list {
char name[10];
char sname[15];
int year, month, day;
};


And a text file:
john smith 1980 01 10
gabriel anderson 1975 11 15
john anderson 1991 03 11


Could you tell me how to read this file to an array of lists?
I don't cover much in how to read this file and extract the info into your structure! but i wanna look at your structure and give some comments instead.
I would suggest that you write down something like this
Expand|Select|Wrap|Line Numbers
  1. #define MAX_NAME_LENGTH           (10)
  2. #define MAX_SURNAME_LENGTH    (15)
  3.  
and use it througout your source code!
In fact, there is no difference between this one and your code but is more convenient. Why ?
Imagin you code a hundred of files with this structure. Now you believe that 10 is a maximum length of name ( or it is a requirement ). and 15 is the maximum of surename length. But what happen when there is a change in requirement, or you just get a new idea to update 10 to whatever, and 15 to whatever else.
So what you gonna do is to go through the hundred files and change it one by one. I would believe that you will spend a whole week to look for it and change it. So do you think you spend much longer just to do a little thing.
I would suggest you to use #define because we don't care how often you use MAX_NAME_LENGTH or MAX_SURNAME_LENGTH, or whatever you change 10 and 15. We only update in one place. So it is easy than before right?

Well there is another point i have to speak out in your structure, but wait tomorrow!.
Dec 5 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently...
2
by: kittu_phani | last post by:
Iam very much new to C-programing.Please give the silution for following problem. How to read data from stdin to array ofstructures -- Posted via http://dbforums.com
3
by: Matt Laver | last post by:
Hi, I have a binary file that I'm currently reading byte by byte using code similiar to: string FileName = @"c:\myFile.dat"; FileStream fs = new FileStream(FileName, FileMode.Open,...
3
by: Usenet User | last post by:
I am trying to read (and then save) a binary file which has certain data structures in it. (The file is in propritetary format produced by a 3rd party MFC application.) I know that those data...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
3
by: Zeke Zinzul | last post by:
Hi Guys & Geeks, What's the most elegant way of dealing with binary data and structures? Say I have this (which I actually do, a woo-hoo): struct Struct_IconHeader { byte width; byte...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
1
by: paulh | last post by:
Hi All, I am having problems reading from a file into an array of structures. I have defined an array of 100 structures as shown below. struct address_t {
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I'm trying to write an array of structures named myStructArray to a binary file and later on read it back. Although I could complete the entire project in C in about 2 minutes, I...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.