473,385 Members | 1,712 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.

ANSI remove() and MS DeleteFile()

1
Dear all,
I encounter a problem about DeleteFile() with ANSI stdio functions.
It always fails to delete file and sets ERROR_SHARING_VIOLATION.
I know I can use remove() instead of DeleteFile(), but it make me confused.
Is MS File Management Functions and ANSI stdio functions not compatible?
the code is:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <windows.h>
  3. int main(int argc, char* argv[])
  4. {
  5.     FILE* fp = fopen("C:/testFS.txt", "wt");
  6.     fclose(fp);
  7.                 // remove("C:/testFS.txt");
  8.     if (DeleteFile(_T("C:/testFS,txt")) == 0)
  9.     {
  10.                                 // It always be here. why?
  11.         DWORD err = GetLastError();
  12.         if (err == ERROR_SHARING_VIOLATION)
  13.         {
  14.             printf("ERROR_SHARING_VIOLATION\n");
  15.         }
  16.         else
  17.         {
  18.             printf("Other error: %d\n", err);
  19.         }
  20.     }
  21.     return 0;
  22. }
Sep 14 '07 #1
1 5907
weaknessforcats
9,208 Expert Mod 8TB
What compiler are you using???

Your code won't compile in Visual Studio.NET 2005. The _T macro was not defined because you did not include tchar.h. And if you are using the tchar mappings, you are using a main() instead of a _tmain().

Beyond that, your file paths have the slashes in the wrong direction. That means the file never opened (you didn't check that) and never deleted.

I fixed your code and it works fine now. There is no incompatibilty between Microsoft and ANSI C file I/O.

Here is your fixed code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. #include <fstream>
  4. #include <stdio.h>
  5. #include <windows.h>
  6. #include <tchar.h>
  7. int _tmain(int argc, char* argv[])
  8. {
  9.     FILE* fp = fopen(TEXT("C:\\testFS.txt"), TEXT("wt"));
  10.     fclose(fp);
  11.                 // remove("C:/testFS.txt");
  12.     if (DeleteFile(TEXT("C:\\testFS.txt")) == 0)
  13.     {
  14.                                 // It always be here. why?
  15.         DWORD err = GetLastError();
  16.         if (err == ERROR_SHARING_VIOLATION)
  17.         {
  18.             _tprintf(TEXT("ERROR_SHARING_VIOLATION\n"));
  19.         }
  20.         else
  21.         {
  22.             _tprintf(TEXT("Other error: %d\n"), err);
  23.         }
  24.     return 0;
  25. }
  26.  
I stll didn't add the the check for fopen().
Sep 14 '07 #2

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

Similar topics

3
by: Tom Bates | last post by:
I can successfully delete files using fso.DeleteFile when in an ASP script. But in Session_OnEnd, where I'd *really* like to clean up files, it appears that DeleteFile doesn't work. I've tried...
11
by: andrea azzini | last post by:
I've got an ASP3 (IIS6) site, in which some scripts need to generate temporary files in order to work. Now, the fact is: I would like those temporary files to be deleted when a user's session ends...
3
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't...
100
by: Roose | last post by:
Just to make a tangential point here, in case anyone new to C doesn't understand what all these flame wars are about. Shorthand title: "My boss would fire me if I wrote 100% ANSI C code" We...
11
by: Nicholas R. Markham | last post by:
When I compile with the -ansi option (using gcc 3.2.3) I'm warned of an implicit declaration of function 'finite'. Does this mean that finite() is not part of ANSI C, or that I didn't include a...
9
by: pdmountaineer | last post by:
hi all, I made an application in MATLAB, but to make code fast and efficient, I use ANSI C routines, which I compile under MATLAB as MEX files (MATLAB executables). To protect my C routines,...
10
by: Michael B. Trausch | last post by:
Alright... I am attempting to find a way to parse ANSI text from a telnet application. However, I am experiencing a bit of trouble. What I want to do is have all ANSI sequences _removed_ from...
6
by: dunleav1 | last post by:
I have an application that uses the old join syntax instead of the SQL92 standards join syntax. I need to justify changing the code to the new standard. Is there any performance issue related to...
8
by: Simon Klein | last post by:
Hi, I've got a problem. I wrote a Program with a Signalhandler Routine. If i use the compile flags "-ansi -pedantic -Wall", "(void) signal(SIGCHLD,Handler)" catches the first signal but ignores...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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.