Re: equivalent of chomp in perl



2006-11-03 <ln1wokmhmm.fsf@xxxxxxxxxxxxxxx>,
Keith Thompson wrote:
CBFalconer <cbfalconer@xxxxxxxxx> writes:
Keith Thompson wrote:
CBFalconer <cbfalconer@xxxxxxxxx> writes:
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.

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.

Anyway, in answer to the question

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



Relevant Pages

  • Re: equivalent of chomp in perl
    ... removing the "\n" at the end of a stdin? ... int flushln{ ... If you happen to know what Perl's chomp function does (which is ...
    (comp.lang.c)
  • Capture stdin, stdout, and stderr from slave in pseudo-terminal
    ... I plan to modify the unix script program to capture stdin, stdout, ... stdout,and stderr file descriptors of the child process are being ... int ptyopen; ...
    (comp.unix.programmer)
  • Re: Keyboard Buffer Check
    ... modifying the stdin with tcsetattr and triggering select. ... > int getch; ... > * use this to return a char from the current input buffer, ...
    (comp.os.linux.misc)
  • Re: program waits before the select system call
    ... A> time from the stdin. ... A> character of whatever i type on stdin and then it just hangs before ... A> int tstGetInput; ...
    (comp.unix.aix)
  • Help with Select
    ... stdin and a connection. ... stdin and listening socket. ... int main ... int establish_connection (const char * name, ...
    (comp.os.linux.networking)