ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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!
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.
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 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.
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.
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.
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) ?".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.