Question

This is the text input for my c program. 2010161 5 123 1a 876 2a 654...

This is the text input for my c program.

2010161 5 
        123 1a  876 2a  654 5d  751 3b  431 2b
3000177 4
        817 2c  123 1a  555 5e  864 5d
1000007 4
        645 7a  643 6f  645 7a  123 1a
2010101 1
        431 2b
1232343 5
        643 6f  751 3b  234 2a  256 4b  654 4c
1856512 7
        555 4f  715 3g  732 2f  914 8g  754 6h  759 2h  723 1g

The meaning of text input is shown as below:

customer_no total_number_of_items_for_this_customer

item_no [row][column]    item_no [row][column]      item_no [row][column]      item_no [row][column]

Based on text input above which all will be copy into text file, in my C program, how should I write my scanf in my main function in order to successfully read information from text input above to my struct function listed below?

typedef struct{
   int buyer_id;
   int no_of_items;
   itemsinfo_t order[MAXORDER];
}buyer_t;   

typedef struct{
   int           item_no;
   int           row;
   char column;  
}itemsinfo_t;

*MAXORDER is stated as value 10, meaning that each customer only able purchase 10 maximum items.

*scanf function should able to read up to 100 customers if another text input with identical format is used in future.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>

#define MAXORDER 10

typedef struct{
    int            item_no;
    int            row;
    char         column;  
}itemsinfo_t;

typedef struct{          
    int buyer_id;
    int no_of_items;
    itemsinfo_t   order[MAXORDER];
}buyer_t;

void print(buyer_t*, int);
int main(int argc, char *argv[])
{
   char filename[200];
   char discard;
   buyer_t buyer[100]; // create array of 100 buyer_t
   int index = 0; // count number of buyers
   FILE *file = NULL;
   int j;
   printf("Enter file name to read buyer data: ");
   scanf("%s", filename);
        file = fopen(filename, "r");// open file
        if (file == NULL) {
                printf("Can't read file.\n");
                exit(0);
        }

        while (!feof (file)) {
                fscanf (file, "%d %d", &buyer[index].buyer_id, &buyer[index].no_of_items);// use fscanf to read data from file
       for (j = 0; j < buyer[index].no_of_items; j++) {
           fscanf(file, "%d %d %c", &buyer[index].order[j].item_no, // read all items from file
               &buyer[index].order[j].row, &buyer[index].order[j].column);
       }
       index++;
       fscanf(file, "%c", &discard); // discard newline char
   }
          
        fclose (file);// close the file;
   print(buyer, index - 1);
}

void print(buyer_t* buyer, int index)
{
   int i = 0, j;
   for (; i < index; i++) {
       printf("buyer_id: %d\nno_of_item: %d\n", buyer[i].buyer_id, buyer[i].no_of_items);// print buyers information
       for (j = 0; j < buyer[i].no_of_items; j++)// print all items
           printf("%d %d %c ", buyer[i].order[j].item_no,
                                buyer[i].order[j].row, buyer[i].order[j].column);
       printf("\n");
   }
}sanjiv@sanjiv-VirtualBox: $ sanjiv@sanjiv-VirtualBox:-$ sanjiv@sanjiv-VirtualBox: $ gcc item.c -o item sanjiv@sanjiv-VirtualB

Add a comment
Know the answer?
Add Answer to:
This is the text input for my c program. 2010161 5 123 1a 876 2a 654...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming Question Hi, I have the following code and in my submission I'm supposed to...

    C Programming Question Hi, I have the following code and in my submission I'm supposed to only include function definitions (i.e. implementations) in the file. However, I'm not NOT required to write the main, struct definitions and function prototypes. Could someone help me fix this code? Text Version: #include<stdio.h> #include<stdlib.h> #include <string.h> struct ip_address { int octet_1; int octet_2; int octet_3; int octet_4; }; typedef struct ip_address ip_address_t; void print_ip_address(ip_address_t ip1){ printf("%d.%d.%d.%d", ip1.octet_1,ip1.octet_2,ip1.octet_3,ip1.octet_4); } int is_valid(ip_address_t ip1){ if(ip1.octet_1 < 0...

  • I need a basic program in C to modify my program with the following instructions: Create...

    I need a basic program in C to modify my program with the following instructions: Create a program in C that will: Add an option 4 to your menu for "Play Bingo" -read in a bingo call (e,g, B6, I17, G57, G65) -checks to see if the bingo call read in is valid (i.e., G65 is not valid) -marks all the boards that have the bingo call -checks to see if there is a winner, for our purposes winning means...

  • I have to write a C program to assign seats on each flight of the airline’s...

    I have to write a C program to assign seats on each flight of the airline’s only plane (capacity: 40 seats, in 10 rows). For the sake of simplicity, assume that each row has 4 seats labeled A, B, C, D. Your program should display the following menu of alternatives: Please type 1 for "first class" Please type 2 for "business class" Please type 3 for “economy class”. If the person types 1, then your program should assign a seat...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT