i have a text file named "name_file.txt" where all my file names is listed in. I'm calling new*data function with a file name. I have to check is the file name is already exists.in(namefile) if exists i have two option over write or making a new file name.if i choose new file name then it is calling the new_*data function . but when function returning the string it is the same string which i have called with not the new one..so I don't know how can i get the new string in main function?
#include<stdio.h>
#include<string.h>
char *new_data(char *str)
{
FILE *name_file, *data_file;
name_file=fopen("name_file.txt","a+");
char file_name[30];
char option;
char res[30];
while(!feof(name_file))
{
fscanf(name_file,"%s",file_name);
if(strcmp(file_name,str)==0)
{
break;
}
}
if(strcmp(file_name,str)==0)
{
printf("file is already exist over write [y n]: ");
scanf(" %c",&option);
if(option=='y')
{
data_file=fopen(str,"w");
fclose(data_file);
}
else
{
printf("Enter the file name: ");
scanf(" %s",&file_name);
strcpy(res,new_data(file_name));
}
}
else
{
data_file= fopen(str,"w");
fprintf(name_file,"%s\n",str);
fclose(data_file);
}
fclose(name_file);
return str;
}
int main(int argc, char **argv)
{
char res[20];
strcpy(res,new_data(argv[1]));
printf("%s",res);
return 0;
}