Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

c - Delete a file named "NUL" on Windows

I ran a program on Windows 7 that was compiled under Cygwin and passed "NUL" as an output file name. Instead of suppressing output it actually created a file named "NUL" in the current directory. (Apparently it expects "/dev/null", even on Windows.) Now I'm stuck with this "NUL" file that I cannot delete!

I've already tried:

  • Windows Explorer - error: "Invalid MS-DOS function" (yes, that is seriously what it says!)
  • Command prompt using "del NUL" - error: "The filename, directory name, or volume label syntax is incorrect."
  • Deleting the entire directory - same deal as just deleting the file
  • remove() in a C program - also fails

How can I get rid of these NUL files (I have several by now), short of installing the full Cygwin environment and compiling a C program under Cygwin to do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Open a command prompt and use these commands to first rename and then delete the NUL file:

C:> rename \.C:..NUL. deletefile.txt
C:> del deletefile.txt

Using the \. prefix tells the high-level file I/O functions to pass the filename unparsed to the device driver - this way you can access otherwise invalid names.

Read this article about valid file / path names in Windows and the various reserved names.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...