Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-12-2004, 01:20 AM   #1
LQ Newbie
Registered: Jan 2004
Posts: 18

Rep: Reputation: 0
Lightbulb 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
Old 05-12-2004, 01:42 AM   #2
Registered: Jul 2003
Posts: 658

Rep: Reputation: 30
ok here goes nothing:

char name[30]="";

scanf(" %s", name);

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

//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!
Old 05-12-2004, 02:16 PM   #3
LQ Newbie
Registered: Jan 2004
Posts: 18

Original Poster
Rep: Reputation: 0
cheers vinay_s_s, works a treat, i was close but i enclosed variable in argument in double quotations.
Old 05-13-2004, 03:34 AM   #4
Registered: Jul 2003
Posts: 658

Rep: Reputation: 30
lol ! silly mistakes huh?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
file not found by glob? - file exists? rgdatit Linux - Newbie 2 07-29-2005 02:16 AM
SIOCADDRT: File exists SIOCCADDRT: File Exists Failed to bring up eth0. opsraja Linux - Networking 0 01-10-2005 08:29 AM
Linker problem: can't find a file, but the file exists atlep Programming 5 08-16-2004 06:15 AM
c++ check if file exists Genjix Programming 3 03-15-2004 12:08 AM
file exists? raven Programming 7 10-26-2003 07:44 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:28 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration