Added file reading
This commit is contained in:
parent
482aad14f4
commit
32f7ae0007
BIN
vendorInfo
BIN
vendorInfo
Binary file not shown.
27
vendorInfo.c
27
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue