Reputation: 339
Hello in first I will give some details, I' m working in:
Ubuntu , in c language and my compiler is gcc.
I was make a lisle test of my skills because I'm not so good with pointers so I tried my luck and I get a weird output.
I will show my code now.
#include<stdio.h>
#include<string.h>
#define F1 ".file.txt"
typedef struct{
char par1[30];
int par2;
int par3;
int par4;
char* par5[3];
}S1;
//this was the structure I was working
void initiate_S1(S1*a,char part1[],int* part2,int *part3, int*part4,char part51[],char part52[],char part53[]);
//this function will start the struct parameters
void get_S1(S1*a);
//this function will print struct parameters
void save_S1_on_file(S1*a);
//this will save all parameters of a structure in file
void search_S1_on_file(S1*a);
//This function will search for a first parameter of structure and if
//find it fill the other parameters
int main(){
S1 a;
int part2,part3,part4;
char part1[30],part51[15],part52[15],part53[15];
scanf("%s %d %d %d %s %s %s",part1,&part2,&part3,&part4,part51,part52,part53);
initiate_S1(&a,part1,&part2,&part3,&part4,part51,part52,part53);
get_S1(&a);
save_S1_on_file(&a);
search_S1_on_file(&a);
get_S1(&a);
return 0;
}
//this main is only to test the functions
void initiate_S1(S1*a,char part1[],int* part2,int *part3, int*part4,char part51[],char part52[],char part53[]){
strcpy(a->par1,part1);
a->par2=*part2;
a->par3=*part3;
a->par4=*part4;
a->par5[0]=part51;
a->par5[1]=part52;
a->par5[2]=part53;
}
void get_S1(S1*a){
printf("%s\n%d\n%d\n%d\n%s %s %s\n",a->par1,a->par2,a->par3,a->par4,a->par5[0],a->par5[1],a->par5[2]);
}
void save_S1_on_file(S1*a){
FILE *af;
af=fopen(F1,"a");
fprintf(af,"%s;%d;%d;%d;%s;%s;%s;\n",a->par1,a->par2,a->par3,a->par4,a->par5[0],a->par5[1],a->par5[2]);
fclose(af);
}
void search_S1_on_file(S1*a){
FILE *af;
char s[100];
char*token;
af=fopen(F1,"r");
int n=2;
while(fgets(s,100,af)!=NULL){
token=strtok(s,";");
if(strstr(a->par1,token)!=NULL){
n=0;
token=strtok(NULL,";");
a->par2=atoi(token);
token=strtok(NULL,";");
a->par3=atoi(token);
token=strtok(NULL,";");
a->par4=atoi(token);
a->par5[0]=strtok(NULL,";");
a->par5[1]=strtok(NULL,";");
a->par5[2]=strtok(NULL,";");
break;
}
else
n=1;
}
if(n==1)
printf("The S1 no exist\n");
}
input:
name 15 3 2 tag1 tag2 tag3
output:
name
15
3
2
tag1 tag2 tag3
name
15
3
2
%��j��2 j��2 tag3
For some reason when I try fill the char pointers from file string appears this weir output why appear this weird chars and whats is wrong in my code?
Upvotes: 1
Views: 138
Reputation: 19864
You are good until you call search_S1_on_file()
Inside this for the array of pointers a->par5
you assign the address of the local variable s
.
a->par5[0]=strtok(NULL,";");/* The string being broken into tokens is a local array */
a->par5[1]=strtok(NULL,";");
a->par5[2]=strtok(NULL,";");
Once you exit this function the memory allocated for the array s
is freed so accessing this memory location you are bound to get garbage values because this is UB.
In order to fix this keep a char array
in main and pass it to the function search_S1_on_file()
along with other requierd parameters and that should work.
Alternatively you can try allocating memory on heap for the array s
like
char *s = malloc(100); /* Take care to free this memory accordingly */
Upvotes: 1