| View previous topic :: View next topic |
| Author |
Message |
Negima I post too much
Reputation: 6
Joined: 22 May 2007 Posts: 2221
|
Posted: Sat Mar 29, 2008 10:35 pm Post subject: How to delete a file in C++? |
|
|
| Can someone give me the API and header in order to delete a file in C++ at a certain directory on my computer? I looked on msnd but they only showed VB
|
|
| Back to top |
|
 |
Renkokuken GO Moderator
Reputation: 4
Joined: 22 Oct 2006 Posts: 3249
|
Posted: Sat Mar 29, 2008 10:59 pm Post subject: |
|
|
| Holy crap, there's an API called DeleteFile()
|
|
| Back to top |
|
 |
Negima I post too much
Reputation: 6
Joined: 22 May 2007 Posts: 2221
|
Posted: Sun Mar 30, 2008 1:03 am Post subject: |
|
|
| Code: | #include <iostream>
#include <windows.h>
int main()
{
std::cout << "Hello World\n";
std::cout << "This is the sum of 4 and 5\n" ;
std::cout << 5 + 4 << "\n";
Sleep(1000);
DeleteFile(C:\kka.txt);
system("pause");
Sleep(2000);
return 0;
} |
y no work
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sun Mar 30, 2008 1:30 am Post subject: |
|
|
it takes a string, guess what you need around it. (also, extra bonus hint that you'll also need to have it work: \\)
and don't use system("pause")
|
|
| Back to top |
|
 |
Negima I post too much
Reputation: 6
Joined: 22 May 2007 Posts: 2221
|
Posted: Sun Mar 30, 2008 1:36 am Post subject: |
|
|
| slovach wrote: | it takes a string, guess what you need around it. (also, extra bonus hint that you'll also need to have it work: \\)
and don't use system("pause") | quotes?
|
|
| Back to top |
|
 |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
Posted: Sun Mar 30, 2008 5:33 am Post subject: |
|
|
| Code: | | DeleteFile("C:\\Something"); |
You need to use the \\ instead of just one \ and you also need to put it in quotes.
_________________
What dosen't kill you, usually does the second time. |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sun Mar 30, 2008 6:46 am Post subject: |
|
|
| cstdio has a function called remove..
|
|
| Back to top |
|
 |
Negima I post too much
Reputation: 6
Joined: 22 May 2007 Posts: 2221
|
Posted: Sun Mar 30, 2008 1:13 pm Post subject: |
|
|
| Code: | #include <iostream>
#include <windows.h>
int main()
{
std::cout << "Hello World\n";
std::cout << "This is the sum of 4 and 5\n" ;
std::cout << 5 + 4 << "\n";
Sleep(1000);
DeleteFile("C:\\kka.txt");
system("pause");
Sleep(2000);
return 0;
} |
why doesn't this work then
|
|
| Back to top |
|
 |
Renkokuken GO Moderator
Reputation: 4
Joined: 22 Oct 2006 Posts: 3249
|
Posted: Sun Mar 30, 2008 1:20 pm Post subject: |
|
|
| Are you running Vista? Do you have proper permissions to delete the file?
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sun Mar 30, 2008 2:11 pm Post subject: |
|
|
should work fine.
also:
| Code: | #include <iostream>
#include <windows.h>
int main()
{
std::cout << "Hello World\n";
std::cout << "This is the sum of 4 and 5\n";
std::cout << 5 + 4 << "\n";
DeleteFile("C:\\kka.txt");
std::cin.ignore();
std::cin.sync();
return 0;
} |
|
|
| Back to top |
|
 |
Negima I post too much
Reputation: 6
Joined: 22 May 2007 Posts: 2221
|
Posted: Sun Mar 30, 2008 7:39 pm Post subject: |
|
|
Gives me this error when I try to compile
| Code: | ------ Build started: Project: Hello World, Configuration: Debug Win32 ------
Compiling...
Hello World.cpp
c:\users\joseph\documents\visual studio 2008\projects\hello world\hello world\hello world.cpp(10) : error C2664: 'DeleteFileW' : cannot convert parameter 1 from 'const char [11]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Build log was saved at "file://c:\Users\Joseph\Documents\Visual Studio 2008\Projects\Hello World\Hello World\Debug\BuildLog.htm"
Hello World - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
|
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sun Mar 30, 2008 8:00 pm Post subject: |
|
|
To Fix this you have 3 choices:
before the string put L
use _T("string") - Best Choice (include tchar.h)
use TEXT("string")
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sun Mar 30, 2008 9:04 pm Post subject: |
|
|
| Go into your project settings and change it from unicode to multi-byte under general.
|
|
| Back to top |
|
 |
|