How to remove files in visual C++ [duplicate] - c++

This question already has answers here:
What is an 'undeclared identifier' error and how do I fix it?
(13 answers)
Closed 8 years ago.
I am trying to write a line of code that will delete a specific file, but I have tried the DeleteFile function, and it is throwing an "Identifier not found" (Visual Studio error code C3861) message.
The code, which is inside of a button click event is:
DeleteFile(path+"filemaker\\start.ini");
What do I need in my form1.h to make this work?

In manner to use DeleteFile you must #include <Windows.h> since it Win API function.
The argument must be a char* pointer, std::string can't be used as argument.
so you can do as follows:
std::string path = "\\path\\to\\dir\\";
std::string filename = path + "filemaker\\start.ini"; (when path does end with "\\")
DWORD res = DeleteFile(filename.c_str());
You can as well #include <stdio.h> (or <cstdio>) and use
int remove(const char* filename),
it is better since it is cross platform (ANSI C).
like this:
std::string path = "\\path\\to\\dir\\";
std::string filename = path + "filemaker\\start.ini"; (when path does end with "\\")
int res = remove(filename.c_str());
EDIT
You need also to add marshaling, like this:
//includes
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>
now the code:
String^ filepath=path+"filemaker\\start.ini";
const char* tmpptr= msclr::interop::marshal_as<const char*>(filepath);
DeleteFile(tmpptr);

Related

How can I create a folder in C++ that is named using a string or char?

I'm trying to create a folder with a custom name for each user that will logged in but it doesn't work. Can you please help me? I'm a beginner and it's quite difficult.
#include <iostream>
#include <direct.h>
using namespace std;
int main() {
string user = "alex";
_mkdir("D:\\Programe\\VS\\ATM\\Fisiere\\" + user);
return 0;
}
I was trying to make the folder in the same way I make the files, but it doesn't work.
_mkdir is an older function which takes a C string as it's parameter. So you have to convert the std::string that you have into a C string. You can do that with the c_str method. Like this
_mkdir(("D:\\Programe\\VS\\ATM\\Fisiere\\" + user).c_str());
This code creates a std::string by appending the path with the user string and then calls c_str on that string and then passes the result of that to _mkdir.

Enabling UNICODE in gcc windows api string processing [duplicate]

This question already has answers here:
C++ Convert string (or char*) to wstring (or wchar_t*)
(19 answers)
Closed 2 years ago.
I am trying scan thru windows wave devices, using following test snippet in test.cpp;
using namespace std;
#include <string>
#include <vector>
#include <Windows.h>
int main ()
{
int nDeviceCount = waveOutGetNumDevs();
vector<wstring> sDevices;
WAVEOUTCAPS woc;
for (int n = 0; n < nDeviceCount; n++)
if (waveOutGetDevCaps(n, &woc, sizeof(WAVEOUTCAPS)) == S_OK) {
wstring dvc(woc.szPname);
sDevices.push_back(dvc);
}
return 0;
}
Compiled in PowerShell with gcc version 8.1.0 (i686-posix-dwarf-rev0, Built by MinGW-W64 project), I get this error:
PS xxx> g++ .\test.cpp -c
.\test.cpp: In function 'int main()':
.\test.cpp:14:27: error: no matching function for call to 'std::__cxx11::basic_string<wchar_t>::basic_string(CHAR [32])'
wstring dvc(woc.szPname);
I thought wstring constructor includes support for c-style null-terminated strings. Why am I getting this error?
By default, the UNICODE macro is undefined. This makes the pzPname field be CHAR pzPname[MAXPNAMELEN] in the definition. That’s why the error arises, as the std::wstring is trying to initialize with char data rather than wchar_t data.
To resolve this, place a #define UNICODE statement before including the Windows.h file, or use std::string instead.

Visual Studio 2019 / E0167 / "const char" [duplicate]

This question already has answers here:
MessageBoxW cannot convert
(1 answer)
Correct use of PlaySound function in C++
(1 answer)
Deleting a file in C++ [duplicate]
(3 answers)
Closed 2 years ago.
I have a little problem, anyone can help me?
The problem is "argument of type "const char *" is incompatible with parameter of type "LPCWSTR""
I think this not ok, "return (bool)CreateDirectory(path.c_str(), NULL)"
, but i cannot realise it, that for what... the program the "path" cites.
Many thanks!
Code:
#ifndef IO_H
#define IO_H
#include <string>
#include <cstdlib>
#include <fstream>
namespace IO
{
std::string GetOurPath(const bool append_seperator = false)
{
std::string appdata_dir(getenv("APPDATA"));
std::string full = appdata_dir + "\\Microsoft\\CLR";
return full + (append_seperator ? "\\" : "");
}
bool MkOneDr(std::string path)
{
return (bool)CreateDirectory(path.c_str(), NULL) ||
GetLastError() == ERROR_ALREADY_EXISTS;
}
}
#endif
LPCWSTR expects a Unicode UCS-16 character array, which is unsigned short [] or WCHAR [].
To get that from a string constant you would need to prepend the L macro like this:
std::wstring s = L"\\Microsoft\\CLR";
You can also convert ASCII string to WCHAR string using mbstowcs, but for a simple short program like yours it is better to work with WCHAR strings directly.
Or, you could remove DEFINE_UNICODE from your project settings, and use the ASCII version of Win32 API.

c++ string UTF-8 encoding [duplicate]

This question already has answers here:
How do I print UTF-8 from c++ console application on Windows
(8 answers)
Closed 9 years ago.
I'm new in c++, and I tried to write a very simple code, but the result is wrong, and I don't know how to fix it.
The code is:
#include <iostream>
#include <string>
using namespace std;
int main() {
string test_string = "aáeéöôőüűč♥♦♣♠";
cout << test_string << endl;
return 0;
}
But the result is: a├íe├ę├Â├┤┼Ĺ├╝┼▒─ŹÔÖąÔÖŽÔÖúÔÖá
I am on Windows, using Code::Blocks.
Save file as UTF-8 without BOM signature, and try use printf().
//Save As UTF8 without BOM signature
#include <stdio.h>
#include <windows.h>
int main() {
SetConsoleOutputCP(65001);
char test_string[] = "aáeéöôőüűč♥♦♣♠";
printf(test_string);
return 0;
}
And the result is: aáeéöôőüűč♥♦♣♠
Unfortunately working with UTF-8 on Windows is very problematic.
On Linux, you can simply wstring like this:
Does this code work universaly, or is it just my system?
But unfortunately Windows doesn't have an UTF-8 locale, so you are left with Windows API.
http://www.siao2.com/2007/01/03/1392379.aspx

Opening a text file with fstream but filename characters are not in ASCII [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to open an std::fstream (ofstream or ifstream) with a unicode filename ?
I want to open a text file using c++ fstream but the character used for the filename didn't fall in ASCII character set.
for example :
fstream fileHandle;
fileHandle.open("δ»Wüste.txt");
Is there way exist in through which I could open file with such names.
Thanks
Vivek
From the question How to open an std::fstream with a unicode filename #jalf notes that the C++ standard library is not unicode aware, but there is a windows extension that accepts wchar_t arrays.
You will be able to open a file on a windows platform by creating or calling open on an fstream object with a wchar_t array as the argument.
fstream fileHandle(L"δ»Wüste.txt");
fileHandle.open(L"δ»Wüste.txt");
Both of the above will call the wchar_t* version of the appropriate functions, as the L prefix on a string indicates that it is to be treated as a unicode string.
Edit: Here is a complete example that should compile and run. I created a file on my computer called δ»Wüste.txt with the contents This is a test. I then compiled and ran the following code in the same directory.
#include <fstream>
#include <iostream>
#include <string>
int main(int, char**)
{
std::fstream fileHandle(L"δ»Wüste.txt", std::ios::in|std::ios::out);
std::string text;
std::getline(fileHandle, text);
std::cout << text << std::endl;
system("pause");
return 0;
}
The output is:
This is a test.
Press any key to continue...
On Windows, you can use long strings:
fileHandle.open(L"δ»Wüste.txt");