I'm attempting to read a data file into a array of structures. The data comes like this
Jane Doe 5 245-44-8899 September 15, 2005
Number of entries in the file is unknown.
My segment to read the file is as follows: -
while( (fscanf(inf, "%s %s %d %s %s %d, %d", arr[numRead].fName, arr[numRead].lName, arr[numRead].gradeLvl, month, arr[numRead].date.day,
-
arr[numRead].date.year)) == 7)
-
{
-
numRead++;
-
}
-
-
and this is the structure declaration:
-
-
typedef struct
-
{
-
int month;
-
int day;
-
int year;
-
} DATE;
-
-
typedef struct
-
{
-
char fName[STR_SIZE];
-
char lName[STR_SIZE];
-
int gradeLvl;
-
char social[SS_SIZE];
-
DATE date;
-
}STUREC;
-
The month string is in the because i have to convert text month into numerical month cuz its stored as an int in my structure.
If more of the code is needed please send a PM but i think the problem is in the posted segment.
3 5527
O and the problem is that its not even going into the loop once. If i remove the loop it still won't store the info into the array so I assume its a problem with the fscanf
AdrianH 1,251
Recognized Expert Top Contributor
I'm attempting to read a data file into a array of structures. The data comes like this
Jane Doe 5 245-44-8899 September 15, 2005
Number of entries in the file is unknown.
My segment to read the file is as follows: -
while( (fscanf(inf, "%s %s %d %s %s %d, %d", arr[numRead].fName, arr[numRead].lName, arr[numRead].gradeLvl, month, arr[numRead].date.day,
-
arr[numRead].date.year)) == 7)
-
...
-
The scanf set of functions (including fscanf) require that you pass pointers to where you want to store the data. arr[numRead].fName will be auto converted to a pointer since it is an array. arr[numRead].gradeLvl is an int so it will not be converted. If you pass anything but pointers, what will occurr is undefined. If you are lucky, it will seg fault, giving you an indication that something is wrong.
The month string is in the because i have to convert text month into numerical month cuz its stored as an int in my structure.
That is fine.
If more of the code is needed please send a PM but i think the problem is in the posted segment.
You also have a problem with the number of parameters you are passing. Excluding the file handle and the format string, your format string states 7, your equality states 7, but the number of parameters you actually pass are 6. Another reason for it to do random things.
Further, you should also set limits in the %s format to keep it from overrunning the char array boundary. This may be beyond the scope of your assignment however, so unless you ask for me to explain further, I won’t go into detail.
Hope this helps.
Adrian
Much appreciated, needed some clarification on pointers with the structures. Added the SS input (7th param) in the scan and fixed the pointers.
Thanks!
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Rick Caborn |
last post by:
Does anyone know of a way to execute sql code from a dynamically built
text field?
Before beginning, let me state that I know this db architecture is
built solely for frustration and I hope to make it better soon.
Unfortunately, there is never a non-crucial time in which we can do an
upgrade, so we are stuck for now.
Point 1:
There are multiple tables: students, courses, cross-reference
|
by: Tony Johansson |
last post by:
Hello!
I'm reading a book about C++ and there is something that I don't understand
so I ask you.
I have marked the section from the book that is of intertest by tagging it
with BOOK START HERE and ending with BOOK ENDING HERE. All that text between
is just a copy from the book.
My question come after the text section.
The book says
|
by: C Gillespie |
last post by:
Dear All,
Hopefully I have a simple problem. Basically, I just want to alter some text
with JS.
Here is some of my test code:
<snip>
<script type="text/javascript">
var tmp='a';
|
by: Abby |
last post by:
My .dat file will contain information like below.
///////////
First
0x04
0x05
0x06
Second
0x07
|
by: Alfonso Morra |
last post by:
Hi,
I have the ff data types :
typedef enum {
VAL_LONG ,
VAL_DOUBLE ,
VAL_STRING ,
VAL_DATASET
}ValueTypeEnum ;
| |
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
|
by: nicolasg |
last post by:
Hi,
I'm trying to open a file (any file) in binary mode and save it inside
a new text file.
After that I want to read the source from the text file and save it
back to the disk with its original form. The problem is tha the binary
source that I extract from the text file seems to be diferent from the
source I saved. Here is my code:
1)
handle=file('image.gif','rb')
|
by: dkwan |
last post by:
Hello!
i have the following data in a file
student_id student_name maths_score english_score physics_score
can i use a nested structure to read the file?
struct student
{ int id;
char name;
|
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 following data.bin (3 Data appended to 2 Data):
2, data0, data1, 3, data0, data1, data2
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |