lkml.org 
[lkml]   [2004]   [Aug]   [19]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
Subjectproblem with fwrite
Hi all,

This is not really a kernel issue, apologize if anyone thinks that this
is not the right place to post it. But I am writing a kernel module and
got stuck on fwrite, really hope someone could point out what stupid
mistake I've made. I wrote a very simple code to test the idea, what I
really want to do in fred() is to read from 'dest' and write to 'src'.
It seems that upon running , fgetc doesn't get anything from in_stream,
so the first char it gets is an EOF and it breaks. Just why fwrite
didn't write anything to in_stream?

If this is not the right way to do it, what is ?

Appreciate any comments, even harsh ones.

TIA
Lei

// in test.c

#include <stdio.h>

fred(char *dest, size_t *destlen, char *src, size_t size)
{
FILE *in_stream = tmpfile();
FILE *out_stream = tmpfile();
fwrite(src, 1, size, in_stream);

int c, i;
for(i = 0;;i++)
{
c = fgetc(in_stream);
fprintf(stderr, "get char %c\n", c);
if ( c == EOF) break;
fputc(c, out_stream);
}

*destlen = i;

fseek(out_stream, 0, SEEK_SET); //rewind
fread(dest, 1, *destlen, out_stream);
fclose(in_stream);
fclose(out_stream);
fprintf(stderr, "buf = `%s', size = %d\n", dest, *destlen);
}

int main(void)
{
static char source[] = "really hope this works ";

char *bp = malloc (2048);
size_t destlen;

fred(bp, &destlen, source, strlen(source));

fprintf(stderr, "buf = `%s', size = %d\n", bp, destlen);
return 0;
}
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 14:05    [W:0.125 / U:0.140 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site