0

Here's my code

#include <stdio.h>
#include <stdlib.h>
int main ()
{
    int x = 0;
    int y = 0;
    float a[5][2];      //array
    float b[3][2];      //array
    float c[2][2];      //array
    FILE *fr;
    //int c;
    float power;
    char unit[5];
    //int N;        //Number of sensors
    float TI;       //Time interval
    //char M;       //Midpoint
    //char T;       //Trapezoid
    //int SR;       //Sample Rate
    fr = fopen("sensor_0.txt","r");
    /*fr = fopen("sensor_1.txt","r");
    fr = fopen("sensor_2.txt","r");
*/
//----------------------------------------------------------------------------------------------------------------------------
 printf("The contents of %s file are :\n", "sensor_0.txt");
 while ( !feof( fr ) )
 {


fscanf(fr, "%f %f %s",&TI, &power, unit);

//printf("%f, %f \n", TI,power);        //print
a[x][y] = TI;
a[x][++y]= power;
x++;
y = 0;

 }
  fclose(fr);
//----------------------------------------------------------------------------------------------------------------------------

  printf("%s", "hello");

    return 0;
}

Why isn't my string printing out anything after the while loop? If I uncomment the same line inside the while loop, it prints properly. I've also tried just adding simple printf("hello") yet nothing seems to work after the while loop.

Edit - minor formatting.

output should just be
700 25.18752608 mW
710 26.83002734 mW
720 26.85955414 mW
730 23.63045233 mW
j.yang29
  • 65
  • 9
  • 1
    show the content of `sensor_0.txt` – artm Dec 03 '15 at 01:11
  • 1
    instead of `while ( !feof( fr ) )` that is incorrect, use `while (fscanf(fr, "%f %f %4s",&TI, &power, unit) == 3)` – chqrlie Dec 03 '15 at 01:13
  • Does your program exit or does it hang? Also, you might want to change the printf to printf("%s\n", "hello"); – bruceg Dec 03 '15 at 01:15
  • 1
    potential buffer overflow on `unit`. This buffer can only hold 4 bytes and a `'\0'`, are you sure the file contents is consistent with that? You should avoid buffer overflow with a `%4s` format. – chqrlie Dec 03 '15 at 01:15
  • You should stop parsing when `x` becomes `>= 5`. – chqrlie Dec 03 '15 at 01:16
  • chqrlie: why 3? Bruceg: What do you mean by exit? I'm not actually done, I'm just trying to verify that my information was stored correctly before continuing – j.yang29 Dec 03 '15 at 01:17
  • You show the output, not the input. – chqrlie Dec 03 '15 at 01:17
  • The output and input are the same, I'm just trying to take data from my file, and then put that data in an array. and then just output that array so i can verify my data – j.yang29 Dec 03 '15 at 01:18
  • 1
    `fscanf` returns the number of variable parsed, A correct line of input should have 3, anything else should fail. – chqrlie Dec 03 '15 at 01:19
  • Yes the files are all similar to the one i mentioned, int, double/float, then char (mw) – j.yang29 Dec 03 '15 at 01:19
  • So why exactly don't any of my printf work properly after the while loop? your fix did print out the next printf statement i added – j.yang29 Dec 03 '15 at 01:22
  • @vvid "What do you mean by exit"? It means does your program actually finish running or is it hanging somewhere (in the `while` loop most probably)? – kaylum Dec 03 '15 at 01:30
  • The while loop ends when it reaches the end of the text file. What I was trying to do was to take the data. see the data. put the data in an element of the array. once it reaches the end, close the file – j.yang29 Dec 03 '15 at 01:31
  • @vvid I mean if you run the program does it finish and get you back to the command prompt? Maybe it has crashed, especially if you overflow the unit buffer. Have you tried running it in gdb where the crash would be more obviuos to see? – bruceg Dec 03 '15 at 01:33
  • 1
    [Why is “while ( !feof (file) )” always wrong](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – M.M Dec 03 '15 at 02:47
  • for readability please indent consistently. I.E. after every opening brace '{' and un-indent before every closing brace '}' – user3629249 Dec 03 '15 at 09:55
  • there are a number of problems in the code that can be caught by the compiler. When compiling always enable all the warnings (for gcc, at a minimum use: `-Wall -Wextra -pedantic` ). Then fix those warnings – user3629249 Dec 03 '15 at 09:57

2 Answers2

2

I suspect the file has 5 lines, not 4.

Your test of !feof() fails because you have not hit the end of file yet when you try to read the 6th line. fscanf fails but you do not test the return value. So you store TI and power beyond the end of the 2D array, invoking undefined behavior.

Changing the loading code this way should fix the problem:

while (x < 5 && fscanf(fr, "%f %f %4s", &TI, &power, unit) == 3) {
    a[x][0] = TI;
    a[x][1] = power;
    x++;
}
if (x != 5) {
    printf("incomplete input\n");
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • The text from the data file shouldn't make a big difference should it? the actual text was 700 26.25889475 mW 705 26.62316775 mW 710 23.50794727 mW 715 26.65350342 mW 720 25.52943698 mW 725 23.39016162 mW 730 24.11399288 mW – j.yang29 Dec 03 '15 at 01:50
  • Since you did not check for overflow, the number of entries in the file matters a lot! With your incorrect test using `feof()`, you would invoke undefined behaviour if the file had more than 4 lines. This is the explanation for the observed behavior. – chqrlie Dec 03 '15 at 09:42
  • @vvid, please post the input file contents as an edit to the question, not as part of a comment for some answer. – user3629249 Dec 03 '15 at 09:51
  • @vvid, your comment indicates the input file has 7 lines. so your code must check for array overflow. – user3629249 Dec 03 '15 at 09:59
0

Doing what chqrlie suggested worked.
"instead of while ( !feof( fr ) ) that is incorrect, use while (fscanf(fr, "%f %f %4s",&TI, &power, unit) == 3)"

j.yang29
  • 65
  • 9
  • Your answer is not correct as it is not checking the current index into the data array, so can overrun the bounds of the array. – user3629249 Dec 03 '15 at 09:52