C/Visual C - Problema con función exec

 
Vista:

Problema con función exec

Publicado por David (1 intervención) el 26/03/2013 16:24:06
Muy buenas a todos:

Estaba haciendo un programa de procesos en Linux que utiliza las funciones "exec", pero me veo en un problema y es que al ejecutar me salen los mensajes de error:

/home/alumno/lee.c: line 5: syntax error near unexpected token `('
/home/alumno/lee.c: line 5: `int main(int const argc, char *const argv[]){'

El primer programa deberá de ejecutar el segundo programa que lee un fichero de texto que se le pasará desde el primer programa. El comando que se le pasará al primer programa es: "lee <fich1>".
Tampoco se si la función exec que yo utilizo sería la más apropiada o eso no importa.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
 
void parse(char *buf, char **args);
 
void ejecutar(char **args);
 
main(){
	char buf[1024];
	char *args[64];
 
	while (1){
 
		printf("Command:");
 
		if (gets(buf) == NULL){
			printf("\n");
			exit(0);
		}
		parse(buf, args);
		ejecutar(args);
 
	}
}
 
void parse(char *buf, char **args){
	while (*buf != NULL) {
	while ((*buf == ' ') || (*buf == '\t'))
		*buf++ = NULL;
		*args++ = buf;
	      while ((*buf != NULL) && (*buf != ' ') && (*buf != '\t'))
		    buf++;
	}
	*args = NULL;
}
 
 
 
void ejecutar(char **args){
	int proceso, estado;
 
	if((proceso = fork()) < 0){
		perror("Fork fallido");
		exit(1);
	}
 
	if (proceso == 0){
		char pega[500];
		strcpy(pega, "/home/alumno/");
		strcat(pega, *args);
 
		execvp(pega, args);
 
		perror(*args);
		exit(1);
 
	}
 
	while (wait(&estado) != proceso);
 
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdlib.h>
#include <stdio.h>
 
 
int main(int argc, char **argv){
	FILE *f;
	f = fopen(*argv, "r");
 
	if(f == NULL){
		perror("Error en la apertura del fichero");
		exit(1);
	}
 
	char c;
 
	c = fgetc(f);
	while (c != EOF){
		printf("%c", c);
		c = fgetc(f);
	}
	printf("\n");
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder