How to convert a values i get from a file (char) and store the values in to an array of double?

StackOverflow https://stackoverflow.com/questions/22704343

  •  23-06-2023
  •  | 
  •  

質問

I am reading data from a file, retrieving how many columns and rows I have ( data file ), everything of so far. Now I am trying to read the values one by one and store the values in to a 2D array (double). I get the values as char using getc but when I try to use atoi or atof to convert the values from char to double i get strange values.

double ther[j][number];
char c;
int tim=0,nther=0;


FILE *fp3 = fopen("data.txt", "r");
c = getc(fp3) ;

while (c!= EOF)
{ 

    ther[tim][nther]=atoi(&c);
    printf("%lf", ther[tim][nther]);


    nther++;
    c = getc(fp3);

    if(nther==number)
    {
        tim++;
        nther=0;
    }
    tim=0;
}

fclose(fp3);

any suggestion?… (i keep searching). Sorry well i have a file data.txt and this file has rows an columns of numbers:

1 2 3 4 5

6 7 8 9 10

So i need to store the data into a 2D array: Thanks to the answers i have some ideas like: use an string, then divide all values and stored every one of them. 1st line is string-> array[0][0], array[0][1], etc. Then move to the other row and do the same.

Until now i get some numeric values but none of them are stored at the data.txt.

役に立ちましたか?

解決 3

#include <stdio.h>

int main(){
    int j = 2;
    int number = 5;

    double ther[j][number];
    double d;
    int tim=0,nther=0;
    FILE *fp3 = fopen("data.txt", "r");

    while (fscanf(fp3, "%lf", &d)==1){
        ther[tim][nther]=d;
        printf("%g ", ther[tim][nther++]);

        if(nther==number){
            ++tim;
            nther=0;
            printf("\n");
        }
    }
{
    int r, c;
    for(r=0;r<tim;++r){
        for(c=0;c<number;++c){
            printf("%f ", ther[r][c]);
        }
        printf("\n");
    }
}
    return 0;
}

他のヒント

Firstly, it should be int c;. The getc() function has to be able to return a different value for all possible chars, and also a different value EOF (which signifies end-of-file, or stream error).

If you use char c;, then you must end up with EOF being converted to the value of a legal character, so you can't tell the difference between the two cases. In other words, your code might act like it hit EOF when in fact it just hit that particular character.

Moving on, atoi(&c) is not correct. If you read the documentation for the atoi function -- this is always a good idea when using functions -- you will find that it expects a string as input. However, &c is not a string. It is the address of a single char.

So your options are:

  • construct a string and pass it to atoi
  • don't use atoi

You didn't say what you were expecting to happen; but if you want to convert the character you read in of '3' to be the integer 3 then you can write: ther[tim][nther] = c - '0';.

However you should also have some code to handle cases when you read something that was not a number.

If you wanted to read in more than one character at a time (e.g. "31" -> 31) then the approach of using getc is not appropriate - you'll need to switch to using a function that reads multiple characters at once, such as fscanf or fgets.

Finally, you set tim to 0 every single loop iteration, so the tim++ never has any lasting effect, maybe this was an oversight.

You can read a double from a file using fscanf. An example would be:

double d;
fscanf("%f", &d);

fscanf can do much more than that so I'd recommend checking the documentation.

The method you're using gets a single character (as Matt McNabb pointed out - incorrectly). A character is 1 byte and can store the values 0-255. You're reading in text mode since you don't specify a mode with fopen (it is generally considered a good practice to explicitly ask for either binary "rb/wb" or text "rt/wt" mode). This means you read the ASCII value of a single digit.

So let's say the file contains:

7.2, 3.0, 1.0

The first call to getc(fp3) would get the character '7' which would have a value of 55 for ASCII. Obviously this is not going to give you what you're looking for.

Instead you could use:

double d[3];
fscanf("%f, %f, %f" &d[0], &d[1], &d[2]);

Which would get the three float values in the file.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top