-1

I write a simple code to read data from texts file. It is showed below:

#include <stdio.h>

int main()
{
    FILE* FP0 ;
    FILE* FP1 ;

    int i, j ;
    int NPOINTS ;

    FP0= fopen("G:\\DATA\\data0.text","r");

    fscanf(FP0,"%d", &NPOINTS); 

    fclose(FP0);

    float COORDINATOFPOINTS[NPOINTS][2] ;

    FP1= fopen("G:\\DATA\\data1.text","r"); 

    for (i = 1; i<NPOINTS+1; ++i) 
    {
        for (j = 1; j<3; ++j)
        {
            fscanf(FP1,"%f", &COORDINATOFPOINTS[i][j]); 
        }
    }
    fclose(FP1);

    printf("%d",NPOINTS); 

    for (i = 1; i<NPOINTS+1; ++i) 
    {
        for (j = 1; j<3; ++j) 
        {
            printf("%f", COORDINATOFPOINTS[i][j]); 
        }
    }
    return 0;
}

But when I run it, it shows:0

Can someone explain why and what is wrong?

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
NULL
  • 5
  • 1
  • 4
  • 1
    For a start read the manual pages for `fopen` and `fscanf`. They return a value that needs to be checked. Some return values denote an error – Ed Heal Feb 12 '21 at 18:39
  • Indent your code properly before posting – klutt Feb 12 '21 at 18:48
  • @EdHeal How this return value leads us to debug the code? – NULL Feb 12 '21 at 18:49
  • 2
    Because the return value tells you if the function call was successful or not – klutt Feb 12 '21 at 18:52
  • 3
    Also, there's no such thing as "C/C++". They are different langugages. – klutt Feb 12 '21 at 18:53
  • 1
    You are accessing your arrays out of bounds. indices start at 0. – Retired Ninja Feb 12 '21 at 18:59
  • @klutt Thanks for your comment. It shows error (returned value= -1) about first read (NPOINTS). I want to read an integer value (like 3) from a file and I don't know why it shows the error. – NULL Feb 12 '21 at 19:02
  • When it comes to arrays, study this https://stackoverflow.com/questions/16892862/why-array-index-start-from-0 – klutt Feb 12 '21 at 19:03
  • Also, learn how to create a [mre] – klutt Feb 12 '21 at 19:04
  • @klutt first "fscanf" showed error ( fscanf(FP0,"%d", &NPOINTS) ). – NULL Feb 12 '21 at 19:08
  • Repeated, even if file Io operations are successful. **You are accessing your arrays out of bounds** . C is zero-based indexing. An array of N elements allows indexes in 0..(N-1) inclusively. Your loops index 1..N. They breach the top end, and in so doing invoke *undefined behavior*. – WhozCraig Feb 12 '21 at 19:34
  • What is the content of your file? And did you add checks for `fopen` as well? – Gerhardh Feb 12 '21 at 19:47
  • @Gerhardh Number 3. No, I have a problem with the format of "FP0" for "printf" command. – NULL Feb 12 '21 at 19:54
  • You don't print `FP0`. As you don't get more output, you don't enter the loop which means your problem is not related to `printf`. Your file content is only `3` on the very first line or are there empty lines before? – Gerhardh Feb 12 '21 at 19:56
  • @Gerhardh I think my problem in "fscanf" command. My first "fscanf" read a value from a text file. In that text file, there is just number 3. You said: "did you add checks for fopen as well?" and I said:"No, I have a problem with the format of "FP0" for "printf" command" – NULL Feb 12 '21 at 20:02
  • Your `scanf` returns -1 which indicates an error. This could be caused by not finding an integer of by feeding a `NULL` pointer into it. Which is why you should check result of `fopen` as mentioned by Ed Heal in the first commnt. Therefore: If you didn't add a check yet, why not? Please do it now. – Gerhardh Feb 12 '21 at 20:32
  • @NULL - It is a good idea to use more meaningful variable names – Ed Heal Feb 12 '21 at 23:57

1 Answers1

0

Despite the simplicity of the answer, since I can not mention, I write it here:

The first problem is about arrays out of bounds due to zero-based indexing of C.

The second problem is referred to the prefix of a text file. There is no ".text" prefix. You must change it to ".txt".

The third problem (as I find your code) is about read data from a text file in line by line approach. You can add "\n" for indicating to the pointer that it must go to the next line.

ALIN
  • 62
  • 1
  • 6
  • 1
    .text is definitely not a prefix since it comes LAST in the file name and nothing is preventing a file from having the extension .text instead of .txt, even if this could potentially be the source for problems – klutt Feb 12 '21 at 21:31
  • Thanks for your answer. It seems your answer is correct but when I print the data, it just prints the last two arrays. What could be wrong? – NULL Feb 12 '21 at 21:47
  • @NULL A lot of things. As I've said, you have not provided a [mre] – klutt Feb 12 '21 at 21:48
  • I agree with @klutt. But I recommend you use the same loop for printing the data as for reading it. Then try to change it and find the logic of the error. – ALIN Feb 12 '21 at 21:53
  • @ALIN Thanks for the recommendation. – NULL Feb 12 '21 at 22:22