LinuxQuestions.org
Help answer threads with 0 replies.
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 08-01-2010, 06:43 AM   #16
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335

Quote:
Originally Posted by Completely Clueless View Post
Sounds distinctly possible. C# takes care of boundary-checking and the pesky newline character automatically, I believe. Perhaps it's time I migrated!
Or you could attempt to write better code, that does not rely on hard-coded constants. Use the sizeof() compiler operator to get a fix on the size of arrays. Also drill into your mind that array indexing in C begins at 0, not 1.

For this declaration:
Code:
char usr[8];
the legal indexes for 'usr' range from 0 through 7.

This code:
Code:
fgets (usr, 9, users);  // grabs first line of user file, including newline char
usr[8]=',';             //overwrite the newline char with a comma
could be better written to be:
Code:
// grabs first line of user file, POSSIBLY including newline char
fgets(usr, sizeof(usr), users);

// check if the newline is present
char* nl = strchr(usr, '\n');
if (nl)
{
  //replace the newline with a terminating NULL char
  *nl = '\0';
}

// write 'usr' to the target file
fputs(usr, target);

// now write the comma to the target file
fputc(',', target);
You need to reference the man-pages before you blindly go using the C library functions. There's information that will help you understand the expected behavior when calling a function, such as fgets().

As mentioned earlier, you can use 'gdb' or the GUI front-end 'ddd' for debugging purposes. Compile your code with the -g option, and never (as suggested earlier) use the -O2 option until you are complete with debugging your task.

Last edited by dwhitney67; 08-01-2010 at 06:45 AM.
 
1 members found this post helpful.
Old 08-01-2010, 09:22 AM   #17
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 899

Original Poster
Rep: Reputation: 70
I take it all back. Just looked into the C# language and you can keep it. I'll stick with C any day.
 
Old 08-01-2010, 09:28 AM   #18
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 899

Original Poster
Rep: Reputation: 70
dwhitney67:

Quote:
the legal indexes for 'usr' range from 0 through 7.
D'oh!! I knew that! Shit! Why can't I function when it's 42C and there's no air-con??
 
Old 08-01-2010, 10:12 AM   #19
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 899

Original Poster
Rep: Reputation: 70
Unhappy

Quote:
Originally Posted by dwhitney67 View Post
You need to reference the man-pages before you blindly go using the C library functions.
Yeah, you are dead right, but I DO actually do this. I have one of these O'Reilly concise guides to the syntax, but the typography used to me at least is completely impenetrable. It's total gobbledegook which bears no resemblance to the code you actually see deployed in practice. It is so far removed from reality that it is far worse, in fact, than useless!
 
Old 08-01-2010, 10:22 AM   #20
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
How about man pages (using the man command)?
 
