Question

Sales person Month6 Month7 Month8 Month9 Month10 Month11 Adam Ellen Eva Justin 2000 200 4000 125 3000 225 2500 110 3000 65 40

how to use c language to read the date from a file called "figures.txt"? includ name and number,And how to declare a set of variables which are name of the salesperson, net income, discount.

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

Figures.txt :- (separate every word with a space ' ')

Sales Person Month6 Month7 Month8 Month9 Month10 Month11
================================================================
Adam 2000 200 3000 65 4000 100 5000 650 7000 500 3000 125
Ellen 4000 125 4520 65 9000 152 5600 458 8900 356 4850 139

Here is the C code :-

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char * readline(FILE *fp, char *buffer)
{
    int ch;
    int i = 0;
    size_t buff_len = 0;

    buffer = malloc(buff_len + 1);
    if (!buffer) return NULL; 

    while ((ch = fgetc(fp)) != '\n' && ch != EOF)
    {
        buff_len++;
        void *tmp = realloc(buffer, buff_len + 1);
        if (tmp == NULL)
        {
            free(buffer);
            return NULL;
        }
        buffer = tmp;

        buffer[i] = (char) ch;
        i++;
    }
    buffer[i] = '\0';

    if (ch == EOF && (i == 0 || ferror(fp)))
    {
        free(buffer);
        return NULL;
    }
    return buffer;
}



int main()
{
    char *fileName = "figures.txt";
    FILE *file = fopen(fileName, "r");
    char *s;
    int length=0,i=0;
    char *salesPerson="";
    int netIncome=0;
    int discount=0;
    int t=0;
    while ((s = readline(file, 0)) != NULL)
    {
        if(i>=2){
            length=strlen(s);
            char * token = strtok(s, " ");
            t=0;
            salesPerson=token;
            while( token != NULL ) {
                token = strtok(NULL, " ");
                if(t%2!=0){
                    netIncome=netIncome+atoi(token);
                }
                else{
                    discount=discount+atoi(token);
                }
                t++;
            }
            printf("%s\t%d\t%d\n",salesPerson,netIncome,discount);
        }
        free(s);
        i++;
    }
    return 0;
}

Output :-

gcc HomeworkLib3.c
./a.exe   
Adam 1640 24000
Ellen 2935 60870

Add a comment
Know the answer?
Add Answer to:
how to use c language to read the date from a file called "figures.txt"? includ name...
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
  • How to do this question? Question 1 (15 Marks) Table below shows aggregate demand and supply...

    How to do this question? Question 1 (15 Marks) Table below shows aggregate demand and supply schedules for an imaginary economy Real domestic output demanded Price level Real domestic output supplied (RM billions) (RM billions) 3000 350 9000 4000 300 8000 5000 250 7000 6000 200 6000 7000 150 5000 8000 100 4000 A. Using the data in the table, graph the aggregate demand and aggregate supply curves for this economy. (7 Marks) B. Determine the equilibrium price level and...

  • Question 1 (15 Marks) Table below shows aggregate demand and supply schedules for an imaginary economy....

    Question 1 (15 Marks) Table below shows aggregate demand and supply schedules for an imaginary economy. Real domestic output demanded Price level Real domestic output supplied (RM billions) (RM billions) 3000 350 9000 4000 300 8000 5000 250 7000 6000 200 6000 7000 150 5000 8000 100 4000 A. Using the data in the table, graph the aggregate demand and aggregate supply curves for this economy. (7 Marks) B. Determine the equilibrium price level and output for this economy in...

  • ans fully Question 1 (15 Marks) Table below shows aggregate demand and supply schedules for an...

    ans fully Question 1 (15 Marks) Table below shows aggregate demand and supply schedules for an imaginary economy. Real domestic output demanded Price level Real domestic output supplied (RM billions) (RM billions) 3000 350 9000 4000 300 8000 5000 250 7000 6000 200 6000 7000 150 5000 8000 100 4000 A. Using the data in the table, graph the aggregate demand and aggregate supply curves for this economy. (7 Marks) B. Determine the equilibrium price level and output for this...

  • 1. (35 pts) Specific fuel consumption rate curves are given for three different engines: A, B...

    1. (35 pts) Specific fuel consumption rate curves are given for three different engines: A, B and C. Power and Torque curves for Engine 'B' are also given at Page 3. Please answer the following questions for Engine 'B'. a) (5 pts) Find the power and torque when it runs at the engine rpm of minimum specific fuel consumption rate. b) (5 pts) Find the torque and specific fuel consumption rate when it runs at the engine rpm of maximum...

  • 2. The controller of Hall Industries has collected the following monthly expense data for use in...

    2. The controller of Hall Industries has collected the following monthly expense data for use in analyzing the cost behavior of maintenance costs. total total Month Maintenance Costs Machine Hours 1 2460 3500 2 3000 4000 3 3600 6000 4 4500 7900 5 3200 5000 6 4620 8000 (a) Determine the fixed- and variable-cost components using the high-low method. ................................. 500 P18.1A (LO1, 2) The controller of Rather Production has collected the following monthly expense data for analyzing the cost...

  • A car weighing 13 kN has an engine with performance characteristics as shown in Figure 1...

    A car weighing 13 kN has an engine with performance characteristics as shown in Figure 1 below. The car equipped with a 5-speed manual transmission system with 90% efficiency, final drive gear ratio (final drive) is 3.0. The equivalent weight of the rotating parts of driveline at 3500 rpm is 300 N with a radius of gyration of 10.1 cm. Each tyres is 66 cm in diameter with a radius of gyration of 25.2 cm and weighs 220.4 N. Each...

  • In regards to question 3, will cavitation occur based on the NPSH available vs NPSH required?...

    In regards to question 3, will cavitation occur based on the NPSH available vs NPSH required? Water is pumped from a reservoir to a large receiver vessel which is maintained at p3 = 6 bar through the operation of the pump and a high pressure nitrogen supply that bleeds into the tank through a regulator. The water enters through an opening at the top of the vessel. A drain is provided at the low point in the system. Elevations are...

  • 1/9 1 Piping system (14 points) Water is pumped from a reservoir to a large receiver...

    1/9 1 Piping system (14 points) Water is pumped from a reservoir to a large receiver vessel which is maintained at p4 6 bar (absolute) through the operation of the pump and a high pressure nitrogen supply that bleeds into the tank through a regulator. The water enters through an opening at the top of the vessel. A drain is provided at the low point in the system. Elevations are as shown. All valves are gate valves (k = 0.15)...

  • 1/9 1 Piping system (14 points) Water is pumped from a reservoir to a large receiver...

    1/9 1 Piping system (14 points) Water is pumped from a reservoir to a large receiver vessel which is maintained at p4 6 bar (absolute) through the operation of the pump and a high pressure nitrogen supply that bleeds into the tank through a regulator. The water enters through an opening at the top of the vessel. A drain is provided at the low point in the system. Elevations are as shown. All valves are gate valves (k = 0.15)...

  • (100 points) Water at 25°C is pumped at a rate of 0.075m'/s from a reservoir 20m above a pump to a free di...

    (100 points) Water at 25°C is pumped at a rate of 0.075m'/s from a reservoir 20m above a pump to a free discharge (this means discharged to atmosphere) 35m above the pump. The pressure on the intake side of the pump is 150kPa and the pressure on the discharge side of the pump is 450kPa. The pipe between the reservoir and the pump is 3 meters of 5in diameter cast iron. From the pump to the discharge the pipe is...

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