By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

Perl chomp() equivalent in C?

P: 5
I have the following lines in a simple for() loop:

Expand|Select|Wrap|Line Numbers
  1. time_t rightnow;
  2. (void) time(&rightnow);
  3. fprintf(fp, "[%s] waited for pass %d to finish", asctime(localtime(&rightnow)), i)
However, when I pull up the file that it's writing to (run.log), or even printing to STDOUT, it appears that asctime() or localtime() is adding an extra newline character because the result is...

[Sun Sep 17 20:15:55 2006
] waited for pass 0 to finish

instead of the originally intended/desired output being a single line...

[Sun Sep 17 20:15:55 2006] waited for pass 0 to finish

I've read somewhere that the function I'm looking for isn't part of regular C programming in that you'd have to pretty much build your own C version of the chomp() function handy in Perl (simply for trimming the last newline character).

Keeping in mind that this is writing to a log file and not the console, is there a way I can trim this newline character off the end of my or am I missing something here?
Mar 9 '07 #1
Share this Question
Share on Google+
1 Reply


sicarie
Expert Mod 2.5K+
P: 4,677
I have the following lines in a simple for() loop:

Expand|Select|Wrap|Line Numbers
  1. time_t rightnow;
  2. (void) time(&rightnow);
  3. fprintf(fp, "[%s] waited for pass %d to finish", asctime(localtime(&rightnow)), i)
However, when I pull up the file that it's writing to (run.log), or even printing to STDOUT, it appears that asctime() or localtime() is adding an extra newline character because the result is...

[Sun Sep 17 20:15:55 2006
] waited for pass 0 to finish

instead of the originally intended/desired output being a single line...

[Sun Sep 17 20:15:55 2006] waited for pass 0 to finish

I've read somewhere that the function I'm looking for isn't part of regular C programming in that you'd have to pretty much build your own C version of the chomp() function handy in Perl (simply for trimming the last newline character).

Keeping in mind that this is writing to a log file and not the console, is there a way I can trim this newline character off the end of my or am I missing something here?
Yeah, as far as I know for C, there's not much pre-built to help (though all my teachers were more into "build it yourself and see how it works" than my Java teachers who had more of a "this is how you find out what's out there and use it" mentality. All that to say - I don't think so, but I don't know a lot of the libraries of C - I would imagine that someone has written one...

On your issue, if the timestamp is formatted like that - if it will always be the same length - you could just put it into a string and remove the '\n' (last position in the character array) yourself...
Mar 11 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.