Best method copying folders in C++ on Windows 7 - c++

I am working on a simple program to copy all files in folders from one drive to another using C++. Using the Windows API function CopyFile(). I have used the following code:
#include <iostream>
#include <windows.h>
int main()
{
std::cout << "File Copier Version 1";
CopyFile("U:\\whateverfile.file","U:\\whateverfile2.file",0);
return 0;
}
What is the best way to handle copying an entire directory and all the files in it? Any other advice on this? Problems I may encounter?

If you wish to do it on Windows with progress display and other features, without putting too much effort into it, look up IFileOperation::CopyItem. However, it requires Vista or later.

Related

How do I create a directory inside the directory where my .cpp file is placed?

I am creating a directory in the desktop folder using this code.
#include <iostream>
#include <stdio.h>
#include <direct.h>
#include<string>
using namespace std;
int main()
{
string k,s="c:/Users/Dell/Desktop/";
cin>>k;
string p =s+k;
if (mkdir(p.c_str()) != 0)
{
cerr<<"Failed"<<endl;
}
return 0;
}
But this code will be specific to my pc only as it has the whole parent directory path.
I want to create a new folder inside my working directory where the main.cpp is placed, because we don't know where the user will place the cpp file.
I know there are a lot of solutions similar to this problem on the forum but I couldn't find or understand any that could help me here. Thanks.
Do you really mean the .cpp file?
You can get the filename as a string using the __FILE__ macro and then use std::filesystem::path to manipulate it, but are you sure this is really what you want to do? Are you expecting end users to compile this program in the location they want to run it?
You can also (in theory) get the path through which the program was executed using the argv argument to main, which seems more likely to be what you mean, but it's unreliable in a number of surprising ways ...

Error in ofStream, Qt 5.14.2 can 't write string into file

I am trying to write my program from VS (MVCS) for Qt(MinGb)
I am using ofstream and has a following code:
#include <fstream>
#include <string>
#include <iostream>
#include <exception>
using namespace std;
void WriteToFile(ofstream* fileToWrite, std::string StringNeedsToWrite)
{
if (fileToWrite)
{
if (IsStartOfNewString(StringNeedsToWrite))
{
*fileToWrite << '\n';
}
*fileToWrite << StringNeedsToWrite;
}
else
{
throw exception();
}
}
I want to write string into file, and set ofstream into my method.
But i have a strange error and don't know how to fix it (on image)
They differ in their make files and project files. A common problem is that moving a project from one environment to another entails big mistakes.
For example, MinGW under Linux does not have standard streams, but there is windows.h.
Such problems should be taken into account and studied at the stage of developing a program architecture and choosing technologies for development.
Frequent practice has shown that Qt is best used with your IDE and, if possible, use its classes for development.

My C++ program is blocked and deleted by Windows Defender

I've written a small C++ program which checks if Windows clipboard content has changed and prints a type of that content. I compiled the program to .exe file using Windows Visual Studio 2019 and it was blocked by the Windows Defender (file was removed). Why is that happened and how to prevent it?
Of course, if I open the Windows Defender and mark my file as "not a virus" then all works fine, but how to prevent blocking on customers computers? Do I need to create some "manifest" file..?
Sorry if the question is dumb, I'm new in C++ world
#include <iostream>
#include <io.h>
#include <fcntl.h>
#include <Windows.h>
#include <conio.h>
int main()
{
DWORD m_lastClipboardSequenceNumber = GetClipboardSequenceNumber();
while (1) {
Sleep(100);
const DWORD newClipboardSequenceNumber = GetClipboardSequenceNumber();
if (newClipboardSequenceNumber == m_lastClipboardSequenceNumber)
continue;
if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
std::wcout << "CF_UNICODETEXT\n";
}
if (IsClipboardFormatAvailable(CF_HDROP)) {
std::wcout << "CF_HDROP\n";
}
if (IsClipboardFormatAvailable(CF_BITMAP)) {
std::wcout << "CF_BITMAP\n";
}
m_lastClipboardSequenceNumber = newClipboardSequenceNumber;
}
return 0;
}
Sounds like your issue isn't with C++ at all and more just with Windows, more precisely, Windows Defender. The issue here, to my knowledge, is that Windows Defender started by default not allowing .exe files from unknown sources to be run on the computer without Admin privileges. This is an issue you cannot fix remotely, otherwise that would massively undermine the existing usefulness of Windows Defender, as malicious actors could just use that to run their exploits.
Steps you could take to possibly fix this for your use case: if you have access to the computers you want to run this on, try adding your distribution method to trusted sources. Alternatively, try signing it with a key and adding that signature to trusted.
I personally think since your method for watching clipboard is too abusive, windows defender is blocking your code.
Try monitoring clipboard section and register listeners for clipboard changes to see if same thing happens or not. Your code will be much more complex, since you will need to create a window loop for receiving messages, but I think it will OK that way.

