473,748 Members | 2,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

equivalent of chomp in perl

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

Nov 3 '06 #1
35 19483
"lnatz" <nm**********@g mail.comwrites:
Is there an equivalent to the perl command chomp in C?
No. (chomp removes a '\n' character from the end of a string.)
And if there is no exact equivalent command, how would I go about
removing the "\n" at the end of a stdin?
I presume that by "a stdin", you mean "a string obtained from stdin".

Search for the '\n' character and replace it with '\0'. Be prepared
to decide what to do if the string doesn't contain a '\n' character,
or if it contains one but not at the end.

Note that C strings are not like Perl strings. They don't
automatically re-size themselves. A C string isn't a data type; it's
a data *format*, a sequence of character terminated by '\0'. A string
is typically held in an array of char; changing a trailing '\n' to
'\0' will change the length of the string but not the size of the
array.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 3 '06 #2
lnatz wrote:
>
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?
int flushln(FILE *f) {
int ch;

while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;
}

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
Nov 3 '06 #3
CBFalconer <cb********@yah oo.comwrites:
lnatz wrote:
>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?

int flushln(FILE *f) {
int ch;

while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;
}
That's a useful function, but it's doesn't bear any particular
resemblance to Perl's chomp function.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 3 '06 #4
lnatz wrote:
Is there an equivalent to the perl command chomp in C?

Perl's chomp() is actually a fairly complex function, especially in a
list context.

Do you just want to remove a newline from a string?

void chomp(const char *s)
{
char *p;
while (NULL != s && NULL != (p = strrchr(s, '\n'))){
*p = '\0';
}
} /* chomp */
Nov 3 '06 #5
james of tucson <jmcgill@[go_ahead_and_sp am_me].arizona.eduwri tes:
lnatz wrote:
>Is there an equivalent to the perl command chomp in C?


Perl's chomp() is actually a fairly complex function, especially in a
list context.

Do you just want to remove a newline from a string?

void chomp(const char *s)
{
char *p;
while (NULL != s && NULL != (p = strrchr(s, '\n'))){
*p = '\0';
}
} /* chomp */
What if '\n' is in the middle of string? (Moreover, it's not nice
telling you won't modify the argument and then modifying it.)

#v+
#include <string.h>

int chomp(char *str) {
if (!str || !*str) return 0;
while (str[1]) ++str;
if (*str!='\n') return 0;
*str = 0;
return '\n';
}
#v-

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--
Nov 3 '06 #6
Michal Nazarewicz wrote:
What if '\n' is in the middle of string?
Then don't touch it!
Moreover, it's not nice
telling you won't modify the argument and then modifying it.)
const char * means that the pointer is fixed, not that it points to some
immutable type.
Nov 3 '06 #7
2006-11-03 <_h************ ********@newsfe 10.phx>,
james of tucson wrote:
Michal Nazarewicz wrote:
>What if '\n' is in the middle of string?

Then don't touch it!
>Moreover, it's not nice
telling you won't modify the argument and then modifying it.)

const char * means that the pointer is fixed, not that it points to some
immutable type.
No, that would be char * const. which hardly matters anyway in function
arguments.
Nov 3 '06 #8
Jordan Abel wrote:
2006-11-03 <_h************ ********@newsfe 10.phx>,
james of tucson wrote:
>Michal Nazarewicz wrote:
>>What if '\n' is in the middle of string?
Then don't touch it!
>>Moreover, it's not nice
telling you won't modify the argument and then modifying it.)
const char * means that the pointer is fixed, not that it points to some
immutable type.

No, that would be char * const. which hardly matters anyway in function
arguments.
Seriously, am I wrong about this? I know that "const char[]" states
that the array contents will not be altered (K&R2, p40), but that's not
the situation here. Here we are talking about declaring a pointer to a
char as const, not that it is a pointer to a const array of chars.
Nov 3 '06 #9
2006-11-03 <Gy************ ****@newsfe15.p hx>,
james of tucson wrote:
Jordan Abel wrote:
>2006-11-03 <_h************ ********@newsfe 10.phx>,
james of tucson wrote:
>>Michal Nazarewicz wrote:

What if '\n' is in the middle of string?
Then don't touch it!

Moreover, it's not nice
telling you won't modify the argument and then modifying it.)
const char * means that the pointer is fixed, not that it points to some
immutable type.

No, that would be char * const. which hardly matters anyway in function
arguments.

Seriously, am I wrong about this? I know that "const char[]" states
that the array contents will not be altered (K&R2, p40), but that's not
the situation here. Here we are talking about declaring a pointer to a
char as const, not that it is a pointer to a const array of chars.
If you want the _pointer_ rather than what is pointed _at_ to be
constant, "const" goes after the * token.

char * const p;
Nov 3 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
41343
by: Fernando Armenta | last post by:
How you cut off the ^M at the end of a line? rstrip is not working. string.rstrip(build_number) thanks,
1
2741
by: Edward WIJAYA | last post by:
Hi, I am new to Python, and I like to learn more about it. Since I am used to Perl before, I would like to know what is Python equivalent of Perl code below: $filename = $ARGV;
3
3402
by: Caj Zell | last post by:
Hello, I am looking a little bit at python and am curious of one thing I often use in perl. Suppose, I want to change the string "best_composer" to "Zappa" in a document called facts.txt, then in perl I would do perl -pi.bak -e "s/best_composer/Zappa/;" facts.txt Can I do that with python from the command line so easily?
0
2161
by: Aaron Powell | last post by:
I am writing a program for a study I am doing where I must contact several different organisations stored in a database I compiled, but I need to send each email one at a time so they don't know who else I sent to. I tested my sendmail and it works well, but I am having a problem with chomp in the case I am using it. When getting $email, if I do not say $email\n, nothing will be displayed to the command prompt. This leads me to believe...
2
5341
by: Matt Taylor | last post by:
I have this piece of code: $/ = ' '; $test = "abc "; chomp $test; print $test; which prints spaces at the end of the line. Shouldn't chomp strip the spaces off the end of my scalar $test?
1
4746
by: mrmattborja | last post by:
I have the following lines in a simple for() loop: time_t rightnow; (void) time(&rightnow); fprintf(fp, " 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... waited for pass 0 to finish
3
1976
by: adriaan | last post by:
I'm having this weird problem with chomp I want to get all the numbers out of a file that looks like this 1,2 4,3,2 and have the first number point to an array of the others in a hash I'm almost able to do this except for the fact that a '\n' get ad to the last number, I'm trying to get rid of it but somehow chomp just won't work code : open (DataBase,"voorwaardelijk.txt");
5
2675
by: Beany | last post by:
Hi, Probably a very simple question for the experienced Perl programmers.... Im new to the language of Perl and at the moment im studying the function Chomp! for some strange reason i cant figure this out? Chomp deletes \n off a string but i dont get it! Can someone please show me a simple example and an outcome? and explain to me the purpose of this and why it's used?
4
1882
by: ezechiel | last post by:
Hi, while testing the program (runs in DOS), I thought "if someone hits enter without typing a letter before, what happens?" I tested and the script ends.. Is this normal, or how can I avoid this? print "To which analytical division is the study belonging to?\n\n"; print "\ti: Immunology\n"; print "\tc: Chemistry\n"; print "\tq: Quit\n"; print "\n\tYour choice: ";
0
8831
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
9376
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...
1
9326
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
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
6796
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
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
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
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
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.