I am new to coding in the C language. I am trying to make a program that detects when the RobloxPlayerBeta.exe is being run, but upon compiling it says that "passing argument 1 of 'strcmp' makes pointer from integer without a cast". Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int logout();
char retrieve();
int main(){
char value;
while(1){
system("cmd.exe /C tasklist > Tasks.txt");
value = retrieve();
printf("%d\n",value);
int thing;
thing = strcmp(value,"1");
printf("%d",thing);
if (thing == 0){
int x = logout();
}
sleep(10);
}
return 0;
}
int logout(){
system("c:\\windows\\system32\\shutdown /l");
return 0;
}
char retrieve(){
system("cmd.exe /C start C:\\Users\\chall\\Documents\\Ccode\\Logout\\dist\\FindTask\\FindTask.exe");
FILE *f;
f = fopen("Tasks.txt","r");
int number = fgetc(f);
return number;
}
FindTask.exe is an exe made with the following python code:
with open(r"C:\Users\chall\Documents\Ccode\Logout\Tasks.txt","r") as db:
dataset = db.readlines()
for data in dataset:
if(data[:20].strip().lower() == "robloxplayerbeta.exe"):
with open("Tasks.txt","w") as f:
f.write("1")
I would like to know what a cast is and why I need one.
Cast is to tell the system convert data of one type to another type.
Example:
#include <stdio.h>
int main(void) {
int a = 10;
double b = (double)a; /* cast is used here */
printf("%f\n", b);
return 0;
}
In this case you don't need cast. strcmp()
is for compareing strings. You should use operators to deal with numbers to compare single character.
Wrong:
thing = strcmp(value,"1");
Correct:
thing = value - '1';
thing = value - '1';
is an unclear subtraction/comparison.@chux-ReinstateMonica Do you prefer
thing = (value > '1' ? 1 : (value < '1' ? -1 : 0));
?A comment explaining the subtraction would help. Or as code later uses
if (thing == 0){
,thing = value != '1';
would be sufficient.I think what @chux is saying is that you should explain why you're subtracting
1
fromvalue
before assigning it tothing
and what that line of code does, especially as the OP seems to be rather new to the language.