473,765 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Perl chomp() equivalent in C?

5 New Member
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 4748
sicarie
4,677 Recognized Expert Moderator Specialist
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
1547
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 eliminate alot of redundancy. What am i missing here?
5
2453
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. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/271246
1
10066
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. This program can perform 5 options from a menu on your POP3 mail by logging in with the correct POP3 server along with a user name and password that you use to log in to your ISP. The user name and password as well as the server name are all hard-coded...
35
19489
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
1789
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 reiterating properly. (I am using Perl 5.8.2 build 808 MSI version). Thanks Reyez #!!c:\perl\bin\perl.exe
0
1271
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 code: #!/usr/bin/perl #
4
4990
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"; while(1) { # Loop forever print "\nDo you want to search by employee ID (I), " . " or quit (Q): ";
4
2534
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 checked. Now what the script does is it checks the time stamp and creates a year folder if it does not exist and then creates a month folder if it does not exist and puts the respective files in the month folders. If the files are created this month then it...
6
1591
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 $number = shift || <STDIN>; printf "Binary Number ---- > %b\n", $number; With the code above. I know it works. I am wonder how everything works.
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.