热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

把一个纯文本文件中的所有行(行数不定,任意行)依次连接读到一个char*buf中去,我下列code出错在何处?在线等!请高手发言

兄弟们,问一个很弱的问题,谁有空请指教!在线等我想把一个纯文本文件中的所有行(行数不定,任意行)读到一个字符数组中去,如何写?要把一行一行的内容依次连接到一个char*buf
兄弟们,问一个很弱的问题,谁有空请指教!在线等 
  

我想把一个纯文本文件中的所有行(行数不定,任意行)读到一个字符数组中去,如何写? 
要把一行一行的内容依次连接到一个char *buf中,不能用数组,因为行数不定 
   
我是这样写的
char *buf_requ, *buf_reps, *pRequ, *pReps; 
pRequ = buf_requ ;pReps = buf_reps ;  // 保存指针首地址
while ( !feof( fp ) ) {
fputs( buffer, fp2 );
buf_reps = ( char * )malloc( lineLen + 1 ); 
assert( buf_reps != NULL );
memset(buf_reps, 0, lineLen );
strcat( buf_reps, buffer ); // 好象指针首地址丢了
}
这样每次malloc的地址不是连续的,所以strcat不能连接上是不?
但又应该如何解决呢?

4 个解决方案

#1


用vector 每一行都读取放入到其中,用法简单,操作容易

#2


这样会后问题的,因为每次分配的地址不同,你可以先读入,最后根据总的长度再分配一次。

#3


strcat( buf_reps, buffer ); //不用你为他分配内存
char *buf_requ, *buf_reps, *pRequ, *pReps; //buf_requ, buf_reps要分配内存给他
fputs( buffer, fp2 );//这个不是读文件内容
看不懂你的程序想要表达的意思
你究竟想做什么的?

#4


reallocSee Also
Memory Allocation Routines | calloc | free | malloc | Run-Time Routines and .NET Framework Equivalents
Requirements
Routine Required header Compatibility 
realloc  and  ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP 

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.
Reallocate memory blocks.

void *realloc(
   void *memblock,
   size_t size 
);
Parameters
memblock 
Pointer to previously allocated memory block. 
size 
New size in bytes. 
Return Value
realloc returns a void pointer to the reallocated (and possibly moved) memory block. The return value is NULL if the size is zero and the buffer argument is not NULL, or if there is not enough available memory to expand the block to the given size. In the first case, the original block is freed. In the second, the original block is unchanged. The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.

Remarks
The realloc function changes the size of an allocated memory block. The memblock argument points to the beginning of the memory block. If memblock is NULL, realloc behaves the same way as malloc and allocates a new block of size bytes. If memblock is not NULL, it should be a pointer returned by a previous call to calloc, malloc, or realloc.

The size argument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block can be in a different location. Because the new block can be in a new memory location, the pointer returned by realloc is not guaranteed to be the pointer passed through the memblock argument.

realloc calls malloc in order to use the C++ _set_new_mode function to set the new handler mode. The new handler mode indicates whether, on failure, malloc is to call the new handler routine as set by _set_new_handler. By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when realloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason. To override the default, call 

_set_new_mode(1)
early in your program, or link with NEWMODE.OBJ.

When the application is linked with a debug version of the C run-time libraries, realloc resolves to _realloc_dbg. For more information about how the heap is managed during the debugging process, see The CRT Debug Heap.

Requirements
Routine Required header Compatibility 
realloc  and  ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP 

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example
// crt_realloc.c
/* This program allocates a block of memory for
 * buffer and then uses _msize to display the size of that
 * block. Next, it uses realloc to expand the amount of
 * memory used by buffer and then calls _msize again to
 * display the new amount of memory allocated to buffer.
 */

#include 
#include 
#include 

int main( void )
{
   long *buffer;
   size_t size;

   if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
      exit( 1 );

   size = _msize( buffer );
   printf( "Size of block after malloc of 1000 longs: %u\n", size );

   /* Reallocate and show new size: */
   if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) )) 
        ==  NULL )
      exit( 1 );
   size = _msize( buffer );
   printf( "Size of block after realloc of 1000 more longs: %u\n", 
            size );

   free( buffer );
   exit( 0 );
}
Output
Size of block after malloc of 1000 longs: 4000
Size of block after realloc of 1000 more longs: 8000
See Also
Memory Allocation Routines | calloc | free | malloc | Run-Time Routines and .NET Framework Equivalents



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

Send feedback on this topic to Microsoft

© Microsoft Corporation. All rights reserved.

推荐阅读
author-avatar
与天使有染的救赎_164
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有