Jump to content

Recommended Posts

Posted (edited)

Boa tarde a todos,

Estou a fazer um projecto em C (ou a tentar) em que deveria ser algo simples. Consiste em ler um string, neste caso uns comandos em linux separar os parâmetros e dizer o que se refere.

Por exemplo:

Input: ls –al | wc –l

Output: comando = ls; Parametro = -al; comando=wc; Parametro=-l

Na minha ideia era fazer uma funcção que percorresse a String e quando chegasse ao primeiro espaço imprimia o comando, e assim sucessivamente. Mas não sei muito sobre manipulação de strings, gostava de alguma ajuda.

o código que tenho é o seguinte:

#include
#include
#include


//#include "parse.c"

void parse_line(char *lin);

int main(int argc, const char * argv[])
{
char *line;
while(1){
line = readline("msh$");
if (line == NULL){
perror("malloc error!\n");
exit(1);
}
if (!strcmp(line, "exit")) exit(0);
add_history(line);
//printf("%s\n", line);
parse_line(line);
free(line);
}
}

void parse_line(char *lin){
int i = 0;
char lin2[10];
for (i = 0; i < strlen(lin); i++){
printf ("Indice - %d\n Caracter - %c\n", i, lin[i]);
while (lin[i] != ' '){
strcpy(lin2[i] = lin[i]);
}
}
printf ("Comando = %s", lin2);
}

Agradeço qualquer ajuda que possam dar.

Obrigado

Edited by Dioguex
Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.