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 |
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! |
cheers vinay_s_s, works a treat, i was close but i enclosed variable in argument in double quotations.
cheers |
lol ! silly mistakes huh?
|
All times are GMT -5. The time now is 05:21 PM. |