Question

C program Read file problem: For example, I have an input.txt file like this: q 1...

C program Read file problem:

For example, I have an input.txt file like this:
q 1 tq 4 p1 10 p2 5 p3 7 p4 20 p5 17 p6 9 p7 3 p8 11 p9 15 p10 1

I want the num of q is in one array, tq is in one array, the order of process(ie,p1,p2) is in one array, and process time is in one array(process time is the number after like p1,p2). There may many q,tq,p so it may be like this
q 1 tq 4 p1 30 p2 10 p3 24 p4 20 p5 17 p6 4 p7 7 p8 11 p9 8 p10 9 p11 5 p12 6 p13 3 p14 2 p15 1

q 2 tq 5 p1 1 p2 2 p3 2 p4 9 p5 8 p6 5 p7 12 p8 11 p9 15 p10 1 p11 4 p12 8 p13 22 p14 21 p15 30

q 3 tq 30 p1 30 p2 10 p3 24 p4 20 p5 17 p6 4 p7 7 p8 11 p9 8 p10 9 p11 5 p12 6 p13 3 p14 2 p15 1

how can I get the array from input file? It must be C program.

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXR 50
#define MAXC 1024

// Function to read dates from file and stores it in character matrix process
// Returns number of rows by using pointer
void readFile(char process[][MAXC], int *r)
{
// Creates a file pointer to open the file Process.txt in read mode
FILE *rFile = fopen("Process.txt", "r");

// Checks if file is unable to open then display error message
if (rFile == NULL)
{
// Display error message
puts("Error: Could not open files");
exit(0);
}// End of if
// To store the data read from file
char data [MAXC];

// Extracts data from file
while (!feof(rFile))
{
// Extracts data from file
fscanf(rFile, "%s,", data);

// Checks if data is "q"
if(strcmp(data, "q") == 0)
{
// Increase the row by one
*r = *r + 1;
// Concatenates data with space
strcat(data, " ");
// Copies data to process row index position
strcpy(process[*r], data);
}// End of if condition

// Otherwise
else
{
// Concatenates data with space
strcat(data, " ");
// Concatenates process r index position string with data
strcat(process[*r], data);
}// End of else
}// End of while loop
// Close the file
fclose(rFile);
}// End of function

// Function to display contents of matrix
void show(char process[][MAXC], int row)
{
// Loop variable
int r, c;

// Loops till number of rows
for(r = 0; r < row; r++)
{
// Displays each row data
printf("\n %s", process[r]);
// Displays new line
printf("\n");
}// End of for loop
}// End of function

// main function definition
int main()
{
// Creates a character matrix
char process[MAXR][MAXC];
// To store number of rows
int row = -1;
// Calls the function to read data
readFile(process, &row);
// Calls the function to display data
show(process, row+1);
return 0;
}// End of main function

Sample Output:

q 1 tq 4 p1 30 p2 10 p3 24 p4 20 p5 17 p6 4 p7 7 p8 11 p9 8 p10 9 p11 5 p12 6 p13 3 p14 2 p15 1

q 2 tq 5 p1 1 p2 2 p3 2 p4 9 p5 8 p6 5 p7 12 p8 11 p9 15 p10 1 p11 4 p12 8 p13 22 p14 21 p15 30

q 3 tq 30 p1 30 p2 10 p3 24 p4 20 p5 17 p6 4 p7 7 p8 11 p9 8 p10 9 p11 5 p12 6 p13 3 p14 2 p15 1

Process.txt file contents

q 1 tq 4 p1 30 p2 10 p3 24 p4 20 p5 17 p6 4 p7 7 p8 11 p9 8 p10 9 p11 5 p12 6 p13 3 p14 2 p15 1 q 2 tq 5 p1 1 p2 2 p3 2 p4 9 p5 8 p6 5 p7 12 p8 11 p9 15 p10 1 p11 4 p12 8 p13 22 p14 21 p15 30 q 3 tq 30 p1 30 p2 10 p3 24 p4 20 p5 17 p6 4 p7 7 p8 11 p9 8 p10 9 p11 5 p12 6 p13 3 p14 2 p15 1

Add a comment
Know the answer?
Add Answer to:
C program Read file problem: For example, I have an input.txt file like this: q 1...
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++) I need to write a priority queue(where the processes inside are listed as example: P25:2...

    (C++) I need to write a priority queue(where the processes inside are listed as example: P25:2 , 25 being a randomly assigned ID(1-50) and 2 being a randomly assigned priority number(1-10). Once the priority queue is full(10 max), it will transfer the highest priority process to a waiting queue. Once the waiting queue is full(7 max), one process(highest priority) will transfer to a ready queue where the system would perform the process in a round robin style. Once 25 processes...

  • 1. Fill in the table below for the following compounds- molecules and polyatomic ions. For each...

    1. Fill in the table below for the following compounds- molecules and polyatomic ions. For each compound, calculate the number of available electrons for all the atoms in the molecule and determine what the central atom in the molecule will be. Central Atom Total Number Molecular of Available Formula Electrons HCN P1 P2 SO3 P3 P4 NO3 P5 P6 P7 P8 SIH4 P9 P10 SO2CI2 NH4 P11 P12 P13 P14 PO43 P15 P16 PF5 SF&C P18 P17 P19 P20 XeF2

  • In this exercise, you will illustrate a small network and calculate some of its basic properties....

    In this exercise, you will illustrate a small network and calculate some of its basic properties. For this case, it will be a social network though any network could do. You will calculate PageRank for the network. Given the following people (nodes) and relations (edges): Nodes: P1: Tom, P2: Jean, P3: Nat, P4: Steven, P5: Vijay, P6: Mary, P7: Oscar, P8: Michelle, P9: Jian, P10: Clark Edges: P1 -> P4 P2 -> P1, P2 -> P4 P3 -> P2, P4...

  • implement MLFQ using C++ please preferably linked list P1 {4,24,5,73,3,31,5,27,4,33,6,43,4,64,5,19,2} P2 {18,31,19,35,11,42,18,43,19,47,18,43,17,51,19,32,10} P3 {6,18,4,21,7,19,4,16,5,29,7,21,8,22,6,24,5} P4 {17,42,19,55,20,54,17,52,15,67,12,72,

    implement MLFQ using C++ please preferably linked list P1 {4,24,5,73,3,31,5,27,4,33,6,43,4,64,5,19,2} P2 {18,31,19,35,11,42,18,43,19,47,18,43,17,51,19,32,10} P3 {6,18,4,21,7,19,4,16,5,29,7,21,8,22,6,24,5} P4 {17,42,19,55,20,54,17,52,15,67,12,72,15,66,14} P5 {5,81,4,82,5,71,3,61,5,62,4,51,3,77,4,61,3,42,5} P6 {10,35,12,41,14,33,11,32,15,41,13,29,11} P7 {21,51,23,53,24,61,22,31,21,43,20} P8 {11,52,14,42,15,31,17,21,16,43,12,31,13,32,15} will compute the overall wait times, response times, and turnaround times for each process and averages for FCFS the processes follow {CPU, IO, CPU, IO, ...} output should look like this Now Running: P1 Ready Queue: Process Burst P2 18 P3 6 P4 17 P5 5 P6 10 P7 21 P8 11 Now In I/O: Process...

  • Python 3 5. (16 points) Determine the big-O running time of each of the following functions:...

    Python 3 5. (16 points) Determine the big-O running time of each of the following functions: def pi (a) for i in range(len (a)): print (a[i]) for i in range(len(a)): print (ali]) def p2(a): for i in rangeClen(a)): for j in a: print (ati].j) def p3(a): for i in a: for j in a: print (i,j) def p4(a): for i in range(len(a)): pi(a) def p5(a): for i in range(len(a)): p3 (a) def p6(a): for i in range(len(a)): p5(a) def p7...

  • Topic Round Robin Answer the following using the First Come First Serve Scheduling Algorithm SHOW COMPLETE...

    Topic Round Robin Answer the following using the First Come First Serve Scheduling Algorithm SHOW COMPLETE SOLUTION 1. Process AT BT P1 1 10 P2 0 15 P3 2 8 P4 5 7 P5 6 5 P6 9 3 P7 8 4 P8 3 5 P9 4. 20

  • Memory Allocation a) Is it possible to have both internal and external fragmentation with fixed p...

    Memory Allocation a) Is it possible to have both internal and external fragmentation with fixed partition memory allocation? How about with dynamic/variable memory partition allocation? Explain. (1 point) b) Suppose we have 1000K of memory where the first 100K is reserved. Also suppose that the following processes have been allocated memory in the following order:                 P1: 100K, P2: 100K, P3: 25K, P4: 200K, P5: 200K, P6: 75K, P7: 100K. Create a diagram of memory. Suppose that P2, P4, and P6...

  • 9. Consider the two substitution reactions 1 - 2 shown below. Select the product(s) that will...

    9. Consider the two substitution reactions 1 - 2 shown below. Select the product(s) that will be produced in each reaction from the list provided below (P1-P8). If more than one product is formed, indicate their approximate ratios. Ust of products to consider OCH CH, ONa DMSO low temp. reaction 1 reaction 2 P7 P8 a) reaction 1: P1 + P2 in 1:1 ratio, reaction 2: Pl only b) reaction 1: P5 only; reaction 2: P7 only c) reaction 1:...

  • Answer all questions in this Section. 1. Read the following and answer questions that follow. The...

    Answer all questions in this Section. 1. Read the following and answer questions that follow. The following information was taken from Masego Leero’s books. 01/01/2015 Started business with P20, 000 cash 03/01/2015 Deposited P15, 000 cash into the bank account 05/01/2015 Bought furniture on credit P3, 000 from Supreme 07/01/2015 Bought goods P7, 000 paying by cheque. 09/01/2015 Sold goods P6, 000 by cash 11/01/2015 Sold goods on credit to Lesego R P1, 000, Lorato L P2, 000 and Kago...

  • (d) Pre-emptive priority scheduling [4 marks]: Consider the following set of processes, with the length of...

    (d) Pre-emptive priority scheduling [4 marks]: Consider the following set of processes, with the length of the CPU-burst time given in milliseconds: Process Burst Priority Arrival Time P1 8 3 0 P2 3 4 1 P3 6 2 3 P4 3 1 5 P5 1 5 7 P6 3 8 14 P7 8 5 18 (d-1) Draw Gantt chart illustrating the execution of these processes using pre-emptive priority (a smaller priority number implies a higher priority) [2 marks] 00   01  ...

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