EvilZone
Programming and Scripting => C - C++ => : Rafy January 04, 2012, 02:02:07 PM
-
So as you can see , what I want to do is compare a string named "in" to an array of strings named "arr". Here is the code I have so far.
#include <stdio.h>
#include <string.h>
int main()
{
char arr[3][128]={"chk","help","ext"};
char in[128];
int cm=1;
int i;
while(cm!=0)
{
printf(">Type a command and press return.\n");
printf(">> ");
scanf("%s",in);
for(i=0;i<3;i++)
{
cm=strmcp(in,arr[i]);
}
}
printf("%d\n", cm);
return 0;
}
The objective is to find the string inside the array,which in this case it's a command.If you know a better way to accomplish it instead of this one please mention it.
I know the compare part is totally wrong but that's what I came up with.I don't know if it's any help but I am doing everything in the Macintosh terminal using Nano and gcc.any help is appreciated.The program is text based , I dont know anything about GUI yet regarding C.
PS: almost forgot , I need another thing. How can I check using C if a website is online?I tried to find something on the net regarding ping.So far the only solution I have managed to come up with is to execute the native ping command of the mac/linux terminal inside my tool.
-
#include <stdio.h>
#include <string.h>
int main()
{
char arr[3][128]={"chk","help","ext"};
char in[128];
int i=0;
do
{
printf(">Type a command and press return.\n");
printf(">> ");
scanf("%s",in);
while( (i++ < 3) && (cm!=0 ) ) cm=strcmp(arr[1], in)!=0)
}
while(cm!=0);
printf("%d\n", cm);
return 0;
}
Also, scanf is really insecure. No input lenght control.
About checking a webpage, look for sockets. Something like (not finished, more than probably not working, on the fly):
int isOnline(char *web, int port)
{
int sock=socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
struct hostent *host;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr = *((struct in_addr *) host->h_addr);
if(connect(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr)) return 1;
else return 0;
}
-
Here you go, I think this is a good example :) :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUMOPT 3
#define MAX 6 // 4 + newline + null
#define NONE -1
const char * options[NUMOPT] = {"chk", "help", "exit"};
int get_option(void)
{
char input[MAX];
if (!fgets(input, MAX, stdin)){
fprintf(stderr, "EOF!\n");
exit(1);
};
int input_size = strlen(input);
if (input_size > 1){ // removing newline
if (input[input_size-1] == '\n')
input[input_size-1] = '\0';
else
while (getchar() != '\n') continue;
}
int i;
for (i = 0; i < NUMOPT; i++){
if (strncmp(options[i], input, input_size) == 0)
return i;
}
return NONE;
}
int main(void)
{
int selected = NONE; // selected option index
do {
printf(">Type a command and press return.\n");
printf(">> ");
} while ((selected=get_option()) == NONE);
printf("You've selected option %s\n", options[selected]);
return 0;
}
The new C standard should introduce new standard function gets_s which will replace fgets (and of course gets) for getting a line of input from the user without getting the trailing newline.
-
Thanks everybody.About scanf being insecure, that's what they taught us in university and that's what they want to see in the lab exam.Maybe , later they will introduce something new that works better.