Thread: I m geting the error Unhandled exception at 0x75c885ea in h1.exe: 0xC0000005: Access

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    1

    I m geting the error Unhandled exception at 0x75c885ea in h1.exe: 0xC0000005: Access

    Code:
    #include< stdio.h >
    #include<windows.h>
    void main()
    {
      char c[4];
      int cnt, i, len;
      FILE *fd;
      fd = CreateFile("d:\\data13.txt", GENERIC_READ | GENERIC_WRITE,
                      FILE_SHARE_READ, NULL, CREATE_ALWAYS,
                      FILE_ATTRIBUTE_NORMAL, NULL);
      if (fd == NULL) {
        printf("file doesnt exist");
      } else {
    //do
    //{ 
        for (i = 0; i <= 4; i++) {
          c[i] = getchar();
        }
        c[i] = '\0';
    //}while(!EOF);
        WriteFile(fd, c, strlen(c), NULL, NULL);
    //fwrite(c,strlen(c),1,fd);
        fputc('\0', fd);
        fclose(fd);
      }
      c[0] = '\0';
      fd = fopen("d:\\data1.txt", "r");
      if (fd == NULL)
        printf("do exist");
      else {
        fread(c, sizeof(char), 1, fd);
        printf("%5s", c);
      }
      fclose(fd);
     }
    Last edited by Salem; 04-18-2012 at 10:45 AM. Reason: demunged the horrible font abuse - next time, make sure you PASTE AS TEXT

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    FFS: Compile with warnings enabled!

    ..\test.c:3: warning: return type of 'main' is not 'int'
    ..\test.c: In function 'main':
    ..\test.c:6: warning: unused variable 'len'
    ..\test.c:6: warning: unused variable 'cnt'
    ..\test.c:21: warning: array subscript is above array bounds
    ..\test.c:19: warning: array subscript is above array bounds

    If you can't see that you're accessing c[i] when i is both 4 and 5 and c only has elements 0-3, it's because you're NOT compiling with warnings on.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Indent your code if you actually want anyone to read it.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    CreateFile doesn't return NULL on failure, and that's not how you call WriteFile in that case. Reread what MSDN says because it's quite explicit about those things.
    Last edited by adeyblue; 04-18-2012 at 08:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unhandled Exception , access violation reading locations.
    By sokoban_1 in forum C Programming
    Replies: 4
    Last Post: 04-03-2010, 05:27 AM
  2. Replies: 0
    Last Post: 04-03-2010, 02:15 AM
  3. Unhandled exception error in VC++
    By tandirovic in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2005, 05:16 PM
  4. Replies: 5
    Last Post: 07-13-2003, 01:01 PM
  5. unhandled exception error
    By trends in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2002, 06:54 PM

Tags for this Thread