C语言,读取txt文件,内容为零

FILE *fp_read;
fp_read = fopen( "atom_guiyi.txt", "r" );//读取现有原子库的文件

float ****ku_final;
ku_final = ( float **** )malloc( nric * sizeof( float *** ) );
for( i = 0; i < nric; i++ )
{
    ku_final[ i ] = ( float *** )malloc( noff * sizeof( float ** ) );
    for( j =0; j < noff; j++ )
    {
        ku_final[i][j] = ( float ** )malloc( n_phase * sizeof( float * ) );
        for( z = 0; z < n_phase; z++ )
        {
            ku_final[i][j][z] = ( float * )malloc( length_ricker * sizeof( float ) );
            for( n = 0; n < length_ricker; n++ )
            { 
                ku_final[i][j][z][n] = 0.f;
            }
        }
    }
}   



FILE *fp_atom;
fp_atom = fopen( "dictionary.txt", "w" );//将读取的原子库写入到文本文件,证明读取的正确

for( iric = 0; iric < nric; iric++ )//频率
{
    ifre = iric * dfre + sfre; //第 i 个频率
    for( ioff = 0; ioff < noff; ioff++ )//偏移 
    {
        for( i_phase = 0; i_phase < n_phase; i_phase++ )//相位
        {
            phase = i_phase * d_phase + s_phase;
            for( n = 0; n < length_ricker ; n++ )
            {
                if( fp_read == NULL )
                    printf( "cannot open");
                else
                {
                    fscanf( fp_read, "%*d%*d%*d%*f%.16f", &ku_final[ iric ][ ioff ][ i_phase ][ n ] );
                    fprintf( fp_atom, "%d\t%d\t%d\t%f\t%.16f\n", ifre, ioff, phase, n * dt, ku_final[ iric ][ ioff ][ i_phase ][ n ] );
                }
            }
        }
    }
}
fclose( fp_read );
fclose( fp_atom );


atom_guiyi.txt文件的数据是五列,但是最后读出来最后一列是零,我想知道是怎么一回事?
![图片说明](https://img-ask.csdn.net/upload/201705/15/1494834908_794700.png)
c

1个回答

很简单,格式化输入的字符串不能用"%.16f",正确格式是%[数字][hl][转换字符],数字在这里表示的是读取内容的长度(不是浮点精度)。参考K&R C B1.3节

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