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.
I believe I have unwanted '\n' characters left in a 9 element character array that are causing subsequent operations with it to fail. I see wildly differing views on the web on the proper way to flush 'em. It's clearly not as simple as it would appear at first sight. What's currently the best (or else "least deprecated") method?
Last edited by Completely Clueless; 08-02-2010 at 03:30 PM.
Here we go again... I posted code very similar to the following in another one of your threads.
Code:
char array[9];
...
// find the '\n' (newline), if any, within the array
//
char* nl = strchr(array, '\n');
if (nl != NULL)
{
// replace the newline character with a null character
//
*nl = '\0';
}
Um, people took the time out to write that flurry, and it was done for your benefit.
I look at all the replies eventually. It can often take a few days, however. But none get ignored. I have even hit the "thanks" button for people more than 3 months after their posts in extreme cases.
Finally, these threads are archived and searchable indefinitely. It's not just one person that gets informed by them, but countless other people with the same query in the future.
I believe I have unwanted '\n' characters left in a 9 element character array that are causing subsequent operations with it to fail. I see wildly differing views on the web on the proper way to flush 'em. It's clearly not as simple as it would appear at first sight. What's currently the best (or else "least deprecated") method?
If you are sure it's the last character before the '\0' then obviously '\n' position is at (strlen(s) - 2).
If you are not sure, 'man 3 rindex' is your friend.
...
'rindex' is a faster solution if '\n' is known to be in the second half of the array.
Last edited by Sergei Steshenko; 08-03-2010 at 05:08 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.