0

I have 950 files and each file has 20k lines and 4 columns of data points. And I want to read all column 0 and column 2 data points and store them into the array for further use.

I've browsed through: Going through a text file line by line in C, Reading text file and storing columns in an array, Reading multiple text files in C, and How to read all files in a folder using C to understand how to read line by line with certain columnn and how to "scan" multiple files.

And I combined them to suit what I desire, the following code is to test to see whether my idea can work or not, so I just put 2 files into the directory:

#include <stdio.h>
#include <dirent.h>



int main ()
{
    float w,x,y,z;

    const char *dir = "C:\\Users\\ONG\\Desktop\\new";
    struct dirent * entry;
    DIR *d = opendir(dir);

    FILE *fp;

    if (d == 0)
    {
        perror("opendir");
        return 0;
    }

    while ((entry = readdir(d)) != 0)
    {
        if((!strcmp(entry->d_name, ".")) || (!strcmp(entry->d_name, "..")))
        {
            continue;
        }

        printf("%s\n", entry->d_name);
        fp = fopen(entry->d_name, "r");
        fscanf(fp, "%f %f %f %f", &w, &x, &y, &z);
        printf("%f %f\n", w, y);
        fclose(fp);
    }
    closedir(d);

    return 0;
}

To see the code work or not, I put 2 files into that directory. And the code is expected to print the file name followed by 2 data points (for example -0.049 0.576), twice.

But what I get is:

2nd_testdata_point0.txt
818583905043582480000000000000000.000000 -1.#QNAN0
2nd_testdata_point1.txt
818583905043582480000000000000000.000000 -1.#QNAN0

What is the problem?

I have tried to use the exact code from the answer from How to read all files in a folder using C, and the error is the same as stated by the author. But I can't see how the suggestion in the comments solve the problem.

Community
  • 1
  • 1
  • 2
    The problem is that you aren't reasoning about your task and writing code that suits your requirements; you're randomly copy/pasting code from the internet without understanding it first, hoping you'll stumble onto a solution that works. – Robert Harvey Apr 13 '20 at 15:06
  • You need to decompose the problem into subproblems (that's basically how all non trivial programming works): 1: list all the files in the directory. 2: read all lines of a file. Once this works, combine 1 and 2. Once this works worry about storing the values in an array. – Jabberwocky Apr 13 '20 at 15:14
  • 1
    And check that `fopen` succeeded and that `fscanf` read 4 values. – Paul Ogilvie Apr 13 '20 at 15:32
  • @Jabberwocky Hi, yes I have tried them independently. And they work: one can list all the files, and another one the fscanf does read the 4 values. – johnny2231 Apr 13 '20 at 15:46
  • @johnny2231 decomposing also means decomposing into functions. And you want to read all values in the file don't you? You only read the values in the first line. – Jabberwocky Apr 13 '20 at 15:58
  • @Jabberwocky Hi Yes I want to read all the values, not just the first line. The code above just to test if it can print the correct values. After a bit more study, I manage to print all data from each file using `sprintf``, which I learnt from [link](https://www.daniweb.com/programming/software-development/threads/194259/read-multiple-files-in-c). it works well. – johnny2231 Apr 14 '20 at 00:11

0 Answers0