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 08-02-2010, 03:24 PM   #1
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 899

Rep: Reputation: 70
Best way to flush a character array


Hi guys,

This is for the C language, BTW.

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.
 
Old 08-02-2010, 03:46 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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';
}
The man-page for strchr().


P.S. Another alternative is to iterate through the array yourself.
Code:
char* p = array;
do
{
   if (*p == '\n')
   {
      *p = '\0';
      break;
   }
} while (*(++p) != '\0');

Last edited by dwhitney67; 08-02-2010 at 03:49 PM.
 
Old 08-02-2010, 03:54 PM   #3
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 899

Original Poster
Rep: Reputation: 70
Sorry, at one point there was such a flurry of postings on that thread in quick succession that I couldn't keep up with it all.
 
Old 08-02-2010, 07:40 PM   #4
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
Sorry, at one point there was such a flurry of postings on that thread in quick succession that I couldn't keep up with it all.
Um, people took the time out to write that flurry, and it was done for your benefit.
 
Old 08-03-2010, 03:27 AM   #5
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 899

Original Poster
Rep: Reputation: 70
Quote:
Originally Posted by wje_lq View Post
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.
 
Old 08-03-2010, 05:05 AM   #6
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
Hi guys,

This is for the C language, BTW.

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.
 
  


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
99% cpu usage due to flush-8:0 & flush-8:16 propofol Debian 4 09-09-2011 03:03 PM
How to define hex character array in C? MrUmunhum Programming 9 09-08-2008 07:16 PM
SIGSEGV: Array of character pointers?? usercsr Programming 2 04-23-2005 11:34 AM
C can 't return a character array Linh Programming 5 06-18-2003 04:58 AM
Cannot pass and return a character array Linh Programming 1 06-12-2003 04:17 PM

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

All times are GMT -5. The time now is 09:11 AM.

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