LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   File already exists (https://www.linuxquestions.org/questions/programming-9/file-already-exists-180575/)

mickstaff 05-12-2004 01:20 AM

File already exists
 
Hi and thanks in advance for reply.

My question is I'm writing a C program on Red hat 9 using Unix, and want to open a file for writting, i'm checking to see if file already exists, if it does i want to prompt the user if they want to over write or give new file a new name, if they want to over write no probs.
If they don't want to over write i want them to input a new file name and assign new file name to the file open command.
What would be nice is for the user to input name store in a variable and use the variable in the file open command. i.e. fgets(name,30, stdin);
fp = fopen("name", "w");
Then i can use this variable again if wanted.

Or any other ideas would be appreciated

vinay_s_s 05-12-2004 01:42 AM

ok here goes nothing:

char name[30]="";

rep:
scanf(" %s", name);

if ( (fp=fopen(name, "r")) != NULL)
{
// file present
//code goes here...
fclose (fp);
}
else
{

//file not found
printf("enter new name");
goto rep; ///note u can do without goto, but it will be a bit complicated
}

//rest of code goes here:



//this is just an idea and not the exact program with all the syntax, hop u got it!

mickstaff 05-12-2004 02:16 PM

cheers vinay_s_s, works a treat, i was close but i enclosed variable in argument in double quotations.
cheers

vinay_s_s 05-13-2004 03:34 AM

lol ! silly mistakes huh?


All times are GMT -5. The time now is 05:21 PM.