473,465 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading structures in c

1 New Member
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 2035
Meetee
931 Recognized Expert Moderator Contributor
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 New Member
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.