读一串未知大小的字符串 [英] Reading a string of unknown size

查看:74
本文介绍了读一串未知大小的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从stdin读取字符并将它们保存在字符串中。

问题是我不知道会读多少字符。


Francesco

-

-------------------------------------

http://www.riscossione.info/

解决方案

Tonio Cartonio说:


我必须从stdin中读取字符并将它们保存在字符串中。

问题是我不知道会读多少字符。

http:// www。 cpax.org.uk/prg/writings/fgetdata.php
http://cbfalconer.home.att.net/download/ggets.zip
http://www.iedu.com/mrd/c/getsm.c
http://storm.freeshell.org/anysize.c


带你的选择。


(即将推出 - Eric Sosman的等价物,如果他能记得这次将它附加到

这封邮件......)< br $>

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。


Tonio Cartonio写道:


我必须从stdin中读取字符并将它们保存在字符串中。

问题是我不知道会读多少字符。



您必须一次性读取输入(使用

getc()或fgetc ()),或一次一行(使用fgets()),并将它们存储在一块内存块中,当你在

中读取时,它可以动态调整大小更多输入。


自己这么做很容易,但是如果你想使用一个已经

的可用,请试试CBFalconer'' ggets()函数。搜索群组的

存档。 URL经常被提到。


我必须从stdin读取字符并将它们保存在字符串中。


问题是我不知道会读多少字符。



int main()

{

char * str = NULL,ch;

int i = 0;

str =(char *)malloc(2 * sizeof(char));

* str =''\''' ;


while((ch = getchar())!=''\ n'')

{

* (str + i)= ch;

i ++;

str =(char *)realloc(str,(2 * sizeof(char))+ i);

}

*(str + i)=''\ 0'';


printf(" \ nn \\ n%s",str);


getch();

返回0;

}

:)

-

问候

Santosh S Nayak

电子邮件 - sa *********** @ gmail.com

WebPage - http://santoshsnayak.googlepages.com


I have to read characters from stdin and save them in a string. The
problem is that I don''t know how much characters will be read.

Francesco
--
-------------------------------------

http://www.riscossione.info/

解决方案

Tonio Cartonio said:

I have to read characters from stdin and save them in a string. The
problem is that I don''t know how much characters will be read.

http://www.cpax.org.uk/prg/writings/fgetdata.php
http://cbfalconer.home.att.net/download/ggets.zip
http://www.iedu.com/mrd/c/getsm.c
http://storm.freeshell.org/anysize.c

Take your pick.

(Coming soon - Eric Sosman''s equivalent, if he can remember to attach it to
the email this time...)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


Tonio Cartonio wrote:

I have to read characters from stdin and save them in a string. The
problem is that I don''t know how much characters will be read.

You''ll have to read the input, either character-at-a-time, (using
getc() or fgetc()), or line at a time, (using fgets()), and store them
into a block of memory which can be dynamically resized as you read in
more input.

It''s fairly easy to do this yourself, but if you want to use an already
available one, try CBFalconer''s ggets() function. Search the group''s
archive. The URL is mentioned quite regularly.


I have to read characters from stdin and save them in a string. The

problem is that I don''t know how much characters will be read.

int main()
{
char *str = NULL, ch ;
int i = 0 ;
str = (char*) malloc (2*sizeof(char)) ;
*str = ''\0'' ;

while( (ch=getchar()) != ''\n'' )
{
*(str+i) = ch ;
i++ ;
str = (char*) realloc(str, (2*sizeof(char)) + i ) ;
}
*(str+i) = ''\0'' ;

printf("\n\n %s ", str) ;

getch() ;
return 0;
}
:)
--
Regards
Santosh S Nayak
E-Mail - sa***********@gmail.com
WebPage -- http://santoshsnayak.googlepages.com


这篇关于读一串未知大小的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