Im a noob in C, and I got some problem with fread().
Here is a struct with 300 char and I hope to use 1 set of data which has 300 char.
And now is the problem, the first data "data.FEE_YM" got all of 300 words inside, and the second "data.APPL_TYPE" got 299 words( 300-1), third "data.HOSP_ID" got 300-1-34....
Here is my code.
---------------------------------------------------------- - void check_file(FILE *file);
-
-
struct Medical_data{
-
char
-
FEE_YM[6],
-
APPL_TYPE[1],
-
HOSP_ID[34],
-
APPL_DATE[8],
-
CASE_TYPE[2],
-
SEQ_NO[6],
-
CURE_ITEM_NO1[2],
-
CURE_ITEM_NO2[2],
-
CURE_ITEM_NO3[2],
-
CURE_ITEM_NO4[2],
-
FUNC_TYPE[2],
-
FUNC_DATE[8],
-
TREAT_END_DATE[8],
-
ID_BIRTHDAY[8],
-
ID[32],
-
CARD_SEQ_NO[4],
-
GAVE_KIND[1],
-
PART_NO[3],
-
ACODE_ICD9_1[5],
-
ACODE_ICD9_2[5],
-
ACODE_ICD9_3[5],
-
ICD_OP_CODE[4],
-
DRUG_DAT[2],
-
MED_TYPE[1],
-
PRSN_ID[32],
-
PHAR_ID[32],
-
DRUG_AMT[8],
-
TREAT_AMT[8],
-
TREAT_CODE[12],
-
DIAG_AMT[8],
-
DSVC_NO[12],
-
DSVC_AMT[8],
-
BY_PASS_CODE[2],
-
T_AMT[8],
-
PART_AMT[8],
-
T_APPL_AMT[8],
-
ID_SEX[1];
-
};
-
int main()
-
{
-
FILE *fptr;
-
struct Medical_data data;
-
-
fptr = fopen("EX_CD20YM.DAT", "r");
-
check_file(fptr);
-
-
fread(&data, sizeof(struct Medical_data),1,fptr);
-
-
return 0;
-
}
-
-
void check_file(FILE *file){
-
if(file){
-
printf("opened sucessed\n");
-
}
-
else{
-
printf("opened failed\n");
-
}
-
}
-
----------------------------------------------------------
Is sizeof(struct Medical_data) exactly the same as the size of the struct definition? Are those arrays strings? With \0? And you are sure that a 3 character string in a 12 character field was written to the file using 12 characters?
Questions like this usually mean that the struct needs to be written member-by-member with some between-field separator and then read field-by-field using the between-field separator to control filling of the struct members.
I have also seen a Pack()-frwrite and fread - Unpack() where the data fields of the struct are packed into a buffer with the fields separated by a designated field separator then written. The fread just reads to a buffer and the unpack uses the buffer to fill members using the field separator to control when to stop filling the current field and move to the next one
One format is CSV (comma-separated-values).
You get into this because of field alignment. Compilers like to align fields on word boundaries for speed in reading and writing but this may introduce "pad bytes".
2 4396
Is sizeof(struct Medical_data) exactly the same as the size of the struct definition? Are those arrays strings? With \0? And you are sure that a 3 character string in a 12 character field was written to the file using 12 characters?
Questions like this usually mean that the struct needs to be written member-by-member with some between-field separator and then read field-by-field using the between-field separator to control filling of the struct members.
I have also seen a Pack()-frwrite and fread - Unpack() where the data fields of the struct are packed into a buffer with the fields separated by a designated field separator then written. The fread just reads to a buffer and the unpack uses the buffer to fill members using the field separator to control when to stop filling the current field and move to the next one
One format is CSV (comma-separated-values).
You get into this because of field alignment. Compilers like to align fields on word boundaries for speed in reading and writing but this may introduce "pad bytes".
Thanks for #weaknessforcats reply, this problem has bothered me a while, i think is all the mistake with the terminal sign, ("\0"). which made over-printing.
Final I fix this problem by using strncpy to split part by part into each length of array and add the terminal "\0" in every sting part. Then make print and output process.
Field alignment is pretty hard for such noob like me and it should take a little time.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Luc Holland |
last post by:
Hey,
I'm working on a program that reads a binary file. It's opened with
====
if ((f1=fopen(argv,"rb"))==NULL) {
fprintf(stderr,"Error opening %s for reading . . .\n",argv);
exit(2);
}
====...
|
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: janssenssimon |
last post by:
//de structure om de highscores in op de slagen
typedef struct score{
char *naam;
int veld;
int score;
struct score *volg;
}HIGH;
void toonhighscores(void)
|
by: Giox |
last post by:
Hello everybody I want to create a routine that mimic the fread
behavior operating on char array.
I wrote
size_t fread_char(void* buf, size_t sz, size_t n, char* string_source)
{
...
|
by: M. Åhman |
last post by:
I'm reading "C: A Reference Manual" but still can't understand a
very basic thing: is there any functional difference between
fgetc/fputc and fread/fwrite (when reading/writing one unsigned
char)?...
|
by: Claude Yih |
last post by:
Hi, everyone. I noticed an interesting thing about fread() this
afternoon. Well, I can't see why so I post this message in the hope of
getting some explanation. Please help me.
I wrote the...
|
by: edware |
last post by:
I want to read header data from a image file,
and I have a struct that represent this header.
But it seems like my compiler pads the data,
resulting that my fread() call won't put the right...
|
by: vippstar |
last post by:
-- foo.c --
#include <stdio.h>
int main(void) {
struct { int a; int b; } foo;
fread(&foo, sizeof foo, 1, stdin);
return 0;
}
-- foo.c --
|
by: xiao |
last post by:
Hi~ every one~ I have a queston about fread function. if i have a
code like this:
(nscrdh and data are defined as two dementional arrays and both of
them were stored in the same binary file)
...
|
by: Highlander2nd |
last post by:
Hello there.
I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models.
When I store mesh data, I...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |