how to create a new file in c++ using windows.h [closed] - c++

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;
}

Related

C2065 's' undeclared identifier [closed]

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.

C++ CreateFile does not found .txt file in same folder as .exe [closed]

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?

How can I write a piece of code in C++ to find the full path of a file that is on the system path? [closed]

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 7 years ago.
Improve this question
Using C++ ( in visual studio 2013), how can I write a piece of code that searches the system path for a particular file and returns the file's full path? I only have the base name of the file. By system path, I mean the path environment variable in windows (you can see the path by typing "set path" on a command window).
For example. I like to have something like,
file_name = "my_test.xml"
path = find_file(fine_name);
Thanks in advance
I figured out the answer. Below is a piece of code that does exactly what I was looking for:
#include <iostream>
#include <Shlwapi.h>
wchar_t* get_file_full_path(const wchar_t file_name[]){
wchar_t buf[MAX_PATH];
wcscpy_s(buf, wcslen(file_name) + 1, file_name);
if (PathFindOnPath(buf, NULL)) {
std::wcout << file_name << " found." << std::endl;
std::wcout << buf << std::endl;
}
else {
std::wcout << "Could not find the file " << file_name << " on the system path.";
}
return buf;
}
You can check the system path first, and if it does not find it there, you need to do a search for the file in the machine.
Depending on you dev environment, there are different ways of doing it. Assuming you are using a Windows system, this is an example:
#include <Windows.h>
#include <iostream>
int main()
{
WIN32_FIND_DATA file;
HANDLE search_handle=FindFirstFile(L"C:\\*",&file);
if (search_handle)
{
do
{
std::wcout << file.cFileName << std::endl;
}while(FindNextFile(search_handle,&file));
CloseHandle(search_handle);
}
}

How detect spacebar? [closed]

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
I have a program in c++ where I have to detect spacebar, how can I do? I see that I need the function getch(), but in my program I havenĀ“t conio.h. Exist other solution?
With getchar I need press intro, exist other form that I press only spacebar?
For example, can I introduce a intro without press intro???
Simple Example
#include <iostream>
using namespace std;
int main()
{
char ans;
do
{
//code
}
while(getchar() != 32 || getchar() != ' ');
cout << "Space pressed" << endl;
return 0;
}
Compiled Code
Windows.h:
if(GetAsyncKeyState(VK_SPACE) & 0x80000000)
MessageBox(NULL, "Spacebar pressed!", "TEST", MB_OK);
See no conio.h

Extract HTML source code linux library [closed]

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.