Old 08-01-2010, 11:09 AM   #21
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
If you want to remove "\n", you can use the strchr method instead of messing with subscripts. It returns a pointer to its exact location in the string (or NULL if there isn't any) so you'd simply dereference the pointer and replace \n with \0.

I would recommend "C Primer Plus" by Stephen Prata for an excellent introduction to C basics.
 
Old 08-01-2010, 04:22 PM   #22
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 899

Original Poster
Rep: Reputation: 70
Angry

Quote:
Originally Posted by MTK358 View Post
How about man pages (using the man command)?
OMG! They are the worst of the lot!
 
Old 08-01-2010, 07:11 PM   #23
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by Completely Clueless View Post
Quote:
Originally Posted by MTK358 View Post
How about man pages (using the man command)?
OMG! They are the worst of the lot!
Until you learn to learn from the man pages, your fate will be as bad as that of Lot's wife.
 
Old 08-01-2010, 09:06 PM   #24
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by wje_lq View Post
Until you learn to learn from the man pages, your fate will be as bad as that of Lot's wife.
+1e9
 
Old 08-01-2010, 09:06 PM   #25
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
I think the man pages are of limited use until you know more or less what you are after. I really wouldn't recommend learning any serious programming language that way.
 
Old 08-01-2010, 10:51 PM   #26
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jay73 View Post
I think the man pages are of limited use until you know more or less what you are after. I really wouldn't recommend learning any serious programming language that way.
Reading (or listening to a foreign yet unlearned language for that matter) is quite a useful experinece - when done stubbornly and persistently it helps creating in one's mind a "cross-reference table" which in the end is the fundamental prerequisite of getting "the whole picture".

As a number of participants in this forum have shown, they have problems in comprehension in the first place.

If a manpage looks unclear, a motivated reader should first of all has to concentrate and write down what is unclear, and then has to try to find answers to the written down issues/questions.

This is what I call mental work and discipline opposed to mental laziness and sloppiness.
 
Old 08-02-2010, 12:00 AM   #27
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by jay73 View Post
I think the man pages are of limited use until you know more or less what you are after. I really wouldn't recommend learning any serious programming language that way.
I don't think anyone here is recommending using that as the way to learn a programming language. I would recommend it as a way to learn a certain subset of the C subject matter: the behavior of library functions. Nothing more, nothing less. For this, the man pages are usually quite excellent.
 
Old 08-02-2010, 04:15 AM   #28
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 899

Original Poster
Rep: Reputation: 70
I will post a typical example of precisely what my beef is with these function summaries shortly...

Last edited by Completely Clueless; 08-02-2010 at 04:34 AM.
 
Old 08-02-2010, 04:54 AM   #29
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 899

Original Poster
Rep: Reputation: 70
OK, here's the kind of thing we run into all the time. For you people, it's no doubt crystal clear what the following means:-

"Block read and write functions.

size_t fwrite (const void *buf, size_t sz, size_t n, FILE *fp );

Writes n objects of length sz from the buffer addressed by buf to the file."

Not much use to me, though. OK, so the author of this book (it's the C Pocket Reference by O'Reilly) has explained what "n", "sz" and "buf" mean, but has neglected to define "size_t" and "const void". Furthermore the expression "FILE *fp" is confusing. It looks like two seperate tokens, the second of which must contain an asterisk. In practice, however, and taking from my earlier example, "FILE *fp" could be something as simple as just "target".

Also in these concise guides and cheat sheets, they typically neglect to include what the function returns when it succeeds and when it fails. fopen() and fclose() don't return the same value when they succeed and when they fail. This should form part of the function summary IMHO. Also there ought to be a note of any particular quirks you need to know about, for example like with fgets() when it leaves a '\n' behind in the buffer.

OR is it just me? Am I nuts?

Last edited by Completely Clueless; 08-02-2010 at 04:56 AM.
 
Old 08-02-2010, 04:57 AM   #30
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Completely Clueless View Post
OK, here's the kind of thing we run into all the time. For you people, it's no doubt crystal clear what the following means:-

"Block read and write functions.

size_t fwrite (const void *buf, size_t sz, size_t n, FILE *fp );

Writes n objects of length sz from the buffer addressed by buf to the file."
...
Not much use to me, though.
OR is it just me? Am I nuts?
So, just read again what the manpage says and answer the question I've asked here "countless" number of times: "what is the very first thing you do not understand (in the above manapage excerpt) ?".
 
  


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
Trouble runnin aMule nightrider Linux - Newbie 2 10-07-2007 08:20 AM
help with runnin pureftpd computerdude Linux - Networking 1 09-04-2005 04:15 AM
help with runnin pure-ftpd computerdude Linux - Software 0 09-01-2005 08:41 PM
Apache 2.0 woes ( runnin on SUSE 8.2) queendevious Linux - Newbie 2 04-01-2004 08:00 AM
runnin a counterstrike server in mandrake? weirdaliens Linux - Newbie 3 12-11-2003 08:08 PM

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

All times are GMT -5. The time now is 05:49 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