Re: equivalent of chomp in perl



2006-11-03 <454B886C.E59D48F1@xxxxxxxxx>,
CBFalconer wrote:
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.

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.
.



Relevant Pages

  • Small Correction to K&R Exercise 1-22 Solution on CLC-Wiki
    ... lines after the last non-blank character that occurs before the n-th ... int t,len; ... This space will be replaced by a newline, ...
    (comp.lang.c)
  • Re: ignore a line
    ... int main ... Without a terminating newline in the printf string, ... You would set a flag before beginning the copy, ... clear the flag and skip writing that line to the destination file. ...
    (comp.lang.c)
  • Re: ignore a line
    ... int main ... Without a terminating newline in the printf string, ... You would set a flag before beginning the copy, ... clear the flag and skip writing that line to the destination file. ...
    (alt.comp.lang.learn.c-cpp)
  • modified "histogram" exercise from K&R2
    ... second word by writing equal number of stars, *, at the output. ... "newline in output". ... each extra space, tab and newline appears in the output ... int main ...
    (comp.lang.c)
  • Re: What is Pythons answer to Perl 6?
    ... >> Most of the suggested changes remove redundancy, such as removing ... str, slice, tuple, type ... In consequence: chr -> int ...
    (comp.lang.python)