I am calling these functions:
unsigned blah[5];
lseek(0, 100, SEEK_CUR);
read(0, blah, sizeof(blah));
and
FILE *fr;
fr = fopen(
I'm not a 100% sure about the difference, but it seems to be related to pipe vs stdio/file. To illustrate this, I made these 2 different test programs:
exam.c
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
char blah[5] = {0,};
/* we're using stdin instead of pipeline :( */
lseek(0, 100, SEEK_CUR);
read(0, blah, sizeof(blah));
printf("%c%c%c%c%c\n", blah[0], blah[1], blah[2], blah[3], blah[4]);
return 0;
}
test.c
#include
#include
#include
#include
#include
void testA(const char *s)
{
int f = -1;
char blah[5] = {0,};
f = open(s, O_RDONLY);
if (-1 < f)
{
lseek(f, 100, SEEK_CUR);
read(f, blah, sizeof(blah));
}
printf("%c%c%c%c%c\n", blah[0], blah[1], blah[2], blah[3], blah[4]);
}
void testB(const char *s)
{
FILE *fp = NULL;
char blah[5] = {0,};
fp = fopen(s, "r");
if (fp)
{
fseek(fp, 100, SEEK_CUR);
fread(blah, 1, sizeof(blah), fp);
}
printf("%c%c%c%c%c\n", blah[0], blah[1], blah[2], blah[3], blah[4]);
}
int main(int argc, char **argv)
{
testA(argv[1]);
testB(argv[1]);
return 0;
}
Then I created some test data.
data.txt
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
When I executed cat ./data.txt | ./exam
, the output is
01234
When I executed ./test ./data.txt
, I got
bcdef
bcdef
Just FYI, the results remain unchanged even if we replace SEEK_CUR
with SEEK_SET
.
However, ./exam <./data.txt
results in
bcdef
which is legitimate.
I know this is not an acceptable answer without knowing why '0' brings up the data file's contents, but I hope it helps somehow.