Reputation: 1
I use a struct with array variables to contain my data set, which is read from a .dat file. After I import the data and store it in a struct array, I try to print the data set out on the main function. It shows something unknown word on the other element of the array set. However, I need the rest of the empty array element to store something else. So, how can I remove the unknown word from the rest of the array element and become empty? Did I do something wrong with the code? Here is the code. Any suggestions on that? I appreciate any help that can provide.
struct Meeting{
char team[40];
char date[40];
char time[40];
char duration[40];
};
void batch_input(struct Meeting marr[100]);
int main(int argc, char *argv[]){
struct Meeting marr[100];
batch_input(marr);
for(int i =0; i<10; i++){
printf("showmain: %s %s %s
%s\n",marr[i].team,marr[i].date,marr[i].time,marr[i].duration);
}
}
void batch_input(struct Meeting marr[100]){//define a team array of 100 records
#define LSIZE 130
#define RSIZE 10
char line[RSIZE][LSIZE];
char fname[20];
FILE *fptr = NULL;
int i = 0;
int tot = 0;//count the number of line
int crow =0; //count for which row is empty
int count=0;
for(int row=0; row<100; row++){ // find the marr row until the row is empty
if(strlen(marr[row].team) !=0){
crow++;
}else{
break;
}
}
count =crow;
printf("Input the filename to be opened : ");
scanf("%s",fname);
fptr = fopen(fname, "r");
while(fgets(line[i], LSIZE, fptr))
{
line[i][strlen(line[i]) - 1] = '\0';
i++;
}
fclose(fptr);
tot = i;
for(i = 0; i < tot; ++i)
{
sscanf( line[i], "%s %s %s %s"
,marr[count].team,marr[count].date,marr[count].time,marr[count].duration);
count++;
}
printf("\nMeeting request has been imported!\n\n");
}
This is the content of the test.dat file.
Team_A 2022-04-25 09:00 2
Team_B 2022-04-26 10:00 1
Team_C 2022-04-26 14:00 3
Team_A 2022-04-26 09:00 4
Team_D 2022-04-27 09:00 3
Team_E 2022-04-27 11:00 2
When I execute the code it shows:
Input the filename to be opened : test.dat
Meeting request has been imported!
showmain: Team_A 2022-04-25 09:00 2
showmain: Team_B 2022-04-26 10:00 1
showmain: Team_C 2022-04-26 14:00 3
showmain: Team_A 2022-04-26 09:00 4
showmain: Team_D 2022-04-27 09:00 3
showmain: Team_E 2022-04-27 11:00 2
Cin2b:
showmain: � qv 0yj�� _product_info_filter.dylib
showmain: yj�� ���� H{j��
showmain:
what I expect to start from marr[6] is empty output,like this
showmain: Team_A 2022-04-25 09:00 2
showmain: Team_B 2022-04-26 10:00 1
showmain: Team_C 2022-04-26 14:00 3
showmain: Team_A 2022-04-26 09:00 4
showmain: Team_D 2022-04-27 09:00 3
showmain: Team_E 2022-04-27 11:00 2
showmain:
showmain:
showmain:
showmain:
Upvotes: 0
Views: 103
Reputation: 409216
The function batch_input
(which you forgot to declare) you read six entries from the file.
But in the main
function you print ten. The last four will be uninitialized and very likely give you undefined behavior.
The batch_input
function should return the number of items actually read from the file:
unsigned batch_input(struct Meeting *marr){
unsigned tot = 0;
...
return tot;
}
Then you can use the returned value in your output loop to only print the structures that were actually read from the file.
As any decent beginners book, tutorial or class should have taught you, all local variables (defined inside functions) start out uninitialized. The values will be indeterminate (and you should look at it as garbage), and depending on type and how you use these values it could lead to undefined behavior.
If you want initialized local variable, then you need to explicitly initialize those variables.
In your case for example like:
struct Meeting marr[100] = { 0 }; // All members of all structures will be "zero"
Upvotes: 1