Unhandled exception in file read

This is a discussion on Unhandled exception in file read within the C Programming forums, part of the General Programming Boards category; Hi I am writing a C program which accesses file. I am using Visual studion 2008. I use fscanf_s to ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    Unhandled exception in file read

    Hi

    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
    Location
    segmentation fault
    Posts
    8,300
    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
    Posts
    7
    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
    Posts
    14,185
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 01: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, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21