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 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.
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>
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.
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 */
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--
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.
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.
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.
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; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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,
|
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;
|
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?
|
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...
|
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?
| |
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
|
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");
|
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?
|
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: ";
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |