473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to check if file exists ?

i want to know how to check if the file already exists before creating the file?
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. puts("please enter a file name to create:\n");
  4. scanf("%s", &file_name);
  5. create_pointer = fopen( file_name, "ab");
  6. if( create_pointer == NULL)    
  7. //create file//
  8. else
  9. {
  10. //file already exist// how would you check if the file already exists ?? 
  11. }
  12.  
  13.  
  14.  
  15.  
Jan 10 '11 #1

✓ answered by horace1

I was thinking
Expand|Select|Wrap|Line Numbers
  1. puts("please enter a file name to create:\n");
  2. scanf("%s", &file_name);
  3.  
  4. if( create_pointer = fopen(file_name. "r") != NULL)
  5. printf("file already exist");
  6. else
  7. {
  8. create_pointer = fopen( file_name, "ab");
  9.  
  10. // write to the file
  11.  
  12. }
  13.  
what do you want to do if the file already exists?
in the second fopen you can open it with "wb" as you know it does not exist

8 3615
horace1
1,510 Expert 1GB
Before you attempt to open it for writing you could open it for reading - if it exists the open will be sucessful otherwise if it does not exist the open would fail.
Jan 10 '11 #2
do you mean something like this ?
Expand|Select|Wrap|Line Numbers
  1. puts("please enter a file name to create:\n");
  2. scanf("%s", &file_name);
  3.  
  4. if( create_pointer = fopen(file_name. "r"); 
  5. printf("file already exist"); 
  6. else
  7. {
  8. if(create_pointer != fopen(file_name"r"); 
  9. create_pointer = fopen( file_name, "ab");
  10. }
  11.  
Jan 10 '11 #3
horace1
1,510 Expert 1GB
I was thinking
Expand|Select|Wrap|Line Numbers
  1. puts("please enter a file name to create:\n");
  2. scanf("%s", &file_name);
  3.  
  4. if( create_pointer = fopen(file_name. "r") != NULL)
  5. printf("file already exist");
  6. else
  7. {
  8. create_pointer = fopen( file_name, "ab");
  9.  
  10. // write to the file
  11.  
  12. }
  13.  
what do you want to do if the file already exists?
in the second fopen you can open it with "wb" as you know it does not exist
Jan 10 '11 #4
Oralloy
988 Expert 512MB
Alternately, you can use stat(), and get everything you ever wanted about the file. Use http://linux.die.net/man/2/stat to get the necessary information.

Cheers!
Jan 10 '11 #5
thanks allot for your hello i appreciate it
Jan 11 '11 #6
Oralloy
988 Expert 512MB
What are you trying to achieve?

If you want to do what is commonly called "create or append", I think you might be able to use the fopen()options string "a+b".

Here's a reference on fopen that explains the options string fairly clearly.
Jan 11 '11 #7
thanks it worked using fopen() was fine did mot need to use stat() thanks for your help much appreciated
Jan 11 '11 #8
Oralloy
988 Expert 512MB
Glad you got it worked out.

Cheers!
Oralloy
Jan 11 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Joseph Oget | last post by:
If I use the code below in a VB.NET form, even if the file exists, and is then executed, the Else statement produces the "Sorry cannot find the File " error message. The MessageBox.Show(...
10
by: Raymond | last post by:
Hi All: To find a file exists using the file name, I have tow routings on UNIX system. 1. access(2) 2. lstat(2) This tow function also can do. When the return value is "-1" and errno is...
6
by: Alex Chan | last post by:
Hi Group, I encountered something strange with File.Exists(string path). This function is simply to check whether a file exists in certain path. However, if the path starts with "file:\" in the...
2
by: Chris Fink | last post by:
I am using the System.IO.File class to determine if a file exists on a network share. The File.Exists method keeps returning false, even though the file does exist. The MSDN documentation...
10
by: Ricardo Luceac | last post by:
Hi all. I'm having a problem with this, I have look if a file exists, if don't wait till it is created and if it exists I need to open it. I do the following: for (; ; ) {
14
by: John Salerno | last post by:
What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I just want to check if a file of a...
10
by: Julia | last post by:
Hi, there, I am trying to append a binary file by using: FILE *strean; stream = fopen( "c:\temp.dat", "ba+" )); Is there a way that I can check if the file exists or not before fopen,...
7
by: sprash | last post by:
Newbie question: I'm trying to determine if a file physically exists regardless of the permissions on it Using File.Exists() returns false if it physically exists but the process does not...
8
by: Sweetiecakes | last post by:
In our series of "probably something simple"... I am doing a File.Exists operation to check if a file exists. Let's assume that this file is C:/test.txt. Now, if I do...
2
Manikgisl
by: Manikgisl | last post by:
HI. How to check File exists in Web Share C# try { WebRequest request = HttpWebRequest.Create("http://www.microsoft.com/NonExistantFile.aspx"); request.Method = "HEAD"; // Just get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.