小编Axl*_*Axl的帖子

如何使用execvp()

用户将读取一行,我将保留第一个单词作为execvp的命令.

让我们说他会输入"cat file.txt" ...命令将是cat.但我不知道如何使用它execvp(),我读了一些教程,但仍然没有得到它.

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

int main()
{
    char *buf;
    char command[32];
    char name[32];
    char *pointer;
    char line[80];
    printf(">");

    while((buf = readline(""))!=NULL){   
        if (strcmp(buf,"exit")==0)
        break;

        if(buf[0]!=NULL)
        add_history(buf);

        pointer = strtok(buf, " ");
        if(pointer != NULL){
            strcpy(command, pointer);
        }

        pid_t pid;

        int  status;

        if ((pid = fork()) < 0) {     
            printf("*** ERROR: forking child process failed\n");
            exit(1);
        }
        else if (pid == 0) {          
            if (execvp(command, buf) < 0) {     
                printf("*** ERROR: exec failed\n");
                exit(1); …
Run Code Online (Sandbox Code Playgroud)

c shell fork execvp

13
推荐指数
1
解决办法
9万
查看次数

IndexOutOfBound从最后打印数组

我试图读取从最后一个元素到第一个元素的数组,并在某些情况下更改它们.但我不知道为什么我得到索引超出范围的例外.

public class Test
{
    private Scanner scan = new Scanner ( System.in );

    private int dimension;

    private int[] a;

    public int getDimension() {
        return dimension;
    }

    public void setDimension(int dimension) {
        this.dimension = dimension;
    }

    public Test(int dimension) {
        System.out.println("Add elements");
        setDimension(dimension);
        this.a = new int[this.dimension];
        for(int i = 0; i < this.dimension; i++)
            a[i] = scan.nextInt();
    }

    public void calc() {
        int aux = 0;
        for(int i = a.length-1; i >= 0; i--)
            if(a[i] > a[i-1]) {
                aux …
Run Code Online (Sandbox Code Playgroud)

java indexoutofboundsexception

-3
推荐指数
1
解决办法
55
查看次数

标签 统计

c ×1

execvp ×1

fork ×1

indexoutofboundsexception ×1

java ×1

shell ×1