LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 05-12-2004, 01:20 AM   #1
mickstaff
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
vinay_s_s
Member
 
Registered: Jul 2003
Posts: 659

Rep: Reputation: 30
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!
 
Old 05-12-2004, 02:16 PM   #3
mickstaff
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.
cheers
 
Old 05-13-2004, 03:34 AM   #4
vinay_s_s
Member
 
Registered: Jul 2003
Posts: 659

Rep: Reputation: 30
lol ! silly mistakes huh?
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:11 AM.

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