Thread: Unhandled exception in file read

  1. #1
    Registered User
    Join Date
    Mar 2010

    Unhandled exception in file read


    I am writing a C program which accesses file. I am using Visual studion 2008. I use fscanf_s to read the file. The file reading happens properly until one step where i get an error which says

    Unhandled exception at 0x778d7dd2 in aca.exe: 0xC0000005: Access violation writing location 0x00000028.

    I tried step by step debugger and found that the error was in

    fscanf_s(f1,"%x %d %d %d %d\n",&address,&function_unit,&dest,&src1,&src2);

    It works till a point from where i get the above exception. The code works fine in linux. But I need VS2008 to execute it. Please help.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    Show the declarations for those five variables.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2010
    int address = 0;
    int function_unit =0;
    int destination =0;
    int src1=0;
    int src2=0;

    The file is being read for few lines after which i get this exception

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Interesting. If the file doesn't end in a blank line, I would expect a scanf fail but not an exception. Does this happen the first time at the file, or do you get some data? Is dest the same as destination? Why isn't address a pointer variable (which is not the same thing as int)?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Was the line before it a %s conversion?

    Perhaps you overran the buffer, and it trashed the value of f1.

    So the next call, it's bye bye code and hello exception.

    Post more code than a single line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read (BIG) File into char array
    By doia in forum C Programming
    Replies: 2
    Last Post: 03-19-2010, 05:26 PM
  2. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 02:01 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM