All of lore.kernel.org
 help / color / mirror / Atom feed
From: Martin Zwickel <martin.zwickel@technotrend.de>
To: Lei Yang <leiyang@nec-labs.com>
Cc: Kernel Newbies Mailing List <kernelnewbies@nl.linux.org>,
	linux-kernel <linux-kernel@vger.kernel.org>,
	Lei Yang <leiyang@nec-labs.com>
Subject: Re: problem with fwrite
Date: Thu, 19 Aug 2004 16:12:47 +0200	[thread overview]
Message-ID: <20040819161247.4fa08389@phoebee> (raw)
In-Reply-To: <4124AE51.2060700@nec-labs.com>

[-- Attachment #1: Type: text/plain, Size: 2255 bytes --]

On Thu, 19 Aug 2004 09:42:41 -0400
Lei Yang <leiyang@nec-labs.com> bubbled:

> 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.

First: Wrong place!!!

> 
> 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);

Seek in_stream to 0 (rewind)! like:
       rewind(in_stream);

> 
>      int c, i;
>      for(i = 0;;i++)
>      {
>    	c = fgetc(in_stream);
>    	fprintf(stderr, "get char %c\n", c);

the fprintf of the read char should be after the EOF check!

> 	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/


-- 
MyExcuse:
Traffic jam on the Information Superhighway.

Martin Zwickel <martin.zwickel@technotrend.de>
Research & Development

TechnoTrend AG <http://www.technotrend.de>

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

      reply	other threads:[~2004-08-19 14:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-19 13:42 problem with fwrite Lei Yang
2004-08-19 14:12 ` Martin Zwickel [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040819161247.4fa08389@phoebee \
    --to=martin.zwickel@technotrend.de \
    --cc=kernelnewbies@nl.linux.org \
    --cc=leiyang@nec-labs.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.