system command not working in C++ -


I took the following program as root user in C ++

  int Main () {if (system ("sudo sh -c sync")! = 0) cout & lt; & Lt; "\ N sync fail"; If (system ("echo 3> / proc / sys / vm / drop_caches")! = 0) cout & lt; & Lt; "\ NClearning cache failed"; }  

However, I know that the cache is not clearing and I am getting the following output:

  Sync failed clearing cache failed  

I do not know where I am wrong Someone can help me with this.

Avoid such things better. You can just call (this is one of the few which can not be unsuccessful) You can call (as FILE * f = fopen ("/ proc / sys / vm / drop_caches", "r "); ...) then & amp; In the second case fclose ; You should always test against failure and use the perror in cases of failure.

Therefore,

  sync (); FILE * f = fopen ("/ proc / sys / vm / drop_caches", "r"); If (! F) {false ("FOPN drop sign"); Exhaust (EXIT_FAILURE); }; If (fprintf (f, "3 \ n") & lt; 0) {false ("fprintf drop_cache"); Exhaust (EXIT_FAILURE); }; If (fclose (f)) {false ("fclose drop_cache"); Exhaust (EXIT_FAILURE); };  

BTW, do not forget the #include -s, compile with all warnings & amp; Debug info ( GCC -Wall -Wextra -g ), and Use debugger ( gdb ).

Read, and learn how to use the documents of each function you are using.


Comments

Popular posts from this blog

HTML/CSS - Automatically set height width from background image? -

php - Mysql Show Process - Sleep Commands and what to do -

c - What is the address of buf (the local variable in the main function)? -