I\'m having problems on storing the strings in the structure pointercorrectly. E
ID: 3614242 • Letter: I
Question
I'm having problems on storing the strings in the structure pointercorrectly. Every time it executes, it has some strange ' ' (newline) in the output, while it shouldn't be. Below is my funnyfile37 20 I love watching basketball on tv
33 12 Cadrin is funny and cute
13 31 She have 3 kids and a wife
09 22 Your kids are spoil and childish
07 88 I need to understand this fast
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct test
{
int mansa;
int man;
char *j;
};
int main(void)
{
FILE *fp;
struct testgroup[5];
char arr[60];
int i = 0;
int len;
fp = fopen("fi.dat","r");
if(fp == NULL)
return 0;
while(fscanf(fp, "%d",&(group[i].mansa)) != EOF)
{
fscanf(fp, "%d", &(group[i].man));
fgets(arr, 59, fp);
len = strlen(arr);
if(len > 30)
{
arr[30] = '';
len = 30;
}
group[i].j = (char *)malloc(sizeof(char) * len);
strcpy(group[i].j, arr);
i++;
}
for(i=0;i<5;i++)
printf("%d %d %s", group[i].mansa, group[i].man, group[i].j);
fclose(fp);
return 0;
}
Explanation / Answer
/*The required changes are highlighted in the code */ #include #include #include struct test { int mansa; int man; char *j; }; int main(void) { FILE *fp; struct testgroup[5]; char arr[60]; int i = 0; int len; fp = fopen("fi.dat","r"); if(fp == NULL) return 0; while(fscanf(fp, "%d",&(group[i].mansa)) != EOF) { fscanf(fp, "%d", &(group[i].man)); fgets(arr, 59, fp); len = strlen(arr); if(len > 30) { arr[30] = ' '; arr[31]=''; len = 30; } group[i].j = (char *)malloc(sizeof(char) * len); strcpy(group[i].j, arr); i++; } for(i=0;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.