Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
im asking for help resolving an error :(
my line of code is as follows
// folder name
std::string folder = "logs";
CreateDirectoryA(folder.c_str(), nullptr);
// time format
auto t = std::time(nullptr);
std::ostringstream timefmt;
timefmt << std::put_time(std::localtime(&t), "%Y%m%d_%H%M%S");
// filename
std::string filename = folder + "\\"s + "client_"s + std::to_string(CLIENT_GET_VERSION()) + "_"s + timefmt.str() + ".dmp"; /* HERE IS MY PROBLEM */
auto hFile = CreateFileA(filename.c_str(), GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile == INVALID_HANDLE_VALUE)
return;
try to add
using namespace std::literals
Maybe your "client_"s raises the issue. Make sure you've used #include <string> and using std::string_literals::operator""s.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
visual studio 2013 windows 7.
std::string filename1 = "hold.a";
std::ofstream filex(filename1, std::ios::binary);
filex << " Hello" << std::endl;
filex.close();
works just fine
std::string filename1 = "hold.txt";
std::ofstream filex(filename1, std::ios::binary);
filex << " Hello" << std::endl;
filex.close();
gives me a permission denied error on file creation. The only difference is the three letter extension on the output file.
Ifstream and fopen behave the same way.
Your both code works fine for me but You also can use Win32 API, Just include Windows.h and:
HANDLE hFile;
char DataBuffer[] = "This is some test data to write to the file.";
hFile = CreateFile("foo.txt", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
NULL);
DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
DWORD dwBytesWritten = 0;
WriteFile(hFile, DataBuffer, dwBytesToWrite, &dwBytesWritten, NULL);
Run the program As Administrator, btw (If you have admin access).
Thanks to all who helped me cover ground on this. The actual answer was the IT department had mucked with permissions and the machine needed to be reimaged.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to use CreateFile function, but it does not go as planned.
I did a simple test code :
#include <Windows.h>
#include <iostream>
#include <tchar.h>
using namespace std;
int main() {
HANDLE hFile;
hFile = CreateFile(_T("test.txt"), GENERIC_READ, NULL, NULL, OPEN_EXISTING, NULL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
cout << GetLastError() << endl;
Sleep(2000);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
I generate the .exe and place a file test.txt in the same folder.
When I execute the .exe I get getLastError() = 2 which means ERROR_FILE_NOT_FOUND
How is it even possible?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i'm using Linux, i need my program to Extract the HTML source code and put it into a string using C++ language , can you give me a library that can do this
Well the easy solution is:
#include <string>
#include <iostream>
#include <stdio.h>
std::string execu(char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
std::string result = execu("curl http://www.facebook.com");
But this is not considered safe unless you know the string passed is not going to blow anything up.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
what have of wrong here ? Work in my notebook, but not in my PC . . .
The two are 64-bits, Windows 7 ultimate.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
HKEY CH;
char File_Name[] = "C:\\Users\\RMS\\Desktop\\M.txt";
if(RegCreateKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&CH) != 0){
printf("Erro - RegCreateKey\n");
system("PAUSE");
return -1;
}
if(RegOpenKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&CH) != 0) // Abre a CH "Minha CH"
{
printf("Erro - RegOpenKey\n");
system("PAUSE");
return -1;
}
if(RegSetValueEx(CH,L"My_Value",0,REG_SZ,(LPBYTE) L"C:\\Users\\RMS\\Desktop\\M.txt",40) != 0)
printf("Erro - RegSetValue\n");
RegCloseKey(CH);
printf("\nsucesso !\n");
system("PAUSE");
return 0;
}
I found. . . Was only do this:
if(RegSetValueEx(CH,L"My_Value",0,REG_SZ,(LPBYTE) L"C:\\Users\\RMS\\Desktop\\M.txt",60) != 0)
VERY THANK GUYS !!
Your problem is that the HKLM registry key is only writable by elevated programs, and your program is not running elevated. The reason it works on one machine and not the other is that one has user access control turned down/off while the other doesn't.
If you ran the program from an elevated command prompt it will work.
Additionally, you're using L"" for the strings, but using a RegSetValueEx call with 40, which is 40 bytes, and will actually cut off the M.txt on the text you're setting (if it works at all). Where you initialize the .txt file you should use:
TCHAR File_Name[] = L"C:\\Users\\RMS\\Desktop\\M.txt";
Then for the RegSetValueEx you do:
RegSetValueEx(CH,L"My_Value",0,REG_SZ,(LPBYTE) File_Name, sizeof File_Name + sizeof(TCHAR))
This makes it the number of bytes that corresponds to the filename, plus the final NULL TCHAR.
what error do you get?
try
RegOpenKeyEx
instead of RegOpenKey, since that's for 16 bit windows.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to know if there are any methods other than file.open(); in <windows.h>
As you specifically state windows.h, the WINAPI function CreateFile() can used to create a file. At the end of the link there are multiple examples of using CreateFile(), but here is a simple one:
#include <windows.h>
#include <iostream>
int main()
{
HANDLE h = CreateFile("test.txt", // name of the file
GENERIC_WRITE, // open for writing
0, // sharing mode, none in this case
0, // use default security descriptor
CREATE_ALWAYS, // overwrite if exists
FILE_ATTRIBUTE_NORMAL,
0);
if (h)
{
std::cout << "CreateFile() succeeded\n";
CloseHandle(h);
}
else
{
std::cerr << "CreateFile() failed:" << GetLastError() << "\n";
}
return 0;
}