从fgets读取的缓冲区中删除换行符 [英] removing newline character from the buffer read by fgets
问题描述
是否有任何有效的方法可以从
fgets()读取
缓冲区中删除换行符?
是否有任何类似于fgets()的库函数,但也告诉
它读入缓冲区有多少
字节?
Is there any efficcient way of removing the newline character from the
buffer read by
fgets() ?
Is there any library function that is similar to fgets() but also tells
how many
bytes it read into the buffer ?
推荐答案
在文章< 11 ********************* @ h54g2000cwb.googlegroups中。 com>,
ju**********@yahoo.co.in < ju ********** @ yahoo.co.inwrote:
In article <11*********************@h54g2000cwb.googlegroups. com>,
ju**********@yahoo.co.in <ju**********@yahoo.co.inwrote:
>有没有任何有效的方法来删除来自
fgets()读取的
缓冲区中的换行符?
>Is there any efficcient way of removing the newline character from the
buffer read by
fgets() ?
>是否有任何与fgets()类似的库函数,但也告诉
它读取了多少个字节进入缓冲区?
>Is there any library function that is similar to fgets() but also tells
how many
bytes it read into the buffer ?
fgets()用空字符终止字符串,所以你可以简单地使用
的内容
<接下来是
/ *代码片段,不是作为独立的例程* /
fgetsresult = fgets(outbuffer,sizeof(outbuffer)-1,stream);
if(fgetsresult == NULL){
/ *处理无字符串* /
}否则{
size_t newbuflen = strlen(outbuffer);
if(outbuffer [newbuflen - 1] ==''\ n'')outbuffer [newbuflen - 1] =''\ 0''; < br $>
}
-
法律 - 它是商品
- Andrew Ryan(环球邮报,2005/11/26)
fgets() terminates the string with the null character, so you can
simply use something along the lines of
/* code fragment follows, not intended as standalone routine */
fgetsresult = fgets( outbuffer, sizeof(outbuffer)-1, stream);
if (fgetsresult == NULL) {
/* handling for no string available */
} else {
size_t newbuflen = strlen(outbuffer);
if (outbuffer[newbuflen - 1] == ''\n'') outbuffer[newbuflen - 1] = ''\0'';
}
--
"law -- it''s a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
2006年11月27日19:03:10 -0800,ju ********* *@yahoo.co.in"
< ju ********** @ yahoo.co.inwrote in comp.lang.c:
On 27 Nov 2006 19:03:10 -0800, "ju**********@yahoo.co.in"
<ju**********@yahoo.co.inwrote in comp.lang.c:
是否有任何有效的方法从<。中删除换行符br />
缓冲区读取
fgets()?
Is there any efficcient way of removing the newline character from the
buffer read by
fgets() ?
#include< stdio.h>
#include< string.h>
$ b函数内的$ b / * * /
if(NULL!= fgets(my_buffer,sizeof my_buffer,stdin)
{
char * nlptr = strchr(my_buffer,''\ n'');
if(nlptr)* nlptr =''\ 0'';
}
#include <stdio.h>
#include <string.h>
/* inside a function */
if (NULL != fgets(my_buffer, sizeof my_buffer, stdin)
{
char *nlptr = strchr(my_buffer, ''\n'');
if (nlptr) *nlptr = ''\0'';
}
是否有任何类似于fgets()的库函数,但也告诉
它读入多少
字节缓冲区?
Is there any library function that is similar to fgets() but also tells
how many
bytes it read into the buffer ?
你当然可以写一个,一个fgets()的简单包装器:
size_t my_fgets(char * buffer,size_t size,FILE * fp)
{
size_t result = 0;
if(NULL!= fgets(my_buffer,sizeof my_buffer) ,fp)
{
/ *可选,删除换行符* /
char * nlptr = strchr(my_buffer,''\ n' ');
if(nlptr)* nlptr =''\''';
result = strlen(my_buffer) ;
}
返回结果;
}
-
Jack Klein
主页: http://JK-Technology.Com
常见问题解答
comp.lang.c http://c-faq.com/
comp.lang.c ++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c -c ++
http:/ /www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
You can certainly write one, a simple wrapper for fgets():
size_t my_fgets(char *buffer, size_t size, FILE *fp)
{
size_t result = 0;
if (NULL != fgets(my_buffer, sizeof my_buffer, fp)
{
/* optional, remove newline */
char *nlptr = strchr(my_buffer, ''\n'');
if (nlptr) *nlptr = ''\0'';
result = strlen(my_buffer);
}
return result;
}
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
ju ********** @ yahoo.co.in 写道:
是否有任何有效的方法可以从
fgets()读取的
缓冲区中删除换行符?
Is there any efficcient way of removing the newline character from the
buffer read by
fgets() ?
#include< stdio.h>
#include< string.h>
#include < stdlib.h>
int use_fgets(void)
{
char buffer [BIGENOUGH]; / * #define BIGENOUGH其他地方* /
char * nl;
如果(!fgets(缓冲区,BIGENOUGH,stdin))返回EXIT_FAILURE;
if((nl = strchr(buffer,''\ n'')))* nl = 0;
/ *在这里使用缓冲区* /
返回EXIT_SUCCESS ;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int use_fgets(void)
{
char buffer[BIGENOUGH]; /* #define BIGENOUGH elsewhere */
char *nl;
if (!fgets(buffer, BIGENOUGH, stdin)) return EXIT_FAILURE;
if ((nl = strchr(buffer, ''\n''))) *nl = 0;
/* use buffer here */
return EXIT_SUCCESS;
}
是否有任何类似于fgets()的库函数,但也告诉
如何很多
字节它读入缓冲区?
Is there any library function that is similar to fgets() but also tells
how many
bytes it read into the buffer ?
#include< stdio.h>
#include< string.h>
int fgets_with_chars_read (char * buffer,int maxchars,FILE * f)
{
if(!fgets(buffer,maxchars,f))return 0;
返回strlen(缓冲区); / *或1 + strlen(缓冲区)如果你想要''\ 0''
算* /
}
#include <stdio.h>
#include <string.h>
int fgets_with_chars_read(char *buffer, int maxchars, FILE *f)
{
if (!fgets(buffer, maxchars, f)) return 0;
return strlen(buffer); /* or 1+strlen(buffer) if you want the ''\0''
counted */
}
这篇关于从fgets读取的缓冲区中删除换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!