Fgets works then returns access violation?

Up vote 0 down vote favorite share g+ share fb share tw.

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 ").

2 See if you can narrow down your code to the essentials. – gspr Apr 5 at 15:16 Check the return value from fgets before using file_buffer; terminate file_buffer with '\0' before using string functions on it (strlen etc); tidy up the code to make it readable (eg delete many redundant comments, such as those on terminating }). Then try asking again :-) – William Morris Apr 5 at 15:30.

You have undefined results on the line else if(sscanf(file_buffer," %s ").

That fixed the problem. Thank you! – randisp Apr 5 at 16:19.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions