从输入中删除换行符 [英] stripping newline from input
问题描述
大家好
为什么这看起来不行?
void stripnl(char * sz)
{
char * nl;
nl = strchr(sz,''\ n'');
if( nl)
{
* nl =''\ 0'';
}
}
调用此函数后输入的字符串仍然有换行符。
谢谢
John
Hi all
Why doesn''t this seem to work?
void stripnl(char *sz)
{
char *nl;
nl = strchr(sz, ''\n'');
if (nl)
{
*nl = ''\0'';
}
}
The string fed in still has the newline after calling this function.
Thanks
John
推荐答案
John Smith< so ***** @ microsoft.com>潦草地写道:
John Smith <so*****@microsoft.com> scribbled the following:
大家好,所有
为什么这似乎不起作用?
void stripnl(char * sz)
{
char * nl;
nl = strchr(sz,''\ n'');
if(nl )
{
* nl =''\ 0'';
}
}
调用此函数后,输入的字符串仍然具有换行符。
Hi all Why doesn''t this seem to work? void stripnl(char *sz)
{
char *nl;
nl = strchr(sz, ''\n'');
if (nl)
{
*nl = ''\0'';
}
} The string fed in still has the newline after calling this function.
它看起来应该有效。你怎么称呼这个功能?怎么用
你查看结果?
-
/ - Joona Palaste(pa ***** @ cc.helsinki.fi)-------------芬兰-------- \
\-- http://www.helsinki.fi/~palaste ------------- --------规则! -------- /
这是,呃,非常书卷。
- Horace Boothroyd
It looks like it should work. How are you calling this function? How are
you checking the results?
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It was, er, quite bookish."
- Horace Boothroyd
" John Smith" <所以***** @ microsoft.com>在消息中写道
news:ch ********** @ newstree.wise.edt.ericsson.se ...
"John Smith" <so*****@microsoft.com> wrote in message
news:ch**********@newstree.wise.edt.ericsson.se...
大家好
为什么这似乎不起作用?
void stripnl(char * sz)
{
char * nl;
nl = strchr (sz,''\ n'');
if(nl)
{
* nl =''\ 0'';
}
}
在调用此函数后,输入的字符串仍然具有换行符。
你怎么知道的?可能有''\ r''(CR-如果文件取自
windows),否则功能正常。
谢谢
John
Hi all
Why doesn''t this seem to work?
void stripnl(char *sz)
{
char *nl;
nl = strchr(sz, ''\n'');
if (nl)
{
*nl = ''\0'';
}
}
The string fed in still has the newline after calling this function.
How do you know that ? There might be a ''\r'' (CR- if file is taken from
windows) otherwise the function is fine.
Thanks
John
" Joona I Palaste" < PA ***** @ cc.helsinki.fi>在消息中写道
news:ch ********** @ oravannahka.helsinki.fi ...
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:ch**********@oravannahka.helsinki.fi...
John Smith< so **** *@microsoft.com>潦草地写道:
John Smith <so*****@microsoft.com> scribbled the following:
大家好
为什么这似乎不起作用?
Why doesn''t this seem to work?
void stripnl(char * sz)
{
char * nl;
nl = strchr(sz,''\ n'');
if (nl)
{
* nl =''\ 0'';
}
}
void stripnl(char *sz)
{
char *nl;
nl = strchr(sz, ''\n'');
if (nl)
{
*nl = ''\0'';
}
}
在调用此函数后,字符串输入仍然具有换行符。
The string fed in still has the newline after calling this function.
它看起来应该有效。你怎么称呼这个功能?你如何检查结果?
-
/ - Joona Palaste(pa*****@cc.helsinki.fi)------ -------芬兰-------- \
\-- http://www.helsinki.fi/~palaste ---------------------规则! -------- /
这是,呃,相当书呆子。
- Horace Boothroyd
It looks like it should work. How are you calling this function? How are
you checking the results?
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It was, er, quite bookish."
- Horace Boothroyd
嗨Joona
char szLine [MAX_LINE]; / *这个够大.. * /
char * szToken;
while(fgets(szLine,MAX_LINE,* file)!= NULL)
{
szToken = strtok(szLine," \t:"); / *按空格,制表符或冒号分隔
个字符* /
stripnl(szToken);
}
如果我读到的行包含one_word \ n然后条带新线
似乎没有做到这一点。当然,如果该行包含two_words
second_word \ n,则令牌没有换行的换行符。
I 我相信这些东西上周有用了,我没碰过它,但是现在它好了。
不开心。
我只需要检查一下条件很好的理智。
谢谢
J
Hi Joona
char szLine[MAX_LINE]; /* this is big enough.. */
char *szToken;
while (fgets(szLine, MAX_LINE, *file) != NULL)
{
szToken = strtok(szLine, " \t:"); /* split by space, tab or colon
characters */
stripnl(szToken);
}
If the line I read in contained say "one_word\n" then the strip newline
doesn''t seem to do its thing. Of course if the line contained "two_words
second_word\n", then the token doesn''t have a newline to strip out.
I''m sure this stuff worked last week, and I haven''t touched it, but now it
isn''t happy.
I just needed to check my sanity that the stripnl was ok.
Thanks
J
这篇关于从输入中删除换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!