-1

If you could help me, that would be much appreciated. This code is about my thesis. I'm trying to write to a file and it works but when I try to read another file it reads incorrectly. For example, the file I want to read has 5 in it, but it reads as 1. I tried to write different numbers into the file but it is constantly being read 1. Another problem is that I have to read more than one group of data in this file. That is, it will read different variables. For example, I have a data file, it will read the distance matrix first, it will read the cost matrix when it is finished. Then it will read demand array. I mean, I have to read something new on the new line. How can I do? Help me please.

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

 int main()
 {
     FILE* myfile;
     errno_t errorcode = fopen_s(&myfile, "output.txt", "w");

     if (myfile == NULL)
     {
        printf("Error");
     }

     FILE* data; 
     errno_t err = fopen_s(&data, "C:\\SA\\input.txt", "r");


     if(data==NULL)
     {
        printf("file does not open");
     }
     int s;
     while (!feof(data))
     {
        s = fscanf_s(data, "%d", &s);
        printf("s:%d", s);
     }
     float dist[15][15], y1, y2, x1, x2;
     dist[15][15] = sqrt(pow((y2-y1),2)+pow((x2-x1),2));

 return 0;
 getchar();
 }
Ece Uyar
  • 39
  • 4
  • 4
    C and C++ are two different languages. `` is not C, code also doesnt look like C++, and you say it is C, what is it? – 463035818_is_not_an_ai Mar 29 '21 at 11:42
  • 2
    BTW: If you're having problems, take a break from your current code and try to reproduce and solve the issue in a [mcve], it will help you help yourself. As a new user here, please also take the [tour] and read [ask]. – Ulrich Eckhardt Mar 29 '21 at 11:50
  • 1
    Visual Studio 2019 comes with a builtin, very easy to use, world class debugger. You should consider having a look in to it. Invest an hour or so learning how to use it. It will save you countless hours. – Jabberwocky Mar 29 '21 at 12:03
  • Also [edit] show a minimal example of the input.txt file. – Jabberwocky Mar 29 '21 at 12:04
  • Hmm, when I compile this with Visual Studio 2019 I get 4 `uninitialized local variable used` errors. – Jabberwocky Mar 29 '21 at 12:06
  • FYI, `(x * x)` is more efficient than `pow(x, 2)`. Also, `pow` is floating point. Squaring a number is both integer and floating point. – Thomas Matthews Mar 29 '21 at 16:31

1 Answers1

2

Regarding this part:

     while (!feof(data))
     {
        s = fscanf_s(data, "%d", &s);
        printf("s:%d", s);
     }
  • Generally while (!feof(data)) is considered as a bad practice. You should check if readings are successful after reading and before using what is read.
  • You assigned the return value of fscanf_s to the variable where the integer read is stored. This will prevent the program from using what is read. The return value of fscanf_s should be stored into different variable for directly checked.

It will be like this:

     while (fscanf_s(data, "%d", &s) == 1)
     {
        printf("s:%d", s);
     }

Regarding this part:

     float dist[15][15], y1, y2, x1, x2;
     dist[15][15] = sqrt(pow((y2-y1),2)+pow((x2-x1),2));
  • The variables y1, y2, x1, and x2 are used without being initialized. They have indeterminate values and using the values will invoke undefined behavior.
  • You are assigning to dist[15][15], but this is out-of-range. The number in array declaration in C are the number of elements, not the maximum index. Another undefined behavior is invoked.

This part looks completely nonsense and should be removed.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • First of all, thank you for help. I try to this part; while (fscanf_s(data, "%d", &s) == 1) { printf("s:%d", s); } but the same thing is happening, and it is constantly being written "1" on the screen. (although it doesn't write "1" in the file) – Ece Uyar Mar 29 '21 at 18:34