LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-19-2003, 09:53 PM   #16
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Rep: Reputation: 30

I'm still working through "The C programming launguage" by K&R, but also have "Advanced Linux Programming" sitting on my hard drive, I can always use more.

Tarts
 
Old 12-20-2003, 12:58 PM   #17
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
You should have a look at anything written by W Richard Stevens. For starters teh above mentioned, and then the classic Unix Network Programming Vol I after that.
 
Old 12-20-2003, 03:01 PM   #18
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
is the stevens book available someplace online for viewing/downloading?
 
Old 12-20-2003, 05:50 PM   #19
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
unfortunately no, but i promise you that you'll want to own these books
 
Old 12-21-2003, 10:27 AM   #20
codedv
Member
 
Registered: Nov 2003
Location: Slough, UK
Distribution: Debian
Posts: 146

Rep: Reputation: 15
Re: fgets

Quote:
Originally posted by h/w
when fgets reads a line, it reads in the '\n' too.
is there a way to use fgets so that it stops reading when it encounters the newline?
thanks.
gets() is bad as theres no way of setting a limit. I have made my own version to solve that problem:
Code:
#include <stdio.h> /* we need this for getchar() and EOF */

int getsn (char * buf, int max)
{
  int count, c;


  for (count = 0; count < max - 1; count++) /* don't forget to leave one for \0 :D */
  {
    if ((c = getchar ()) == '\n' || c == EOF)
      break;
    else
      *(buf + count) = c;
  }

   *(buf + count) = '\0'; /* append the newloine character */

   return (c == EOF)?EOF:0;
}
It looks simple but it works for me.
 
Old 12-21-2003, 03:18 PM   #21
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
he said fgets(), not gets.
 
Old 12-21-2003, 05:38 PM   #22
codedv
Member
 
Registered: Nov 2003
Location: Slough, UK
Distribution: Debian
Posts: 146

Rep: Reputation: 15
I know what he asked. But fgets reads a new line - always has done which is why I suggested that function which I often use. It can be easily modified to to work with any file by including an extra file stream argument:
Code:
#include <stdio.h> /* we need this for getc() and EOF */

int getsn (char * buf, FILE * pstream, int max)
{
  int count, c;


  for (count = 0; count < max - 1; count++) /* don't forget to leave one for \0  */
  {
    if ((c = getc (pstream)) == '\n' || c == EOF)
      break;
    else
      *(buf + count) = c;
  }

   *(buf + count) = '\0'; /* append the newloine character */

   return (c == EOF)?EOF:0;
}
 
Old 12-21-2003, 05:49 PM   #23
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
thanks guys.
codedev: thanks for the code snippets.

this thread can now be closed?
 
Old 12-21-2003, 06:20 PM   #24
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
ahh my apologies, gotcha now codedev.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php fgets really slow pasholy2001 Programming 0 04-22-2005 02:17 AM
reading in string with fgets bostonbanana Programming 4 12-12-2004 02:10 PM
Is fgets functions secure? mullog Programming 2 10-02-2004 01:22 PM
fgets() problems in C AMMullan Programming 4 03-12-2004 04:39 AM
fgets vs gets cxel91a Programming 2 12-01-2003 12:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration