Using the sprintf function? C++

Jul 27, 2014 at 2:43pm
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
Jul 27, 2014 at 3:01pm
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)
Jul 27, 2014 at 3:32pm
^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...
Jul 27, 2014 at 3:47pm
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?
Jul 27, 2014 at 3:51pm
The -r is to specify the path to a folder.
Cin (user input who gives path to folder) > Clamscan scans that folder.
Jul 27, 2014 at 4:07pm
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.
Jul 27, 2014 at 4:13pm
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 Jul 27, 2014 at 4:14pm
Jul 27, 2014 at 4:17pm
^That seems awesome! :)
That I didn't thought of that myself! :D

I just set commandSof in the system(); then right?
Jul 27, 2014 at 4:46pm
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/
Jul 27, 2014 at 5:57pm
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.