替换fgets [英] Replacing fgets

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

问题描述

Hello C World& Fanatics


我正在尝试替换fgets并提供等价函数

BufferedInputReader :: readLine。

我称之为readLine作为get_Stream的函数。

在第4行,其中default_buffer_length从4改为24个代码

工作正常。

但是在同一行如果我从4 - 10更改default_buffer_length

的值,我得到一个内存错误。

如果更改同一个变量的值从4 --1024字节;

代码失败了。它不会在文件/标准输出上显示内容

流。


我正在尝试从对象的行返回字符指针

变量''temp''变为chars变量''result''的指针;而且我不知道如何在50或51行中做到这一点。


和最后一个问题

如果我尝试将对象变量''temp''的内存地址复制到

''结果''内存地址我将不得不在函数中调用该函数
该行80左右。

我想避免在80行左右调用无功能;因为

这样一些其他用户可以使用这个代码。

当前代码:

short check_Bits(int decimal){

int mask = sizeof(char)* 8; / * 8位* /

短位= 0;

(掩码,位= 0;掩码;位=((掩码和小数)?1:0 ),mask = mask>>

1);

返回位;

}


1 short get_Stream(FILE * pSource,char * result){

2 char * final_buffer; final_buffer = 0;

3 int default_buffer_length;

4 default_buffer_length = 4; / * nibble * /

5 int buffer_length = default_buffer_length;

6 char * temp; temp = 0;

7 char byte = 0;

8 int temp_length = 0;

9 / *为缓冲对象分配1024个字节* /

10个字符*缓冲区;缓冲区= 0; buffer =(char

*)malloc(default_buffer_length); / *当前fgets行* /

11 temp =(char *)malloc(buffer_length + default_buffer_length); / *

临时行+当前缓冲* /

12 / *阅读文件内容* /

13做{

14 fgets(buffer,default_buffer_length + 1,pSource);

15 temp_length = strlen(temp);

16 / *追加文字* /

17 if(temp_length == buffer_length){

18 buffer_length = buffer_length + default_buffer_length;

19 final_buffer =(char *)malloc(buffer_length);

20 final_buffer = strcpy(final_buffer,temp);

21 free(temp); temp = 0;

22 temp =(char
*)malloc(buffer_length + default_buffer_length);

23 temp = strcpy(temp,final_buffer);

24 free(final_buffer); final_buffer = 0;

25}

26

27 if(temp_length == 0)

28 temp = strcpy(temp,缓冲区);

29其他

30 temp = strcat(临时,缓冲区);

40字节= buffer [default_buffer_length-(check_Bits(buffer_length)==

0? 2:1)];

41} while((byte!=''\ n''&& byte!=''\ r'')&& byte! = 0&& byte!= 0xFF);

42免费(缓冲区);

43 / *免费内容* /

44 buffer_length = buffer_length-(check_Bits(strlen(temp))== 1?2:1);

45 temp [buffer_length] =''\''';

45 short ret = EXIT_SUCCESS;

47 if(byte == 0xFF || byte == 0 || byte == EOF)

48 ret = EXIT_FAILURE;

49其他{

50 ret = EXIT_SUCCESS;

51 printf("%s\\\
Done",temp);

52}

53 / *免费内容* /

54免费(临时);

55返回ret;

56}

57 / *功能结束* /

58

59 void main(int argc,const char * argv []){

60

61 FILE * pSourceFile = 0;

62 const char * ATOMIC_NAME;

63

64 ATOMIC_NAME =" /home/generic/algorith.txt" ;;

65 / * ATOMIC_NAME =" / usr / share / magic"; * /

66

67 printf("%s \ n" ;,ATOMIC_NAME);

68 pSourceFile = fopen(ATOMIC_NAME," rb");

69

70 if(pSourceFile == NULL ){

71 printf(未找到文件);

72退出(EXIT_FAILURE);

73}

74其他

75 fseek(pSourceFile,SEEK_SET,SEEK_SET);



77 char * ret ="" ;;

78 if(feof(pSourceFile)== EXIT_SUCCESS)

79 if(get_Stream(pSourceFile,ret)== EXIT_SUCCESS)

80 printf (<< 2> \ n%s< 1>" ret);

81 fclose(pSourceFile);

82}

有人可以提供帮助吗。

我稍后会在HP-UX平台上使用这些相同的功能因此

我不能使用GCC中已有的函数getline但是在HP-UX平台上没有



谢谢。

Hello C World & Fanatics

I am trying replace fgets and provide a equavivalant function of
BufferedInputReader::readLine.
I am calling this readLine function as get_Stream.
In the line 4 where default_buffer_length is changed from 4 --24 code
works fine.
But on the same line if I change the value of default_buffer_length
from 4 --10 and I get a memory error.
And if the change the value of the same variable from 4 --1024 bytes;
the code just fails. It does not display an content on the file/ stdout
stream.

I am trying return the pointer of chars from the line from the object
variable '' temp '' into the pointer of chars variable '' result ''; and I
do not know how to do this in the line 50 or 51.

And a last question
If I try to return copy the object variable '' temp '' memory address to
the '' result '' memory address I will have to call the function free in
the line 80 or so.
I want to avoid calling function free in the line 80 or so; because
this way some other user can use this code.
Current Code:
short check_Bits(int decimal){
int mask=sizeof(char)*8; /* 8 bits */
short bit = 0;
for(mask,bit = 0;mask; bit = (( mask & decimal) ? 1:0),mask = mask >>
1);
return bit;
}

1 short get_Stream(FILE* pSource,char* result){
2 char* final_buffer;final_buffer=0;
3 int default_buffer_length;
4 default_buffer_length=4; /* nibble */
5 int buffer_length = default_buffer_length;
6 char *temp;temp = 0;
7 char byte = 0;
8 int temp_length= 0;
9 /*allocate 1024 bytes to buffer object*/
10 char * buffer;buffer = 0;buffer = (char
*)malloc(default_buffer_length); /*current fgets line */
11 temp = (char *)malloc(buffer_length+default_buffer_length); /*
temporary line + current buffer */
12 /* read the file contents */
13 do{
14 fgets(buffer,default_buffer_length+1,pSource);
15 temp_length = strlen(temp);
16 /* append text */
17 if(temp_length == buffer_length){
18 buffer_length = buffer_length+default_buffer_length;
19 final_buffer = (char *)malloc(buffer_length);
20 final_buffer = strcpy(final_buffer,temp);
21 free(temp);temp = 0;
22 temp = (char
*)malloc(buffer_length+default_buffer_length);
23 temp = strcpy(temp,final_buffer);
24 free(final_buffer);final_buffer = 0;
25 }
26
27 if(temp_length == 0)
28 temp = strcpy(temp,buffer);
29 else
30 temp = strcat(temp,buffer);
40 byte = buffer[default_buffer_length-(check_Bits(buffer_length) ==
0? 2:1)];
41 }while((byte != ''\n'' && byte != ''\r'') && byte!=0 && byte!=0xFF);
42 free(buffer);
43 /* free the contents */
44 buffer_length = buffer_length-(check_Bits(strlen(temp)) == 1? 2:1);
45 temp[buffer_length]=''\0'';
45 short ret=EXIT_SUCCESS;
47 if(byte==0xFF || byte==0 || byte==EOF)
48 ret = EXIT_FAILURE;
49 else {
50 ret = EXIT_SUCCESS;
51 printf("%s\nDone",temp);
52 }
53 /* free the contents */
54 free(temp);
55 return ret;
56 }
57 /*End of Function */
58
59 void main(int argc,const char* argv[]){
60
61 FILE* pSourceFile=0;
62 const char* ATOMIC_NAME;
63
64 ATOMIC_NAME = "/home/generic/algorith.txt";
65 /*ATOMIC_NAME = "/usr/share/magic";*/
66
67 printf("%s\n",ATOMIC_NAME);
68 pSourceFile = fopen(ATOMIC_NAME,"rb");
69
70 if(pSourceFile==NULL){
71 printf("File not found");
72 exit(EXIT_FAILURE);
73 }
74 else
75 fseek(pSourceFile,SEEK_SET,SEEK_SET);
76
77 char* ret = "";
78 if(feof(pSourceFile)==EXIT_SUCCESS)
79 if(get_Stream(pSourceFile,ret)==EXIT_SUCCESS)
80 printf("<2>\n%s<1>",ret);
81 fclose(pSourceFile);
82 }
Can somebody help.
I will use these same function on HP-UX platform later on so therefore
I cannot use a function getline which is already available in GCC but
not available on the HP-UX platform.

Thanks.

推荐答案

FireHead写道:
FireHead wrote:

>

我是尝试替换fgets并提供

BufferedInputReader :: readLine的等效函数。
>
I am trying replace fgets and provide a equavivalant function of
BufferedInputReader::readLine.



这是一个C ++操作。

This is a C++ operation.


>
>



.... snip ...

.... snip ...


>

有人可以提供帮助吗。

我稍后会在HP-UX平台上使用这些相同的功能所以

因此我不能使用已经
$ b的函数getline $ b在GCC中可用,但在HP-UX平台上不可用。
>
Can somebody help.
I will use these same function on HP-UX platform later on so
therefore I cannot use a function getline which is already
available in GCC but not available on the HP-UX platform.



您的代码难以识别且无法编译(不完整,格式不佳

格式化等)。另外C ++在这里是偏离主题的,我们只用ISO标准C来处理




我建议你试试ggets,这是用纯标准C编写的,和

可在以下网址获得:


< http://cbfalconer.home.att.net/download/>


-

计算机软件行业最惊人的成就

是它继续取消稳定而惊人的
$ b计算机硬件行业获得的$ b收益...... - Petroski


-

通过免费的Usenet帐户从 http://www.teranews.com

Your code is illegible and not compilable (incomplete, poorly
formatted, etc.). In addition C++ is off-topic here, we deal
solely with ISO standard C.

I suggest you try ggets, which is written in pure standard C, and
is available at:

<http://cbfalconer.home.att.net/download/>

--
"The most amazing achievement of the computer software industry
is its continuing cancellation of the steady and staggering
gains made by the computer hardware industry..." - Petroski

--
Posted via a free Usenet account from http://www.teranews.com


Hello CBFalconer


我正在查看这段代码更多的C代码而不是C ++代码。

我试过这段代码。
Hello CBFalconer

I am looking at this code more of C code rather than a C++ code.
I have tried this code.

< http://cbfalconer.home.att.net/download/>
<http://cbfalconer.home.att.net/download/>



我发现了一些内存泄漏。

我已下载整个代码并发现内存泄漏。

任何我都不能使用C ++(我希望),因为这个呈现的C代码将转换为UNIX转换为


但是如果我使用C ++算法,那么我仍然是指出

同样的问题/问题。

and I have found a few memory leaks.
I have download the entire code and find the memory leak.
Any I cannot use C++ (I wish) as this presenting C code will be
transitioned to UNIX.
But eventhough if I use C++ algorithmcally speaking I am still pointed
at same problem/issue.


> BufferedInputReader ::的readLine。
> BufferedInputReader::readLine.



基于ISO C99标准BufferedInputRead是命名空间和

readLine,它通常只是一个函数。


如果我是你让我使用你的PDL(公共领域许可证)代码。


感谢您的回复。

CBFalconer写道:

Based on ISO C99 standard BufferedInputRead is the namespace and
readLine which as usual is just a function.

If I am you presribing me to use your PDL (Public Domain Licence) code.

Thanks for reply thou.
CBFalconer wrote:


FireHead写道:
FireHead wrote:


我正在尝试替换fgets并提供等价函数

BufferedInputReader :: readLine。

I am trying replace fgets and provide a equavivalant function of
BufferedInputReader::readLine.



这是一个C ++操作。


This is a C++ operation.



... snip ...

... snip ...



有人可以提供帮助。

我稍后会在HP-UX平台上使用这些相同的功能所以

因此我不能使用已经
$的函数getline b $ b在GCC中可用,但在HP-UX平台上不可用。

Can somebody help.
I will use these same function on HP-UX platform later on so
therefore I cannot use a function getline which is already
available in GCC but not available on the HP-UX platform.



您的代码难以识别且无法编译(不完整,格式不佳

格式化等)。另外C ++在这里是偏离主题的,我们只用ISO标准C来处理




我建议你试试ggets,这是用纯标准C编写的,和

可在以下网址获得:


< http://cbfalconer.home.att.net/download/>


-

计算机软件行业最惊人的成就

是它继续取消稳定而惊人的
$ b计算机硬件行业获得的$ b收益...... - Petroski


-

通过免费的Usenet帐户从 http://www.teranews.com


FireHead说:
FireHead said:

你好CBFalconer


我正在查看这段代码更多的C代码而不是C ++代码。

我试过这段代码。
Hello CBFalconer

I am looking at this code more of C code rather than a C++ code.
I have tried this code.

> < HTTP://cbfalconer.home.att.net/download/>
> <http://cbfalconer.home.att.net/download/>



我发现了一些内存泄漏。

and I have found a few memory leaks.



< snorfle>


好​​吧,Chuck,这是你眼中的一个。 :-)


-

Richard Heathfield

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

电子邮件:rjh在上面的域名(但显然放弃了www)

<snorfle>

Well, Chuck, that''s one in your eye. :-)

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


这篇关于替换fgets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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