1

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;
}
mah011
  • 11
  • 1
  • 4
    For *one* problem, please read [Why is “while( !feof(file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – Some programmer dude Nov 21 '22 at 12:18
  • Pass the address of the *new* string to your function, along with its maximum allowed size. Otherwise you are forced to `malloc` in your function to return something. And in fact you are returning the input parameter. – Costantino Grana Nov 21 '22 at 12:20
  • 3
    You need to think about what you're doing. Right now, `new_data` has a local array, `res`. It stores into that array, but never uses it. In other words, it's utterly pointless. If you want to return an array (or string), you have two choices: (1) Have the caller pass the array to the function, then write to *that* array, or (2) Have the function allocate the array with `malloc`, and return it. In the latter case, the caller is responsible for calling `free` to free it when it's done with it. – Tom Karzes Nov 21 '22 at 12:24
  • @ Costantino Grana what do you mean by that? – mah011 Nov 21 '22 at 12:28
  • To me it's unclear what you are doing but... should `strcpy(res,new_data(file_name));` be `strcpy(str,new_data(file_name));` ? – Support Ukraine Nov 21 '22 at 12:44
  • Opening the file with `a+` set the file pointer to the end of the file for appending. Therefore your loop to check if the filename is already present will never work. – Anthony Kelly Nov 21 '22 at 12:54
  • 2
    Does this answer your question? [Returning string from C function](https://stackoverflow.com/questions/25798977/returning-string-from-c-function) – Tom Karzes Nov 21 '22 at 13:16
  • Tnx for your help... Have found the problem.. – mah011 Nov 22 '22 at 18:27

0 Answers0