473,324 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Perl chomp() equivalent in C?

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
1 4730
sicarie
4,677 Expert Mod 4TB
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

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

Similar topics

7
by: el_roachmeister | last post by:
Is there a good article that explains why php does not support =~ like perl for regex? I am confused by all the different ways one can do regex in php but it would seem supporting =~ would...
5
by: Premshree Pillai | last post by:
Hello, I recently wrote a Perl version of pyAlbum.py -- a Python script to create an image album from a given directory -- plAlbum.pl . It made me realize how easy-to-use Python is. ...
1
by: bobano | last post by:
Hi everyone, I am writing a POP3 Client program in Perl. You connect to a POP3 Server and have a running conversation with the mail server using commands from the RFC 1939 Post Office Protocol....
35
by: lnatz | last post by:
Hi, Is there an equivalent to the perl command chomp in C? And if there is no exact equivalent command, how would I go about removing the "\n" at the end of a stdin? Thank you, Natalie
2
by: Reyez | last post by:
Hi all. I am currently working on the below but having real difficulties. Could someone give me some advice or some tips. I keep getting syntax errors each time it is run and the loop is not...
0
by: shapper | last post by:
Hello, I have a Perl code which I am having problems to translate to VB.NET. Could someone tell me what would be the VB.NET equivalent or an online Perl VB.NET converter? This is the Perl...
4
by: jonniethecodeprince | last post by:
#This is a program from dummies.com $TheDB = 'edata.txt'; # Open the database file but quit if it doesn't exist open(INDB, $TheDB) or die "The database $TheDB could " . "not be found.\n"; ...
4
by: jonathan184 | last post by:
Hi I have a perl script, basically what it is suppose to do is check a folder with files. Now the files are checked using a timestamp with the command ls -l so the timestamp in this format is...
6
by: Iron Blood | last post by:
#!/usr/bin/perl -w use strict; print "This program will convert Decimal to Binary \n"; print "Please pick your Decimal Number \n"; # Get the number from the command line or use default. my...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.