Using the sprintf function? C++

Hello

I try to use the sprintf function but it doesn't work, could someone explain me what I am doing wrong? :)

1
2
3
4
5
6
7
#include <stdio.h>
//...
char sof[100], commandSof[100];
//...
cin >> sof;
sprintf(commandSof, "clamscan -r ", sof);
system(commandSof);


It gives me warnings (to many arguments) and errors. :)

Thanks for reading,
Niely
There's info here in on sprintf syntax.
http://www.cplusplus.com/reference/cstdio/sprintf/


If you're going to use cin, that's in <iostream> - (not sure if you have that include and it just didn't copied and pasted in the post)
^I've included it. :)
I already took a look at that page but don't understand it.

For some reasons a lot of resources make it hard to understand the code...
sprintf expects two parameters, with additional ones expected to match format specifiers used in the second parameter. The format specifiers follow the ones used for the printf function here http://www.cplusplus.com/reference/cstdio/printf/

Is the -r supposed to be interpreted as a format specifier?
The -r is to specify the path to a folder.
Cin (user input who gives path to folder) > Clamscan scans that folder.
OK, I think I understand what you're trying to do - it seems you're missing a format specifier in the second parameter for sof.
You could build a string with the C++ string instead of c-style strings. I'm just outputting the combined string instead of running it, but I think this is what you're trying to do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

int main()
{
    std::string sof, commandSof;
    std::string scantext (" clamscan - r");
    std::cin >> sof;
    commandSof = sof + scantext;
    std::cout << commandSof;

return 0;

myFolderToScan
myFolderToScan clamscan - r
Last edited on
^That seems awesome! :)
That I didn't thought of that myself! :D

I just set commandSof in the system(); then right?
Looks like system takes a cstyle string.

Using system generally isn't recommended.

Why system() is evil
http://www.cplusplus.com/forum/articles/11153/

thread advising using fstream instead of system
http://www.cplusplus.com/forum/beginner/109352/
Code doesn't work, I tried to edit it my own way as well but still no success.
How does fStream works then?

The program will be Linux only so System should be okay.

Topic archived. No new replies allowed.