From 32f7ae00074eb74d4e347c011748aa5a0444c76d Mon Sep 17 00:00:00 2001 From: Win Date: Thu, 29 Aug 2024 10:57:19 +0700 Subject: [PATCH] Added file reading --- vendorInfo | Bin 16984 -> 17184 bytes vendorInfo.c | 29 ++++++++++++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/vendorInfo b/vendorInfo index f1929920803a9f9deb0aa528b174d7ac575040e1..c90780c8cf1d76541018976877a871247926ed8d 100755 GIT binary patch delta 2127 zcmZWqT}%{L6ux(M2b>jQXV(T6tjtCbR66_Tx}f|FqOzuIf{Dh5T6QV2g@B5(Cfd}9 z36j#*20X@?rb$U33=cJdZf&gbhXiZiT4`;vX& z&bjB@pL6d`4bkyoI$GkGwrhlu!3j~ejIF<9o6O@NnamseS+xZ%h8DDdgQ`*iHPiyw zNj=gY!O&80nm(0l6ho^~G~TFz5#j#I;CH|5iLJL^80?i3h2NA8-)rd}f_E+ZsT+1% zJ+uxMEX7pg+`)N(aq$p_P+&@M(o&ALG7gHhgyyqW9*H0A>No&{)|LK)9esU=J4rO! z*Yj3qbRf}jG!Z@6(Q}Bz6J33MB;MI~c%X~)AMH7mh?Dq0S6442t%0pXAeQV$vn@g> zT6ST2JD&w1$4Yx7g7$qT_T|_GTq1#Vf?M2x!`UT9sR)yk9=@WxQoD zZw`*(vQ(Rc+p;KbN)Em}IV-*)owE-%=#`&&2Thw zoS8$196#R=>CJ`{Go)7=PAS955Lb*YXIbcLJlDi?7UN3lfe+=aYDHtCBaGRn3O@~k4W;1wmCT=Vq6gphOndixMj;a;sl(iunsT{x>Dzj|yVhFZTk1Gl-fj7u%MR9DbeW0N{Z10xILaV$4wOX^)N#-!EiWSZx=O~6^}vMqq&C7 zYvzI}FPZYIq}%(HN|{^ecX0d5q$j5F3DY3vudr1+HI{*Ro$!&rR3hf^FTs?*%vNO@ ztGMwc6;=i+mU~T)pi{%^)=6yxF;`WP4GILu_Y^<}>k_WeMg9Y|gI#&DnuCi^lFCM!a%S7Bie( z_Tsust8O#4WTAwxn-7E4J z0lI5z9mb>ag2Sh^Yi0gCooWsSg;)qr!u4YIP# zteCNc=oa8z78A2%{#oJ=O-V-rKY%Jm;|~q`3-yOnXVPCvGY#7^cYV+8ElfPkec$sw z@5edkJP;YY;V81{ObLcpFF?rhv#pn;H>U-x(kXr zD{WC_JCl&r+YCoBRf0LrLX9f6p|e>?B0V%pk|U|aFnKdEI&h-@nCr5PZkE)XGffA1RQ47q#F}8b%C+LmfoMAU+t{4f%JO>{c_EH0UW9Y?o z+l^LQk&9V6O|wD<@txMj{9uLBsN)-oas+i6^&skCQBkg;UPqlrP2iZ+*v_`o!mNW809xIXwqYHXNB?}vzxSdDOjJ5X)pu2fZ25>k)Q`}hgDqO@6M@56gx!=q`9ox~5K z%fY41;B_6R8JKZ7=pf9wqBIRfmqY9^l+r5HWI~+K;C46O(U(wc`v`NCi{VyO`<7mY zIpY`<+;T4N*`R#+{alBbw~n9JUei3YAmeQ^rOl;+88dw9-OcByJ--L{^-6J@OToj5ht?q;bBKyK)h`SEM=NKBRPSLecW2F4 znAh+&dP`6HEHrdj&rP8%G8Stz)oaB*P^;KU_JCY2uj;Fao4Fd2)y`UG&x;Jl9+a;` zWg2G8BQ6pgg!I!`u+6ym-nsb*z-_u-gXFsKRmG$$jcJ4NSd_NG sBFY<(S-0f=p7f~g6^Z)<= diff --git a/vendorInfo.c b/vendorInfo.c index 25bc368..afd4f67 100644 --- a/vendorInfo.c +++ b/vendorInfo.c @@ -12,34 +12,49 @@ int read_input(char str[], int n); -struct vendor_item { +struct vendor_item +{ char vendor_name[20]; char merchandise[15]; int inventory_count; int item_price_baht; } vendors[MAX_VENDORS]; -int main() { +int main() +{ char file_name[MAX_FILENAME_LENGTH]; read_input(file_name, MAX_FILENAME_LENGTH); FILE *fp; - if ((fp = fopen(file_name, "r")) == NULL) { + if ((fp = fopen(file_name, "r")) == NULL) + { printf("%s cannot be opened.\n", file_name); exit(EXIT_FAILURE); } - + // calculate file size + fseek(fp, 0, SEEK_END); + unsigned long file_size = ftell(fp); + fseek(fp, 0, SEEK_SET); + + char *file_buffer = (char *) malloc(file_size); + fread(file_buffer, file_size, 1, fp); + + printf("%s", file_buffer); fclose(fp); + free(file_buffer); return 0; }; -int read_input(char str[], int n) { +int read_input(char str[], int n) +{ int ch, i = 0; - while ((ch = getchar()) != '\n') { - if (i < n) { + while ((ch = getchar()) != '\n') + { + if (i < n) + { str[i++] = ch; } }