Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: how to read 10 lines from a 200 lines file and write to a new file?? (http://www.velocityreviews.com/forums/t314424-re-how-to-read-10-lines-from-a-200-lines-file-and-write-to-a-new-file.html)

Joe Wright 07-27-2003 08:50 PM

Re: how to read 10 lines from a 200 lines file and write to a new file??
 
Serve Laurijssen wrote:
>
> "Rick Ng Chi Wah" <ngchiwah9@netscape.net> wrote in message
> news:57d06692.0307270834.41bcf575@posting.google.c om...
> > Dear all,
> >
> > How can I read first 10 lines from a 200 lines file and write to a new

> file?
> > Is it possible under C or C++ language?

>
> untested:
>
> #include <stdio.h>
>
> FILE *fout = fopen("otherfile", "w");
> FILE *fin = fopen("inputfile", "w");
>
> if (fin && fout)
> {
> int i;
> char line[1024], *p = line;
> for (i=0; p && i < 10; i++)
> {
> if (p = fgets(line, sizeof line - 1, fin))
> fputs(line, fout);
> }
> }
> if (fin) fclose(fin);
> if (fout) fclose(fout);


I'm sorry to be the one to tell you but you shouldn't post this kind of
'stuff' here. First, you open the input file in "w" mode, removing it.
Then you pretend to read it. The second argument to fgets, 'sizeof line
- 1' is wrong. Figure it out.

Most of us here, even the guru's have been bitten by this kind of thing
more than once. The first rule is 'Be Quiet' and let the next guy screw
up the response. The next rule is 'Compile it and Test it before you
Post it.'

--
Joe Wright mailto:joewwright@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---


All times are GMT. The time now is 07:48 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.