Converting a simple folder to shared folder in Qt C++

I just made a program in Qt that creates a folder in a specific diretory.
the code is:
QDir directory;
directory.mkdir("Sample");
my problem is how could i convert the folder to a shared folder using Qt codes?
Or is there a way to create a shared folder using Qt??
You can share a directory using NetShareAdd. As far as I know, Qt doesn't provide anything with the same basic capability as NetShareAdd.
Edit: here's a quite bit of demo code:
#include <windows.h>
#include <lm.h>
int main() {
SHARE_INFO_2 info = {0};
info.shi2_netname = L"test_share";
info.shi2_type = STYPE_DISKTREE;
info.shi2_permissions = ACCESS_ALL;
info.shi2_max_uses = -1;
info.shi2_path = L"C:\\a\\b\\c";
NetShareAdd(NULL, 2, (BYTE *)&info, NULL);
return 0;
}
Note that NetShareAdd (like most of the Net* functions) is only available in a "wide" version that uses wide character strings.
This seems like it would be operating system dependent; Qt's abstraction of the OS-native directory functions isn't likely to be concerned with such a thing.
You'll probably want to look into your OS' specific methods for changing the "shared" status of a directory. On Windows, this might involve using WMI.

How to create a text file in a folder on the desktop

I have a problem in my project. There is a project folder on my desktop. I want to create a text file and write something include this text file. That is my code:
ofstream example("/Users/sample/Desktop/save.txt");
But I want to it could been run the other mac. I don't know what I should write addres for save.txt.
Can anyone help me?
Create a file and write some text to it is simple, here is a sample code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
std::ofstream o("/Users/sample/Desktop/save.txt");
o << "Hello, World\n" << std::endl;
return 0;
}
I hope that answers your question but I am not sure if i understand your question correctly, If not please add the details correctly of what you are trying to acheive.
[Update]:
Okay I guess the comment clears the problem.
Your real question is, You want to save the file in the desktop of the user who is playing the game. So getting the path of the current user's desktop is the problem.
I am not sure if there is an portable way to get desktop path but it can be done in following ways:
In Windows:
Using the SHGetSpecialFolderPath() function.
Sample code:
char saveLocation[MAX_PATH] = {0};
SHGetSpecialFolderPath(NULL, saveLocation, CSIDL_DESKTOPDIRECTORY, FALSE);
//Now saveLocation contains the path to the desktop
//Append your file name to it
strcat(saveLocation,"\\save.txt");
ofstream o(saveLocation);
In Linux:
By using environment variables $HOME
sample code:
string path(getenv("HOME"));
path += "/Desktop/save.txt";
ofstream o(path);
Rules defining where-you-should-save-file vary from platform to platform. One option would be to have it part of your compile script (that is you #define SAVEGAME_PATH as part of your compilation configuration), and thus your code itself remain more platform-agnostic.
The alternative is to find a save-data-management library that is already designed to be ported across different platforms. Whether it'd be a C or C++ or whatever-binary-interoperable library then no longer matters.
Just don't expect that to be part of C++ (the language).
if you want your program to run across platform,you'd better use the
relative path.
eg. "./output.txt",or better “GetSystemDirectory()”to obtain the system
directory to create a file,and then you could write or read the file
with the same path..