使用gcc在Ubuntu 12.04上运行

我有一个文本文件,其具有如下所示的10个值:


  0020.50 0020.49 0020.47 0020.46 0020.51 0020.50 0020.50 0020.49 0020.49 0020.50


我想在缓冲区中读取这些值,然后对它进行一些计算。所有值都必须视为浮点数。

所以我要做的是,我首先在缓冲区buffer中读取10个字符,然后使用atof()将每个值从字符转换为浮点并存储在bufferFloat中。但是我看到bufferFloat没有正确的值。

这是我的代码:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

using namespace std;

int main()
{
FILE *fp;
int i,x;
int read;

char * buffer;

buffer=(char *)malloc(20);

fp=fopen("Xvalues.txt","r");

read=fread(buffer,1,10,fp);

printf("no. of bytes read %d", read);

float bufferFloat[10];
int j;

for(j=0;j<10;j++)
{

    bufferFloat[j]=atof(&buffer[j]); //converting characters to buffers


}

int k;

for (k=0;k<10;k++)
{

    printf("printing buffers as floats: %f \n", bufferFloat[k]);

}

fclose(fp);
return 0;
}


输出量

root@ubuntu:/home/ravi/Desktop/New/build# ./new1
file opened sccesfully
no. of bytes read 10printing buffers as floats: 20.500000
printing buffers as floats: 20.500000
printing buffers as floats: 20.500000
printing buffers as floats: 0.500000
printing buffers as floats: 0.500000
printing buffers as floats: 50.000000
printing buffers as floats: 0.000000
printing buffers as floats: 0.000000
printing buffers as floats: 0.000000
printing buffers as floats: 0.000000
root@ubuntu:/home/ravi/Desktop/New/build#


这不是我们存储在文本文件中的内容。

请帮我解决这个问题。

提前致谢。

这是我使用令牌化修改的代码

 #include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main()

{

FILE * fp ;

int read;
int i;

char * buffer;
buffer = (char *)malloc(2000);

fp=fopen("Xvalues.txt", "r");
if(fp==NULL)

{

printf("error reading the file");

}



//storing one line  in  buffer using fgets
if (fgets(buffer, 80, fp)!=NULL){

//    puts(buffer) ;

    }


//Tokanizing


const char s[2]= " ";

//get the first token

char * token;
token = strtok(buffer, s);


while (token!=NULL)
{

//printf("%s\n", token);
token  = strtok(NULL, s);

}


//converting to float
float bufferFloat[10000];
float ret;
ret =strtof(buffer, bufferFloat);

for(i=0; i< 10; i++)
{
printf("float values are%f\n", bufferFloat[i]);
}

fclose (fp);
return 0;


}


仍然输出不正确

root@ubuntu:/home/ravi/Desktop/New# gcc test.c
test.c: In function ‘main’:
test.c:44:7: warning: assignment makes pointer from integer without a cast [enabled by default]
test.c:51:8: warning: assignment makes pointer from integer without a cast [enabled by default]
test.c:60:1: warning: passing argument 2 of ‘strtof’ from incompatible pointer type [enabled by default]
/usr/include/stdlib.h:173:14: note: expected ‘char ** __restrict__’ but argument is of type ‘float *’
root@ubuntu:/home/ravi/Desktop/New# ./a.out
float values are0.000000
float values are0.000000
float values are0.000000
float values are0.000000
float values are0.000000
float values are0.000000
float values are0.000000
float values are0.000000
float values are0.000000
float values are0.000000
root@ubuntu:/home/ravi/Desktop/New#

最佳答案

通过执行类似read 10 characters in a buffer的操作不会在float中给您10个buffer [或其他内容]。这不是您应该从文件读取的方式。我的建议是


使用fgets()从文件中读取一行。
使用适当的定界符(可能是空格)使用strtok()对读取缓冲区进行标记。
使用strtof()(优于atof())将每个令牌转换为浮点并存储到数组中。



笔记:
 


1. fgets()读取并存储结尾的\n。标记时要小心。
2. fgets()中提供的缓冲区应该很长才能容纳文件中的完整行输入。


另外,值得一提的是,请在通话后对fopen()进行NULL检查,以检查fp通话是否成功。 fread()也是如此。



编辑:

下面的工作代码。

#include<stdio.h>
#include<stdlib.h>
#include <string.h>

#define MAXVAL 1024

int main()

{
        FILE * fp  = NULL;
        int read = 0;
        int i = 0;
        char * token = NULL;

        char buffer[MAXVAL] = {0};
        float bufferFloat[10] = {0}; //better way, use dynamic allocation

        fp=fopen("Xvalues.txt", "r");
        if(fp==NULL)
        {
                printf("error opening the file");
                return -1;      //don't continue
        }

        //storing one line  in  buffer using fgets
        if ( ! fgets(buffer, MAXVAL, fp) ){
            printf("error reading the file");
            return -2;      //don't continue
        }
        //Tokanizing
        const char *s = " \n";      //because of fgets() stroing \n
        //get the first token

        token = strtok(buffer, s);
        i = 0;
        while (token!=NULL)
        {
                bufferFloat[i++] = strtof(token, NULL);     //I used NULL used for simplicity, you follow the proper way.
                token  = strtok(NULL, s);
        }

        for(i=0; i< 10; i++)
        {
                printf("float values are%f\n", bufferFloat[i]);
        }

        fclose (fp);
        return 0;
}


编译使用

gcc test.c --std=c99




./a.out


输出:

[sourav@broadsword temp]$ ./a.out
float values are20.500000
float values are20.490000
float values are20.469999
float values are20.459999
float values are20.510000
float values are20.500000
float values are20.500000
float values are20.490000
float values are20.490000
float values are20.500000
[sourav@broadsword temp]$

10-08 14:34