Question

In: Computer Science

I have a C problem. I need to read the data of a txt file to...

I have a C problem. I need to read the data of a txt file to array by struct, then use these data to sum or sort.

The txt file and the struct like

aa.txt

1 2

3 4

*****************************

struct aaa{
int num1,num2;
}bbb[2];

num1 for 1,3, num2 for 2 4

I made a void readfile () function for read data. How can I pass the numbers to another function ( void sum () and void sort() ) for sum and sort

I finished the void readfile(), but I have no idea how to pass the value to another function.

I tries to use the pointer of struct, can you give me a general idea?

Because I must move data to an array, is my array of struct wrong? I mean if I need to use malloc

Thanks

Solutions

Expert Solution

To pass a struct variable you have to create a pointer and use malloc and pass the pointer to the function and access it using an index.

C++ Code:

#include <stdio.h>
#include <stdlib.h>

//Declaring a struct type
struct aaa{
int num1, num2;
};

//main method
int main()
{
//allocating memory to a struct pointer to store 2 struct variables of type "aaa"
struct aaa *bbb = malloc(sizeof(struct aaa)*2);
//open the file
FILE *f1;
f1 = fopen("aa.txt", "r");
readFile(f1, bbb); //read the file into the struct
sum(bbb); //calculate sum
sort(bbb); //sort the values

fclose(f1); //close the file
return 0;
}

void readFile(FILE *f1, struct aaa *bbb){
int i=0;
for(i=0; i<2; i++){
//Loop through the file and store in the struct
fscanf(f1, "%d %d",&bbb[i].num1,&bbb[i].num2);
printf("Values are: %d %d\n", bbb[i].num1, bbb[i].num2);
}
}

void sum(struct aaa *bbb){
int i=0;
for(i=0; i<2; i++){
//Calculate sum of each struct variable
printf("Sum of struct %d is %d\n", (i+1), bbb[i].num1+bbb[i].num2);
}
}

void sort(struct aaa *bbb){
int i=0;
int temp=0;
for(i=0; i<2; i++){
//Sorting the values using a temp variable
if(bbb[i].num2 < bbb[i].num1){
temp = bbb[i].num2;
bbb[i].num2 = bbb[i].num1;
bbb[i].num1 = temp;
}
//Display struct after sorting
printf("After sorting the values are: %d %d\n", bbb[i].num1, bbb[i].num2);
}
}

Sample Output:

Values are: 1 2
Values are: 3 4
Sum of struct 1 is 3
Sum of struct 2 is 7
After sorting the values are: 1 2
After sorting the values are: 3 4


Related Solutions

C++ Question: we need to read speech from .txt file. Steve Jobs delivered a touching and...
C++ Question: we need to read speech from .txt file. Steve Jobs delivered a touching and inspiring speech at Stanford's 2005 commencement. The transcript of this speech is attached at the end of this homework description. In this homework, you are going to write a program to find out all the unique tokens (or words) used in this speech and their corresponding frequencies, where the frequency of a word w is the total number of times that w appears in...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C language problem. Suppose I open I file and read a integer 24. And I want...
C language problem. Suppose I open I file and read a integer 24. And I want to store them into a array byte []. The answer should be byte[0] = 0x9F. How can I do that?
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have...
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards...
I need C++ program that Read an input file of text.txt one word at a time....
I need C++ program that Read an input file of text.txt one word at a time. The file should consist of about 500 words. The program should remove all punctuations,keep only words. Store the words in a built-in STL container, such as vector or map.Can someone help with any additional comments that I can understand the logic?thank you
(Write/read data) Write a Program in BlueJ to create a file name Excersise12_15.txt if it does...
(Write/read data) Write a Program in BlueJ to create a file name Excersise12_15.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. Integers are separated by spaces in the file. Read data back from the file and display the data in increasing order. After writing the file to disk, the input file should be read into an array, sorted using the static Arrays.sort() method from the Java API and then displayed in the...
This is all in Python. Also, the names come from a .txt file that i have...
This is all in Python. Also, the names come from a .txt file that i have created with 3 names on it(Jim,Pam,Dwight). Main Function. Write a program where the user enters a name. Using the read function, check to see if the name is in the text document. If it is, respond back to the user the name is found within the list. Read Function. The read function should return the contents of the text document as a list. EXAMPLE...
language c++(Data structure) You have to read a file and store a data in character array...
language c++(Data structure) You have to read a file and store a data in character array ( you cant initialize a character array, you have to code generically) code must be generic u must read a file onece u cant use built in function etc string, code in classes if u initialized a char array or ur code doesn't run i will dislike and report u you can use link list to store data
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT