Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7192113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:52:22+00:00 2026-05-28T19:52:22+00:00

It’s been a very long time since I coded in C, and I’ve spent

  • 0

It’s been a very long time since I coded in C, and I’ve spent about two hours googling how to properly use fscanf. As far as I can tell, this code is correct, but I’m getting EXC_BAD_ACCESS every time I run it:

int rasterWidth;
int rasterHeight;
FILE* f = fopen("scene.u2d","r");

if (f == NULL)
{
    perror("Can't open file!");
}

char m [2];
fscanf(f, "%s", m);
fscanf(f, "%d %d",&rasterWidth,&rasterHeight); // Getting EXC_BAD_ACCESS here

printf("%s %d %d",m,rasterWidth,rasterHeight);

I’ve verified that XCode is opening the proper file (it loads the first two chars correctly). scene.u2d looks like this:

U2
500 500
-1.0 -1.0 1.0 1.0
g triangle.raw
c 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1
s 0.5 1.0
t 0.3 -0.2

Is there something obvious that I’m doing wrong? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:52:23+00:00Added an answer on May 28, 2026 at 7:52 pm

    First, the man page for scanf says:

    s Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough
    to hold the input sequence and the terminating null character ('\0'), which is added automatically. The input string stops
    at white space or at the maximum field width, whichever occurs first.

    This means you need at least char m[3] for that input file because of the null terminator.

    I would also recommend using %2s as your format specifier so you don’t overflow the buffer.


    If that’s not the problem, have you tried running only the code snippet you posted? It works for me – I suspect there’s some memory mismanagement before the posted code. Or, is your code actually something like:

    int *rasterWidth, *rasterHeight;
    
    fscanf(f, "%d %d",rasterWidth,rasterHeight); // this is definitely a bad access 
    

    Bad accesses are on scanf calls are often caused by passing uninitialised pointers in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.