user7804708
user7804708

Reputation:

C fscanf reading data from file error

I am new in C and I am trying to write a program that will read data from a file and store them in an array (so I can later work with them).

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


int day[3], month[3], year[3], hour[3], minute[3], second[3];
float value[3];


int main()
{  
    // Open the file for read
    FILE* file1 = fopen("test.txt", "r");

    // Safety check
    if (file1 == NULL)
    {
        printf("Error: file1 == NULL\n");
        getchar();
        return 0;
    }

    // Open the new file for write
    FILE* file2 = fopen("new.txt", "w");

    // Safety check
    if (file2 == NULL)
    {
        printf("Error: file2 == NULL\n");
        getchar();
        return 0;
    }


    int j = 0;

        for (j = 0; j < 4; j++)
        {
            int count = fscanf(file1, "%d-%d-%d %d:%d:%d %f", &day[j], &month[j], &year[j], &hour[j], &minute[j], &second[j], &value[j]);
            printf("%d-%d-%d %d:%d:%d %f\n", day[j], month[j], year[j], hour[j], minute[j], second[j], value[j]);
            // Check for finising early
            if (count == EOF)
            {
                printf("EOF");
            }
        }

    // Close the original file
    fclose(file1);
    // Close the target file
    fclose(file2);  

    getchar();
}

The file contains the data in this format:

20-03-17 08:49:01 28,515
20-03-17 08:49:31 29,1837
20-03-17 08:50:01 27,845

EDIT AFTER COMMENTS:

The code now does not crash any more and it works almost perfectly!

This is what comes out:

20-3-17 8:49:1 28.514999
20-3-17 8:49:31 29.183701
20-3-17 8:50:1 27.844999
1105469112-1-20 17:8:49 0.000000

is there a way to fix the decimal problem? and any idea what is that last line?

Thank you for your time!

Upvotes: 2

Views: 459

Answers (2)

Michael Strobel
Michael Strobel

Reputation: 367

This is how i do it, but I'm certainly not a master at programming in C myself. I think best would be to use stat though, for file size etc.

You could do something like:

   #include <stdio.h>
   #include <string.h>
   #include <stdlib.h>
   #include <sys/stat.h>
   #define dir "/path/to/file"
   #define MAX_WORDS 32
   int main() {
       FILE *fp;
       struct stat stz;
       stat(dir, &stz);
       int charz;
       char data[4096], buff[8];
       char *words[MAX_WORDS];         
       if (stz.st_size > 0) {
            fp = fopen(dir, "r");
            fseek(fp, 0, SEEK_SET);
            for (i = 0; i < stz.st_size; i++) {
                charz = fgetc(fp);
                if (charz == 10) {
                    strcat(data, " ");

                } else {
                    sprintf(buff, "%c", charz);
                    strcat(data, buff);
                }
            }
            fclose(fp);
            words[0] = strtok(data, " ");
            for (i = 0; i < MAX_WORDS; i++) {
                if (!words[i]) {
                    break;
                }
                words[i+1] = strtok(NULL, " ");
            }
       } else {
             printf("No data available in given directory");
       }
       return 0;
   }

This puts each byte into variable charz, then to a single character buffer, and concatenates said character onto the end of string data if the character is not equal to 10 (NewLine), otherwise, it adds a space, or delimiter.

Then, you can initialize an array of words by splitting the string.

Upvotes: 0

hallazzang
hallazzang

Reputation: 701

In here:

for (j = 0; j < 3; j++)
{
    int count = fscanf(file1, "%i-%i-%i %i:%i:%i %f ", day[j], month[j], year[j], hour[j], minute[j], second[j], value[j]);
    printf("%d-%d-%d %d:%d:%d %f\n", day[j], month[j], year[j], hour[j], minute[j], second[j], value[j]);
    // Check for finising early
    if (count == EOF)
    {
        printf("EOF");
    }
}

Put & before every variables you wish to read.

Like:

for (j = 0; j < 3; j++)
{
    int count = fscanf(file1, "%i-%i-%i %i:%i:%i %f ", &day[j], &month[j], &year[j], &hour[j], &minute[j], &second[j], &value[j]);
    printf("%d-%d-%d %d:%d:%d %f\n", day[j], month[j], year[j], hour[j], minute[j], second[j], value[j]);
    // Check for finising early
    if (count == EOF)
    {
        printf("EOF");
    }
}

After question edited:

Ok, the problem is your data file(test.txt) is formated like

20-03-17 08:49:01 28,515

, Not

20-03-17 08:49:01 28.515

So %f will read only 28, not 28.515(since , is not used for floating point numbers). You have two choices: (1) Change 28,515 to 28.515 in your data file (2) Read 28 and 515 seperately. I think the first choice would be good.

Upvotes: 1

Related Questions