0

I'm finishing a project for college in C using freeglut (2.8.1), GLEW 1.10.0, Visual Studio 2012 and Windows 8.1. It's nearly done but for some reason I can't read from console after something has been drawn.

For example, I draw something and then ask the user for a file to read, with new data to draw:

printf("File to read: ");
gets(fname);                 //with scanf the result is the same
printf("%s", fname);         //just for testing

This prints the correct file name to the command line, and it reads the file correctly, however, for some reason, before the new data is displayed the entire thing crashes with the following error:

Unhandled exception at 0x73D3FB6D (freeglut.dll) in CV_TP1.exe: 0xC0000005: Access violation writing location 0x000000D4.

If I remove the gets/scanf and hard-code the file name the program works correctly. The same if I try to read something before all the openGL initializations, right at the top of the main function.

te-x
  • 11
  • 4
  • according to this http://msdn.microsoft.com/en-us/library/cc231199.aspx you dont have access to the file, try running the program as administrator. – Anton D Dec 11 '13 at 21:50
  • It doesn't solve it. I tried both running VS and the .exe as admin and got the same error. – te-x Dec 11 '13 at 22:04
  • hmm , so it reads the file correctly? we would need to see some code to try and guess where your problem is. show us how you read the file. – Anton D Dec 11 '13 at 22:20
  • Yes, that's what I found so weird about this. And reading the file works perfectly, I tested that hardcoding the filename. The problem is reading from the command line, even if I don't use the result. – te-x Dec 11 '13 at 22:24
  • A mistake I often make is declaring char * fname, not char fname[1024]. So obvious that it's actually difficult to spot! Sure you haven't done this? – Hugh Fisher Dec 11 '13 at 23:00
  • No @hugh-fisher, I'm already using the array (char fname[100]). And, as I have said, the string prints correctly and the file is read correctly. It's only when rendering the model that the problems emerge. My colleagues that use the same setup as me are also having the same problem. Linux and OSX are ok and, I'm not sure about this, the ones using CodeBlocks instead of VS are also without problems. – te-x Dec 11 '13 at 23:28
  • Have you considered `fgets (...)` using `stdin`? You can guarantee no buffer overflows this way, as the 2nd parameter is the maximum length of the buffer you have allocated for storing the result. Something along the lines of: `fgets (fname, 100, stdin);` in this case. I am not convinced this is actually the source of your problem, and if that changes nothing then we can put that issue to rest. You need to include more code, in my opinion. – Andon M. Coleman Dec 12 '13 at 00:22
  • fgets also didn't solve. I know it must be hard to diagnose a problem with so little information but I really don't know what code to post. Independently of where I put a scanf/gets/fgets the program will break with the same error. – te-x Dec 12 '13 at 04:05

0 Answers0