By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 852 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

C fread into a struct

P: 2
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".

Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
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

P: 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

Post your reply

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