Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm trying to use _unlink. I'm a C

Status
Not open for further replies.

hugh7

Technical User
Joined
Mar 2, 2003
Messages
24
Location
US
I'm trying to use _unlink. I'm a C++ beginner. I use the following code but get the error message "invalid argument"

I followed the example in the docs and put void inside the main brackets but this gets a warning message so I took it out .

The code compiles but fails. I certainly need a little guidance. And I'm starting 'small'

Thanks hugh

#include <stdio.h>
int main( )
{
if (_unlink(&quot;c:\tump.txt&quot;) == -1 )
perror( &quot;could not delete it&quot;) ;
else
printf( &quot;deleted that file \n&quot;);

}
 
1. In C++ main() has the signature int main(int,char*[]).
2. Therefore main() must return value: insert
Code:
return 0;
statement.
3. Include C++ Standard <cstdio> header instead of old C <stdio.h>.
4. Use Standard C++ library
Code:
remove(filename)
function instead of platform-specific _unlink().
5. Write file name with backslashes as
Code:
&quot;c:\\tump.txt&quot;
: backslash id escape character in C/C++ text and char literals.
Ooh! Is it all?...
Good luck!
 
Hi ArkM
Thanks for the tips. I will give it a try . I'm unclear about the parameters inside main() I'm not passing any parameters since I hard code the file name in my example.
I completely forgot about the escape character for the backslash.

Hugh
 
>hugh7
all you havve written in the first post is correctly for C. For C++ you only should return an integer from function main and everything will be ok.

Ion Filipski
1c.bmp
 
> main() must return value
AND
> you only should return an integer from function main

Code:
int main()
{
}

is a perfectly valid function in C++. The main function has the special property that, if you don't specify a return value, it returns 0. Some compilers (VC++, for one) give a warning or even an error about nor returning an integer, but those compilers are wrong. You never have to explicitly give main's return value. It's never a syntax error not to do so.
 
Hi to all,

I followed ArkM's instructions and all works well !
Thanks for you quick responses.

Hugh
 
Ion: not all have written was correct in C: TAB instead of t in the file name was incorrect. Why C? Evidently, hugh7 wrote about C++.

chipperMDV: yes, C++ Standard allows
Code:
int main()
signature and does not require
Code:
return
stmt in main(). But let's read our (sub)forum topic...

hugh7: thanks for thanks, full speed ahead!
 
ArkM: Sorry if you think I went off-topic, but first, trying to stop the spread of misinformation should always be &quot;on-topic,&quot; and second, you were actually the first one to bring up the issue of a return value from main.

I think that any time someone makes a false claim, correcting it is fair ground. In this case, two people made the same false claim, and correction was necessary.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top