TermoZour TermoZour - 1 month ago 9
C++ Question

How to use file strings as commands in c++

I am trying to make a program on Windows 10 using Visual Studio 2015 that would sim-link certain files to certain locations. I am trying to make a text file with the location of the files, and the sim-link destination to use.

This is an example of the file data that would be in the

properties.txt
file:


FileLocation: "Z:\Folder\file.txt"

FileMkdirLocation: "Z:\Folder2\file.txt"


I want to use something like
system(mkdir "sim-link_file_location" "file_location")
by changing the data that is in
properties.txt
. I want to be able to add more than 1 file, without recompiling the program and writing each command for each file, one by one.

The problem is that I don't know how to make the commands use the data in the file.

EDIT: I managed to find out a way, but I get errors when compiling the program. I use this code:

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>

using namespace std;

//initialization of Properties File used
ifstream PropertiesFile ("PropertiesFile.txt");

int main()
{
//initialization of variables used
int input_option;
char FileLocation[256], Command[]="mklink ";
// string FileLocation, Command;

PropertiesFile >> FileLocation;

/* switch (input_option)
{
case "add all mods":
}
*/

cout << "FileLocation: " << FileLocation;
cout << endl;

strcat(Command, FileLocation);
Command[strlen(FileLocation)] = '\0';

cout << Command;
cout << endl;

//system(command);

system("pause");
return 0;
}


I know that i haven't used all variables yet.
It tells me that "strcat" is deprecated and to use "strcat_s" instead, and when i replace it with that, I get


"Debug Assertion Failed - Expression: (L"Buffer is too small" && 0)"

Answer

I had to make the "Command" char bigger than "FileLocation" because then strcat_s would not be able to copy the content. After that the program worked fine, and there were no other Assert Errors.