a once running executable is now not running

I am trying to read the first five lines of a CSV file and write to the screen. The following code was written last week and it compiled and ran correctly. This code is to write the entire file to the screen (I just wanted to make sure that the entire file was read correctly, and then I'll change the code so that only the first 5 values are written). The CSV file has 4 columns (I've assigned them a, b, c, and d), and 54444 entries . The CSV file looks like this
SNP # BTA ID CONTIG
51032 1 135098 Chr1
Why is it now, that when I run the program, either i get a,b, and c printed as zeros and d printed as <NULL>, or the executable file doesn't run at all? I'm stumpted. I assume that I had an error in the code and just got lucky last week.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
#include <vector>
#include <stdio.h>
using namespace std;

struct record
{
   int  a, b, c;
   char d;
};

int main(void)
{
   const char filename[] = "C:\\Users\\rhcr56\\Documents\\C++\\ANG_snp_list_120109.csv";
   FILE *file = fopen(filename, "r");
   if ( file != NULL )
   {
      char line [ 80 ];
      struct record record [ 54444 ];
      size_t count, i = 0;
      while ( i < sizeof record/ sizeof *record )
      {
         if ( fgets(line, sizeof line, file) == NULL )
         {
            break;
         }
         if ( sscanf(line, "%d,%d,%d,%s", &record[i].a, &record[i].b,
                     &record[i].c, &record[i].d) == 4 )
         {
            ++i;
         }
      }
      fclose(file);
      for ( count = i, i = 0; i < count; ++i )
      {
         printf("record[%lu]: a = %d, b = %d, c = %d, d = %s\n", 
                (long unsigned)i, record[i].a, record[i].b, record[i].c, 
                record[i].d);
      }
   }
   else
   {
      perror(filename);
   }
   system("PAUSE");
   return 0;
}
struct record record [ 54444 ];
There wasn't a worse name?

The problem is that you're trying to read a string into a single char.
I don't understand why you are using c++ style headers but c functionality. You aren't using anything from the fstream, vector, or iostream headers as far as I can tell. You are correct in your assumption that there is undefined behavior in the program. Therefore the results will always be unpredictable or "undefined". It might seem to work one day and then fail the next due to the memory buffer overflow that helios pointed out. Use C++ functionality and you'll end up with a much more secure application.
Topic archived. No new replies allowed.