English 中文(简体)
我怎么说要停止阅读?
原标题:How do I tell wc to stop reading?

I have a program that fork()s a new process and then overwrites that new process s stdin with my file descriptor pipe fd. In this process, I then use execvp() to execute wc and it should read its input from the parent. In the parent, I then write to the write end of the pipe and when done writing, close the pipe.

The problem is that wc is still expecting input and des not exit.
Usually, I can stop wc by typing CTRLD but I can t seem to send that signal in C/C++.

我如何告诉<代码>wc以停止阅读?

EDIT: I did follow the pipe/fork/dup/exec idiom (I think that s what it s called.)
The streaming works with other programs that need input, its just that wc needs the special EOF to stop reading.

int in_fd[2];
//parent-child setup
pipe(in_fd);
child = fork();
if(child == 0) {
    close(in_fd[1]);
    dup2(in_fd[0], 0);
    close(in_fd[0]);
    execvp(cmdArg[0], cmdArg);
} else {
    close(in_fd[0]);
    in_pid = fork_in_proc();
    close(in_fd[1]);
}

//writing function
pid_t fork_in_proc() {
    string line;
    pid_t in_pid;
    if((in_pid = fork()) == 0) {
        close(in_fd[0]);
        ifstream file(stream_file[STREAM_IN].c_str(), ios_base::in);
        if (file.bad()) {
            cerr << "File read error
";
            return 1;
        }

        while(file.good()) {
            getline(file, line);
            if(file.good()) {
                write(in_fd[1], line.c_str(), line.length());
            }
        }
        int end = 3;
        write(in_fd[1], &end, sizeof(end));
        file.close();
        close(in_fd[1]);
        cout << "PIPE IN" << endl;
        exit(0);
    } else {
        return in_pid;
    }
}

如果该法典似乎有些脱节,则会发生争执。 我不得不从档案中集中起来。

最佳回答

wc exits when you close the stdout.

问题回答

暂无回答




相关问题
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?

热门标签