Home > Software engineering >  how to use execvp() and fclose()?
how to use execvp() and fclose()?

Time:04-06

I'm making a program that reads a text file with different numbers each line and put that numbers in a string. (could be in a array of integer but I need to use the function execvp so I guess i need do as a string). Then it uses a second program to calculate the square of that numbers.

The problem is: I can't pass the arguments to the functions execvp and function execvp is not working. (I guess is something with the fopen)

Edit: I tried with popen instead of execvp and didn't work as well

Here it goes part of my code:

#define LSIZ 128

#define RSIZ 10

char line[RSIZ][LSIZ];
FILE *fptr = NULL;
int i = 0;
int tot = 0;

fptr = fopen("casosproduto.txt", "r");
while(fgets(line[i], LSIZ, fptr)) 
{
    line[i][strlen(line[i]) - 1] = '\0';
    i  ;
}
fclose(fptr);

char* argument_list3[]={"produto",&line[i],NULL};
tot = i;    
for(i = 0; i < tot;   i)
{
    execvp("./produto",argument_list3);
    printf(" %s\n", line[i]);
}

CodePudding user response:

execvp will load produto to the original process space, and then execute it, which is equivalent to executing the for loop only once, you can first fork a subprocess and call execvp in the subprocess, Like this

for (i = 0; i < tot;   i) {
    char *argument_list3[] = {"produto", line[i], NULL};
    if (fork() == 0) {
        execvp("./produto", argument_list3);
    }
}

You can also call function system to execute, and you can read a line and execute it once, so it is not limited by the number of lines in the file, e.g:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#define LSIZ 128
int main()
{
    char line[LSIZ];
    char cmd[LSIZ   16];
    FILE *fptr = NULL;

    fptr = fopen("casosproduto.txt", "r");
    if (fptr == NULL)
        return -1;

    while (fgets(line, LSIZ, fptr)) {
        line[strlen(line) - 1] = '\0';
        snprintf(cmd, sizeof(cmd), "./produto %s", line);
        system(cmd);
    }

    fclose(fptr);
    return 0;
}

CodePudding user response:

Udpdate. I did some changes in my code but still dont work.

Here it goes the code updated:

#define LSIZ 128 
#define RSIZ 10 

 char line[RSIZ][LSIZ];
 FILE *fptr = NULL; 
 int i = 0;
 int tot = 0;


 fptr = fopen("casosproduto.txt", "rt");
 while(fgets(line[i], LSIZ, fptr)) 
 {
    line[i][strlen(line[i]) - 1] = '\0';  
     i  ;
 }
 fclose(fptr);


tot = i;
pid = fork();

for(i = 0; i < tot; i  )
{
char* argument_list3[]={"produto",line[i],NULL};
if (fork() == 0) {
    execvp("./produto", argument_list3);

}
}
 }
return 0;
}

There are two reasons for the execvp is not working... first- Something related with the fclose function. If i put the fclose function inside the loop (I know that doesn't make sense but just for tests) then it executes the execvp. Outside the loop it doesnt.

Second- Are my arguments to the execvp alright? (when I tested puting the fclose inside the loop i had to change to this "char* argument_list3[]={"produto","4",NULL};" to test with a number, otherwise the result would be always 1.

  • Related