Alisamix
Alisamix

Reputation: 21

Redirecting a file in a binary in unix

I have a binary in an Unix system and I want it to read all of my input from a file.

int main(int argc, char * argv[]) {
   foo(atoi(argv[1])
    exit(0);
}


int foo(int a) {
[..]
read(STDIN_FILENO, chararray, 5);
[..]
read(STDIN_FILENO, another_chararray, 10);
}

I already found out that read() continues to read at the place where it stopped to read before.

My questions would be:

How can I create a file.txt, so that "./binary < file.txt" gives foo() a parameter a, and writes input in both read() calls?

What happens if there is a nullbyte in the first read after the second character, will the next read continue to read after the nullbyte?

Upvotes: 1

Views: 286

Answers (1)

Adrien
Adrien

Reputation: 273

It should do so as you are reading your file as a binary (hence "nullbytes" don't exist) and not as text.

Upvotes: 1

Related Questions