equivalent of chomp in perl

L

lnatz

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
 
C

CBFalconer

lnatz said:
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;
}
 
K

Keith Thompson

lnatz said:
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.
 
K

Keith Thompson

CBFalconer said:
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.
 
J

james of tucson

lnatz said:
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 */
 
M

Michal Nazarewicz

james of tucson said:
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-
 
J

james of tucson

Michal said:
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.
 
J

Jordan Abel

2006-11-03 said:
Then don't touch 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.
 
J

james of tucson

Jordan said:
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.
 
J

Jordan Abel

2006-11-03 said:
Jordan said:
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;
 
C

CBFalconer

Keith said:
That's a useful function, but it's doesn't bear any particular
resemblance to Perl's chomp function.

Probably so, but I suspect it is the answer to the OPs real
question, which was not well defined.
 
J

james of tucson

Michal said:
Your code does touch it.

So it does. Sorry, I don't pay much attention when helping others with
their homework, since it's their grade not mine :)

Sincere apologies.
 
J

James Antill

Jordan said:
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.

Yes.

% cdecl explain 'const char *foo'
declare foo as pointer to const char
 
K

Keith Thompson

CBFalconer said:
Probably so, but I suspect it is the answer to the OPs real
question, which was not well defined.

If you happen to know what Perl's chomp function does (which is
admittedly off-topic), the question is perfectly well defined, though
it could have been worded a bit better. The OP wants to remove a
trailing '\n' from a string (for example, from a string representing a
line read by fgets(). There's no indication that he wanted to discard
a line of input.
 
J

Jordan Abel

2006-11-03 said:
Probably so, but I suspect it is the answer to the OPs real
question, which was not well defined.

Are you sure? chomp is used for a specific purpose, which is removing
the newline from a whole line read from input. fgets() also leaves the
newline on the input line. The function above is more useful for people
who think they want to fflush(stdin) [after, say, a scanf] - but the OP
didn't say anything like that.
 
J

Jordan Abel

2006-11-03 said:
If you happen to know what Perl's chomp function does (which is
admittedly off-topic), the question is perfectly well defined, though
it could have been worded a bit better. The OP wants to remove a
trailing '\n' from a string (for example, from a string representing a
line read by fgets(). There's no indication that he wanted to discard
a line of input.

Anyway, in answer to the question

void
chomp(char *x) {
char *p = strrchr(x,'\n');
if(p) *p = 0;
}
 
W

Walter Roberson

[with respect to perl chomp]
Are you sure? chomp is used for a specific purpose, which is removing
the newline from a whole line read from input.

That would be chop, not chomp. chomp removes the newline only
if the newline is there. This is useful if you cannot guarantee
that the newline will be there (such as at end of file, or when
dealing with serial I/O or with network programming, as line
or message boundaries are not -certain- to be preserved for those).

It is also useful for robust modular programming where you don't want or
need the newline but you also don't want to force callers of the
routine to have to have removed it as a precondition to calling
your routine.
 
W

Walter Roberson

[/QUOTE]
Anyway, in answer to the question
void
chomp(char *x) {
char *p = strrchr(x,'\n');
if(p) *p = 0;
}

perl chomp removes the \n (actually, the current input record seperator)
only at the end of the string (unless you are in paragraph mode),
leaving the string alone if it does not end in \n.

Your chomp routine truncates the string at the last newline in it,
possibly rendering inaccessible characters after that point.

For example, chomp "hello\nthere" in perl would leave the string
alone; in your routine, it would truncate the string to "hello" .
 
J

James McGill

Jordan said:
Are you sure? chomp is used for a specific purpose, which is removing
the newline from a whole line read from input.

That is one common use of chomp(), but it is more accurate to say that
chomp removes zero or one trailing INPUT_RECORD_SEPARATORs from each
element of its argument list, or from each of the the values of a hash
argument.

It can be used for a great many things aside from merely removing a
newline from a string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top