Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: how to read 10 lines from a 200 lines file and write to a new file??

Thread Tools

Re: how to read 10 lines from a 200 lines file and write to a new file??

Joe Wright
Posts: n/a
Serve Laurijssen wrote:
> "Rick Ng Chi Wah" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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 (E-Mail Removed)
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
200/f2 vs. 70-200/f2.8 Cynicor Digital Photography 74 02-20-2008 03:23 PM
ow can i use fgets to read and ignore the first two lines of a file and output into another file Justme C Programming 9 10-01-2006 02:26 PM
Tamron 18-200 vs Sigma 18-125 & 18-200 Bill Tuthill Digital Photography 11 09-01-2005 09:55 PM
Light tele for D70: Sigma 55-200 or Nikon 28-200? Albert Voss Digital Photography 7 04-11-2004 12:24 AM