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

C fread into a struct

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.
----------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. void check_file(FILE *file);
  2.  
  3. struct Medical_data{
  4.       char     
  5. FEE_YM[6],
  6.             APPL_TYPE[1],
  7.               HOSP_ID[34],
  8.             APPL_DATE[8],
  9.             CASE_TYPE[2],
  10.             SEQ_NO[6],
  11.             CURE_ITEM_NO1[2],
  12.             CURE_ITEM_NO2[2],
  13.             CURE_ITEM_NO3[2],
  14.             CURE_ITEM_NO4[2],
  15.             FUNC_TYPE[2],
  16.             FUNC_DATE[8],
  17.             TREAT_END_DATE[8],
  18.             ID_BIRTHDAY[8],
  19.             ID[32],
  20.             CARD_SEQ_NO[4],
  21.             GAVE_KIND[1],
  22.             PART_NO[3],
  23.             ACODE_ICD9_1[5],
  24.             ACODE_ICD9_2[5],
  25.             ACODE_ICD9_3[5],
  26.             ICD_OP_CODE[4],
  27.             DRUG_DAT[2],
  28.             MED_TYPE[1],
  29.             PRSN_ID[32],
  30.             PHAR_ID[32],
  31.             DRUG_AMT[8],
  32.             TREAT_AMT[8],
  33.             TREAT_CODE[12],
  34.             DIAG_AMT[8],
  35.             DSVC_NO[12],
  36.             DSVC_AMT[8],
  37.             BY_PASS_CODE[2],
  38.             T_AMT[8],
  39.             PART_AMT[8],
  40.             T_APPL_AMT[8],
  41.             ID_SEX[1];      
  42. }; 
  43. int main()
  44. {    
  45.     FILE *fptr;
  46.     struct Medical_data data;
  47.  
  48.     fptr = fopen("EX_CD20YM.DAT", "r");
  49.     check_file(fptr);
  50.  
  51.         fread(&data, sizeof(struct Medical_data),1,fptr);
  52.  
  53.        return 0;
  54. }
  55.  
  56. void check_file(FILE *file){
  57.     if(file){
  58.         printf("opened sucessed\n");
  59.     }
  60.     else{
  61.         printf("opened failed\n");
  62.     }    
  63. }
  64. ----------------------------------------------------------
Dec 12 '15 #1

✓ answered by weaknessforcats

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
weaknessforcats
9,208 Expert Mod 8TB
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".
Dec 12 '15 #2
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.
Dec 21 '15 #3

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

Similar topics

2
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); } ====...
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
4
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)
3
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) { ...
8
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)?...
6
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...
9
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...
8
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 --
20
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) ...
4
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...
0
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$) { } ...
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: 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...
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:
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...

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.