English 中文(简体)
1. 修建一枚炮弹——IO麻烦
原标题:building a shell - IO trouble

我正在为一个系统方案拟订班进行工作。 我对档案的重新定位感到有些麻烦。 我只是把产出转向工作,即工作。 然而,当我把像“编目”;a”这样的指挥打到我身上时,它就删除了档案中的所有内容。 如果说“fdin = 开放(_inputFile,777)的话,我也感到问题。

如果情况与建议的教学/其他实例挂钩,将受到高度赞赏。

在一旁,我包括整个功能,然而,在它创建管道的时候,我还没有在那里测试任何东西。 我不相信它会很好地发挥作用,但这可能来自另一个档案中的错误。

void Command:: execute(){

    if(_numberOfSimpleCommands == 0){

    prompt();
        return;
    }

    //save input/output
    int defaultin = dup(0);
    int defaultout = dup(1);

    //initial input
    int fdin;
    if(_inputFile){
        fdin = open(_inputFile,0777);
    }else{
        //use default input
        fdin = dup(defaultin);
    }

    //execution
    int pid;
    int fdout;
    for(int i = 0; i < _numberOfSimpleCommands; i++){
        dup2(fdin,0);
        close(fdin);

        //setoutput
        if(i == _numberOfSimpleCommands -1){
            if(_outFile){
                fdout = creat(_outFile,0666);
            }else{
                fdout = dup(defaultout);
            }
        }else{
            int fdpipe[2];
            pipe(fdpipe);
            fdout = fdpipe[0];
            fdin = fdpipe[1];
        }
        dup2(fdout,1);
        close(fdout);

        //create child
        pid = fork();
        if(pid == 0){
            execvp(_simpleCommands[0]->_arguments[0],_simpleCommands[0]->_arguments);
            perror("-myshell");
            _exit(1);
        }
    }
    //restore IO defaults
    dup2(defaultin,0);
    dup2(defaultout,1);
    close(defaultin);
    close(defaultout);

    if(!_background){
        waitpid(pid,0,0);
    }
}

最佳回答

页: 1 《<>开放的第二个论点是,除其他外,应包含明确准入模式和立案旗的双向或混合价值(O_RDONLY,等)。 自您通过<代码>0777以来,这很可能最终含有以下两种内容:O_CREATO_TRUNC,造成_inputFile被删除。 也许希望<代码>开放(_inputFile, O_RDONLY)。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签