Question

In: Computer Science

I got a problem in C struct. I made a aa.txt file and read data by...

I got a problem in C struct. I made a aa.txt file and read data by struct. I am learning how to pass variable and value between function by pointers.

The first line in txt (integer 5) means I need to read the next five lines data to struct

aa.txt

5

111

222

333

444

555

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int i,totalRow;
char inBuf[20];
char str[20];
void readData();
void sum() ;

typedef struct demo{

int num;
}demo1;
  

int main()
{
  
struct demo demo1;
struct demo *sptr;
sptr= &demo1;
sum(sptr);
return 0;
}
  

void readData(struct demo *ptr)
{
  
int i,j;
FILE *fin = NULL;
fin = fopen("aa.txt", "r");   
if (fin == NULL)
{
    printf("Could not open the file.\n");
   }
fgets(str, sizeof(str), fin);
       sscanf(str,"%d ",&totalRow);   
printf("%d\n",totalRow);
   for(i = 0; i {
fgets(str,sizeof(str),fin);
sscanf(str,"%d ", &ptr->num);
       printf("%d\n", ptr->num);   
}
fclose(fin);   
return;
}

//find the sum   
void sum(struct demo *ptr)
{
int result;
readData(ptr);
printf("the sum is %d\n", ptr->num);
return;  
}

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

My code of sum function is wrong, I have no idea how to pass the data to sum function from readData function.

readData function can read the data from the file, it works.

Maybe the problem is still in the readData function, because I guess I did not store the data to memory(I am not sure)

Thanks

Solutions

Expert Solution

Below is the working code , leave comments in case of any doubt :

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int i,totalRow;

char inBuf[20];

char str[20];

void readData();

void sum() ;

typedef struct demo{

int num;

}demo1;

int main()

{

struct demo demo1;

struct demo *sptr;

sptr= &demo1;

sum(sptr);

return 0;

}

void readData(struct demo *ptr)

{

int i,j, temp = 0;

FILE *fin = NULL;

fin = fopen("aa.txt", "r");

if (fin == NULL)

{

printf("Could not open the file.\n");

}

fgets(str, sizeof(str), fin);

sscanf(str,"%d ",&totalRow);

printf("%d\n",totalRow);

for(i = 0; i < totalRow ; ++i ){

fgets(str,sizeof(str),fin);

sscanf(str,"%d ", &ptr->num);

temp += ptr->num; //Adding each value of file

printf("%d\n", ptr->num);

}

fclose(fin);

ptr->num = temp;

return;

}

//find the sum

void sum(struct demo *ptr)

{

int result;

readData(ptr);

printf("the sum is %d\n", ptr->num);

return;  

}

Output screentshot:


Related Solutions

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...
Write a c program that creates a struct to be able to read a .img file...
Write a c program that creates a struct to be able to read a .img file and a .pat file.
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?
I made the command cp code in c language. But when I copy a file, file...
I made the command cp code in c language. But when I copy a file, file permissions are not copied equally. So I want to copy the file authority as well. What should I do? #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { struct stat st; char ch; int src, dst; if(argc != 3) { printf("argument error \n"); printf("usage: ./a.out src dest \n"); exit(0); } src = open(argv[1], O_RDONLY); if(src == -1){ perror("open...
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
Write a C++ program to read a data file containing the velocity of cars crossing an...
Write a C++ program to read a data file containing the velocity of cars crossing an intersection. Then determine the average velocity and the standard deviation of this data set. Use the concept of vector array to store the data and perform the calculations. Include a function called “Standard” to perform the standard deviation calculations and then return the value to the main function for printing. Data to use. 10,15,20,25,30,35,40,45,50,55. Please use something basic.
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
HW_6d - Write a struct object to a binary file. Create a new C++ project and...
HW_6d - Write a struct object to a binary file. Create a new C++ project and name it as:   Cats Create a Source.cpp file. Declare a struct named Cat. (in the Source.cpp file, or in a separate header file) Each Cat object has a name and age. The name data member is a c_string. The age data member is an integer. Ask the user to enter 3 cats. Use a while loop to read the information about one cat entered...
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of...
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of the word. Next, use the struct to store the word read from a text file call “word.txt” and the length of the word. Print out the word x number of times based on the length of the word as shown below. Also, print a statement with the length and determine if the string length has an odd number or even number of characters. You...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT