0

I have a file with data like

zz:yy:xx.xxx [-]pp.pp

The minus is optional. I need to separate the data. I need that [-]pp.pp to the next actions in float type. How can I make an float array with that part of data?

Here I opened the file and printed all data.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 2000
FILE *file;
int main()
{
    file = fopen("data.txt" , "r");
    int i,znak=0;
    char string[N];
    while ((znak = getc(file)) != EOF)
    {
    string[i]=znak;
    printf("%c",string[i]);
    i++;

    }
    return 0;
}
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Mateo
  • 3
  • 2
  • 2
    It seems that you can simply use `fscanf()`. – Iharob Al Asimi Oct 23 '16 at 11:55
  • `fscanf_s` should be preferred. – Dai Oct 23 '16 at 11:59
  • 1
    Turn on compiler warnings, there's at least use of uninitialized variable *i* in your code (which is bad). Also seems you have partially edited (anonymized? translated?) code, *file* vs *plik* variables. Please provide real code which compiles and doesn't produce obvious waenings (or ask about compiler errors/warnings if you don't know how to fix those). – hyde Oct 23 '16 at 12:01
  • 1
    @Dai NO, that is not ANSI c. – Iharob Al Asimi Oct 23 '16 at 12:04
  • 2
    @iharob It is since C11. – Some programmer dude Oct 23 '16 at 12:17
  • 1
    @Someprogrammerdude It's not, it's an "*extensions*". – Iharob Al Asimi Oct 23 '16 at 12:27
  • 2
    @iharob Okay, but a *standard* extension, unlike the compiler-specific extensions that existed before it. :) – Some programmer dude Oct 23 '16 at 12:28
  • 1
    See [Do you use the TR 24731 'safe' functions?](http://stackoverflow.com/questions/372980/) for a discussion of the merits and otherwise of the functions defined in Annex K of ISO/IEC 9899:2011, which were previously in TR 24731 and which include `fscanf_s()`. Note that two major problems are (1) only Microsoft has implemented functions with the `_s` suffixes in their system, and (2) what Microsoft implemented doesn't match what the standard defined. That means it is impossible to use the functions portably; they aren't available on Unix and wouldn't be the same as on Windows if they were. – Jonathan Leffler Oct 23 '16 at 16:13

1 Answers1

2

Try like this

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

#define FORMAT "%*[^:]:%*[^:]:%*[^: ] %f"

int main(void)
{
    FILE *file;
    float value;
    int size;
    float *array;
    // Open the file
    file = fopen("data.txt", "r");
    // This is really important
    if (file == NULL) {
        fprintf(stderr, "Can't open the file\n");
        return -1;
    }
    size = 0;
    // Count the number of entries in the file
    while (fscanf(file, FORMAT, &value) == 1) {
        size += 1;
    }
    // Reset the file position to the beginning
    rewind(file);
    // Allocate space for the array
    array = malloc(size * sizeof(*array));
    // And, ALWAYS check for errors
    if (array == NULL) {
        fprintf(stderr, "Out of memory\n");
        fclose(file);
        return -1;
    }
    // Extract the data from the file now
    for (int i = 0 ; i < size ; ++i) {
        fscanf(file, FORMAT, &array[i]);
    }
    // The file, is no longer needed so close it
    fclose(file);
    // Do something with the array 
    handle_array(size, array);
    // Free allocated memory
    free(array);
    return 0;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • It's tempting to simplify the format to: `"%*s %f"` where the space is optional. It won't barf if the colons aren't present as shown, but is simpler to understand. – Jonathan Leffler Oct 23 '16 at 16:19