473,898 Members | 3,815 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

checking if a file/stream exists


Cracow, 3.01.2005

Hello,

When opening a file stream in the append mode,
either a new file is created (if a specified file does not exist),
or an existing file is opened for adding stuff.
Is there any way to determine, after calling the open(filename,i os::app)
method, which of these two alternatives has occurred?
I would like to differentiate the program operation, depending
on whether I write to a new or existing file.

L.Bieniasz
*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Electrochemical Oxidation of Gaseous Fuels, |
| ul. Zagrody 13, 30-318 Cracow, Poland. |
| tel./fax: +48 (12) 266-03-41 |
| E-mail: nb******@cyf-kr.edu.pl |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistr y? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
Jul 22 '05 #1
14 5104
Leslaw Bieniasz wrote:

Cracow, 3.01.2005

Hello,

When opening a file stream in the append mode,
either a new file is created (if a specified file does not exist),
or an existing file is opened for adding stuff.
Is there any way to determine, after calling the open(filename,i os::app)
method, which of these two alternatives has occurred?
I would like to differentiate the program operation, depending
on whether I write to a new or existing file.


Your system may contain a system specific function which allows yo
to check for file existence.

Another way:
Try to open the file in non-append mode. If this works, then the
file does exist, if not ... Close the file and open it in append
mode. You then can now use the information you found out in
the first open attempt to guide your operations.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #2
You can use the ifstream::tellg () (part of STL) to find the size of the
file. If it is greater than zero then the file must have existed
before you opened it. If the file size is zero then you can assume that
the file is new..Hope it helps.

-Shan

Jul 22 '05 #3
Shan wrote:

You can use the ifstream::tellg () (part of STL) to find the size of the
file. If it is greater than zero then the file must have existed
before you opened it. If the file size is zero then you can assume that
the file is new..Hope it helps.


Not really.
tellg() returns some number which can only be used for a seekg().
No one gives a guarantee of what this number represents. It can
be the number of bytes read so far, it need not be that way.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #4
>Not really.
tellg() returns some number which can only be used for a seekg().

Correct. I missed that. I wanted to give a portable solution, which i
think is not possible. I think stat() function can be used , even
though it is a C function.

Jul 22 '05 #5

"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:41******** *******@gascad. at...
Shan wrote:

You can use the ifstream::tellg () (part of STL) to find the size of the
file. If it is greater than zero then the file must have existed
before you opened it. If the file size is zero then you can assume that
the file is new..Hope it helps.


Not really.
tellg() returns some number which can only be used for a seekg().
No one gives a guarantee of what this number represents. It can
be the number of bytes read so far, it need not be that way.


Also a file with size zero might already exist.

-Mike
Jul 22 '05 #6

"Shan" <sh*******@redi ffmail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Not really.
tellg() returns some number which can only be used for a seekg().

Correct. I missed that. I wanted to give a portable solution, which i
think is not possible. I think stat() function can be used , even
though it is a C function.


'stat()' is an extension provided by some implementations ,
where it is applicable. It's not a standard C or C++ function.

-Mike
Jul 22 '05 #7
> When opening a file stream in the append mode,
either a new file is created (if a specified file does not exist),
or an existing file is opened for adding stuff.
Is there any way to determine, after calling the open(filename,i os::app)
method, which of these two alternatives has occurred?
I would like to differentiate the program operation, depending
on whether I write to a new or existing file.


There is a way with standard C++ but it is cumbersome considering what you
want to do.
Open the file for input. If the open succeeds, it must exist.

Stephen Howe


Jul 22 '05 #8


Hello,

On Mon, 3 Jan 2005, Karl Heinz Buchegger wrote:
When opening a file stream in the append mode,
either a new file is created (if a specified file does not exist),
or an existing file is opened for adding stuff.
Is there any way to determine, after calling the open(filename,i os::app)
method, which of these two alternatives has occurred?
I would like to differentiate the program operation, depending
on whether I write to a new or existing file.


Your system may contain a system specific function which allows yo
to check for file existence.

Another way:
Try to open the file in non-append mode. If this works, then the
file does exist, if not ... Close the file and open it in append
mode. You then can now use the information you found out in
the first open attempt to guide your operations.


That's what I have been doing, but I feel this is not elegant.
Aren't there a better way?

L.B.

*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Electrochemical Oxidation of Gaseous Fuels, |
| ul. Zagrody 13, 30-318 Cracow, Poland. |
| tel./fax: +48 (12) 266-03-41 |
| E-mail: nb******@cyf-kr.edu.pl |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistr y? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
Jul 22 '05 #9
Leslaw Bieniasz wrote:
Your system may contain a system specific function which allows yo
to check for file existence.

Another way:
Try to open the file in non-append mode. If this works, then the
file does exist, if not ... Close the file and open it in append
mode. You then can now use the information you found out in
the first open attempt to guide your operations.


That's what I have been doing, but I feel this is not elegant.
Aren't there a better way?


Not that I know of.
Elegance is in the eye of the beholder.
Come on, we are talking about 3 or 4 lines of code. You can
wrap them up in a function of your own.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
114222
by: Geiregat Jonas | last post by:
is using if(open("file",O_EXCL) != -1){ printf("File does exists")}else{printf("file does not exists"); } a good way of checking if a file exists or not, if not how should I do it ?
1
5435
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved completely/succsessfully on the client's computer before updating some metadata on the server (file downloaded date for instance) However, All examples i have tried, and all examples I have found that other people says works - doesn't work for me :-(
18
4370
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't get it to upload a file to the FTP server. I just get a "Cannot connect to remote server" error after this TRY: s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
0
468
by: JD | last post by:
Hello Everyone, I am trying to set up a media kit page where when a user comes to the page for the first time and click on the image they wish to download they are brought to a registration page. I can get that part to work successfully for me, but this is the part does not work the way I want it to work. After the user has entered in their registration information the download for the file begins, once the download is done I want...
17
8048
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm supposed to be doing here. :) The problem: I am trying to use the SaveFileDialog class to get a filename, which is subsequently opened for writing (write access, read sharing, but using read/write sharing doesn't make the problem go away anyway). Sometimes, on the statement where I...
3
2148
by: tshad | last post by:
I have a function that downloads a file to the users computer and it works fine. The problem is that I then want the program to rename the file (file.move) to the same name plus todays date. The problem is that when you run the program it gets to the: Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name)
4
6933
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the function creates the file as expected. When I loop through my array I get the error - "ArgumentException was unhandled - Illegal characters in path" The value "C:\Temp.txt" is the first value in the array - as it works
1
13554
by: sandhyabhavani | last post by:
This article is used to zip a file or directory using vb.net. The classes and method to zip a file is availale in java.io, java.util, java.util.zip class library.To import these you have to add a reference vsjlib Library in .net component. .NET Classes used : Imports System.IO Imports java.io Imports java.util Imports java.util.zip
0
2157
by: nagashree | last post by:
Hi, i want to compress the file or directory i am able to do that can anybody help me to decompress the file when i double click on the zipped file i am getting the error message as compressed(zipped) folder is invalid or corrupted. I am using VB.NET .Please help me out in this regard...Thank you in advance. Here is the code for that. Public Class clsZip
0
9994
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9842
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10956
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7191
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5882
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6079
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4712
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3309
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.