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

  • SEARCH
  • Home
  • 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 7745955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:09:15+00:00 2026-06-01T10:09:15+00:00

I am using fgets() to read lines from file. I am able to read

  • 0

I am using fgets() to read lines from file. I am able to read a few lines of the file then fgets() returns an access violation. One would expect that there is an issue with my file buffer but as you can see in my code, that is not the case. One bit of strange behavior I noticed was if I read and print all the lines of the file in a tight loop, I have no issues. I used some printf() statements to debug this problem and noticed that the file position is different depending on which loop is executed. The FILE * is not touched in my “full loop” logic.

The tight loop file positions go: 0, 27, 53, 80, 82, 99, 127, 155, etc.

The full loop file positions go: 0, 27, 53, 80, 82, 99, 138

input file:

!!!!!!!!!!!!!!!!!!!!!!!!!
! Test sparc gagdet file
!!!!!!!!!!!!!!!!!!!!!!!!!

! instruction 1
1: subcc     %g0, %i4, %i4
1: subc      %g0, %i4, %i4 ** access violation reading this line **

! instruction 2
** etc. **

code:

/*
* parse_profile: Parse the gadget profile and load the memory structures required to scan the library file
*/
int parse_profile(FILE * gadget_file, struct g_handle * gadget_handle){

// Buffers used to temporarily store file imput
char op_code [NODE_BUF_SIZE] = "\0";
char reg [NODE_BUF_SIZE] = "\0";

// Reference nodes in the bod_ops and save_regs lists
struct char_node * temp_node = NULL;
struct char_node * op_node = NULL;
struct char_node * reg_node = NULL;

// 
int level = 1;
int old_level = 1;
int curr_line = 0;

// A buffer to hold file data
char file_buffer [PAGE_SIZE];

// Reference nocdes in the instruction tree
struct instruction_node * current_node = NULL;
struct instruction_node * prev_node = NULL;
struct instruction_node * prev_level = NULL;

// Read a line from the gadget file (data for a single instruction)
//while(fgets(file_buffer, PAGE_SIZE, gadget_file) != NULL){
char * shiz = file_buffer;
while(shiz != NULL){
printf("\n file location: %d", ftell(gadget_file));
fflush(stdout);
shiz = fgets(file_buffer, PAGE_SIZE, gadget_file);
/*
// tight loop with different file position
while(shit != NULL){
printf("\n file location: %d", ftell(gadget_file));
fflush(stdout); 
shiz = fgets(file_buffer, PAGE_SIZE, gadget_file);
}
*/
    // Increment the current line
    curr_line = curr_line + 1;
printf("\nline (%d)", curr_line);
fflush(stdout);     
    // Ensure we have gathered the entire line of the file
    if(strlen(file_buffer) >= PAGE_SIZE){

        // We have exceeded the maximum line size,  quit
        printf("\nError reading gadget profile, Line %d: Maximum line length of 4096 has been exceeded", curr_line);
        return(-1);

    } // Ensure we have gathered the entire line of the file

    // If this is a comment
    if(*file_buffer == '!'){

        // Do nothing
    }
    // If this is a blank line
    else if(sscanf(file_buffer," %s ") < 1){

        // Do nothing
    }
    // Scan the current line until we have saved all instructions
    else if(sscanf(file_buffer,"%d: %s", &level, op_code) == 2){
printf("\n file location: %d", ftell(gadget_file));
fflush(stdout);
printf("1");
fflush(stdout);     
        // Store instruction information
/*      
commented block
*/
    } // Scan the current line until we have saved all instruction/nibble pairs

    // Scan the current line until we have saved all  registers to be preserved
    else if(sscanf (file_buffer,"r: %s", reg) == 1){
/*
commented block
*/          
    } // Scan the current line until we have saved all  registers to be preserved

    // Scan the current line until we have saved all  op_codes to be avoided
    else if(sscanf (file_buffer,"o: %s", op_code) == 1){
/*
commented block
*/

    } // Scan the current line until we have saved all  op_codes to be avoided

    else{

        // quit
        printf("\nError reading gadget profile, Line %d: \n%s", curr_line, file_buffer);
        return(-1); 
    }
printf("7");
printf("\n file location: %d", ftell(gadget_file));
fflush(stdout);     
} // Read a line from the gadget file (data for a single instruction) 
printf("a");
fflush(stdout);     
// If fread() returned an error, exit with an error
if(ferror(gadget_file) != 0){

    // Print error and exit
    printf("\nError reading gadget profile");
    return(-1);

} // If fread() returned an error, exit with an error   

return 0;
}
  • 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-06-01T10:09:16+00:00Added an answer on June 1, 2026 at 10:09 am

    You have undefined results on the line

    else if(sscanf(file_buffer," %s ") < 1){
    

    The number of format specifiers exceeds the number of pointers passed. It is quite possible that the sscanf tries to store the scan result at the arbitrary location whose bit-pattern was in the wrong place.

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

Sidebar

Related Questions

I am trying to read from a file using fgets and sscanf. In my
I'm using the fgets function to read a line from a file. I don't
I'm writing an script that is able to read from stdin and then request
I'm trying to read lines from a SIC/XE program using a C program that
I'm using fgets with stdin to read in some data, with the max length
Using a restful resource in Rails, I would like to be able to insert
I'd like to read line from file with fstream (I used this before with
I am using vc2010 and I am trying to read contents of a file
I wanted to read input from user (multiple lines) and write it into a
I wanted to compare reading lines of string input from stdin using Python 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.