Re: how to read 10 lines from a 200 lines file and write to a new file??
Serve Laurijssen wrote:
> "Rick Ng Chi Wah" <firstname.lastname@example.org> wrote in message
> news:email@example.com om...
> > Dear all,
> > How can I read first 10 lines from a 200 lines file and write to a new
> > Is it possible under C or C++ language?
> #include <stdio.h>
> FILE *fout = fopen("otherfile", "w");
> FILE *fin = fopen("inputfile", "w");
> if (fin && fout)
> int i;
> char line, *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
Joe Wright mailto:firstname.lastname@example.org
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
|All times are GMT. The time now is 06:31 AM.|
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.