dat文本的数据格式是这样 0 986 10 0.245013 3.265986 0.447903 0.006471 0 1654 14 0.402352 10.947632 1.034885 0.050735 0 1616 152 0.717833 15.144267 3.373916 0.141620 1 1097 0 0.986062 23.380904 0.000000 0.254367 1 1358 0 0.271866 5.778606 0.293544 0.011625 1 1365 0 0.242986 1.707825 0.500000 0.002850 1 1582 0 0.238849 2.000000 0.000000 0.002550 1 1585 0 0.235804 1.779513 0.500000 0.001475 1 1597 0 0.244481 2.872281 0.500000 0.003233 1 2062 0 0.296608 5.188127 0.000000 0.020833 定义的结构体是这样 typedef struct{ int label; int x; int y; double intensity; double sigmaX; double sigmaY; double sigmaIntensity; }PeakData; 现在要把每行的数据按label x y intensity sigmaX sigmaY sigmaIntensity读取存到PeakData *m_all的结构中 FILE *fp = fopen(sFile, "r"); int x1; int y1; double intensity1; double sigmaX1; double sigmaY1; double sigmaIntensity1; int label1; int i = 0; while(!feof(fp)) { m_all = (PeakData *)realloc(m_all, (i+1)*sizeof(PeakData)); fscanf(fp,"%d %d %d %f %f %f %f",&label1,&x1,&y1,&intensity1,&sigmaX1,&sigmaY1,&sigmaIntensity1); m_all[i].label = label1; m_all[i].x=x1; m_all[i].y=y1; m_all[i].intensity=intensity1; m_all[i].sigmaX=sigmaX1; m_all[i].sigmaY=sigmaY1; m_all[i].sigmaIntensity=sigmaIntensity1; i++; } fclose(fp); 出错了不知道怎么改 ,求大牛指教修改,非常感谢 |
|
10分 |
格式问题吧,”%d %d %d %f %f %f %f”改成”%d\t%d\t%d\t%f\t%f\t%f\t%f”
|
30分 |
在第12行加一句:
m_all=NULL; 因为: realloc Reallocate memory blocks. void *realloc( void *memblock, size_t size ); Routine Required Header Compatibility For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version 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. Parameters memblock Pointer to previously allocated memory block size New size in bytes 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 Using C Run-Time Library Debugging Support. Example /* REALLOC.C: This program allocates a block of memory for #include <stdio.h> void main( void ) if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL ) size = _msize( buffer ); /* Reallocate and show new size: */ free( buffer ); Output Size of block after malloc of 1000 longs: 4000 Memory Allocation Routines See Also calloc, free, malloc |
如果m_all没定义的话,还需要在第11行加一句
PeakData *m_all; |
|
非常感谢, 添加了m_all=NULL;就解决了 |
|
m_all在头文件中已经定义了 |