logo
down
shadow

Segmentation Fault While Reading File in C


Segmentation Fault While Reading File in C

By : Pradeep Kalipatnapu
Date : October 24 2020, 06:10 PM
To fix this issue The segmentation fault might be caused by fopen failing to open the file.
You should always test for such failures and exit with an informative message.
code :
FILE *fp = fopen("students.db", "rb");


Share : facebook icon twitter icon
Segmentation fault in reading a .dat file

Segmentation fault in reading a .dat file


By : Chung
Date : March 29 2020, 07:55 AM
wish helps you You are reading into a buffer you never allocated memory for.
What you're trying now is to read from some junk value in memory, who knows, which almost always leads to a segmentation fault.
code :
char *buffer = malloc(512 * sizeof(char)); // this allocates 512 times the size of a single char of memory
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char **argv)
{  
    FILE *fp ,*fptest;
    long lSize;
    char *buffer;

    //Open file
    fptest = fopen("input.txt", "rb");

    if (fptest == NULL)
    {
        printf("Error occurred when opening file");
        return 1;
    }

    buffer = malloc(sizeof(char) * 512);
    //Read file into buffer
    fread(buffer,1,512,fptest);

    //Parse the boot sector
    char tmpA, tmpB;
    tmpA = buffer[10]; //First byte
    tmpB = buffer[11]; //Second byte

    //Combine the two bytes into one
    char combinedBytes[3];
    strcpy (combinedBytes, &tmpA);
    strcat (combinedBytes, &tmpB);

    //Hex to decimal converter
    long int li1;
    li1 = strtol (combinedBytes,NULL,16);
    printf ("The sector size is: %ld.\n", li1);

    return 0;
}
Asm x86 segmentation fault in reading from file

Asm x86 segmentation fault in reading from file


By : user3810770
Date : March 29 2020, 07:55 AM
Hope that helps The value in R8 at the time your program crashes is the file descriptor returned by the open syscall. Its value is probably 3 which isn't a valid address. You'll need to stores these values in a range of memory you've properly allocated. You can create a buffer in your program's .bss section or dynamically allocate memory with the brk syscall.
Segmentation Fault in reading a file in C

Segmentation Fault in reading a file in C


By : Mazing
Date : March 29 2020, 07:55 AM
I wish this helpful for you I'm trying to read a file which has each line containing two integers delimited by tabs into an array of edges. , You want:
code :
sscanf(line, "%d\t%d", &first, &second);
int n;
...
n = sscanf(line, "%d\t%d", &first, &second);
if (n != 2) { /* do something appropriate here */ }
% pr -t -n xx.c
    1   #include <stdio.h>
    2   
    3   int main ( void ) {
    4           char line[100];
    5           int     first, second;
    6   
    7           (void)fgets(line, sizeof(line), stdin);
    8           (void)sscanf(line, "%d\t%d", first, second);
    9           return 0;
   10   }
   11   
% cc -c xx.c
% cc -c -Wall xx.c
xx.c: In function `main':
xx.c:8: warning: format argument is not a pointer (arg 3)
xx.c:8: warning: format argument is not a pointer (arg 4)
Segmentation Fault: 11 while Reading File

Segmentation Fault: 11 while Reading File


By : Dan Paquin
Date : March 29 2020, 07:55 AM
it fixes the issue You can add ferror() after getc to check for reading error. You need to check that read != EOF also. The problem is that even after checking feof(), you may reach end-of-file with getc(). So, inner loop must contain read != EOF condition.
Also, you have to check for lin and col so as not to assign values to unallocated memory.
code :
 lin = 0;
 while (!feof (f) && lin < my_maze->lins) {
   int read, col = 0;
   do {
        read = getc (f);

        if (ferror(f)) {
            perror("Reading error");
            exit (1);
        }

        if (read != '\n') {
            my_maze->maze[lin][col++] = read;
        }

    } while (read != '\n' && read != EOF && col <= my_maze->cols);
    lin++;
 }
Segmentation Fault when reading File

Segmentation Fault when reading File


By : andrew
Date : March 29 2020, 07:55 AM
To fix this issue fgets() returns NULL only when characters couldn't be read.
What you need to do is check for feof(file). When you finish reading the file the EOF bit is set to 1 and feof() returns true;
Related Posts Related Posts :
  • dynamic memory for 2D char array
  • C compiler producing lightweight executeables
  • Cannot Open Shared Object cygmpfr-1.dll
  • How to access a structure member in a function that get it as void* type?
  • Replace for loop with formula
  • how to find if stack increases upwards or downwards?
  • Why does my homespun sizeof operator need a char* cast?
  • Setting exit status when creating core dump
  • Signal Handling in C
  • How to implement B+ Tree for file systems?
  • Stack storage at function call
  • Manipulate the Clipboard in C on Mac OS X
  • Problem with writing a hexadecimal string
  • Why does this code read all ' ' for the anything after the 4th character?
  • GUI for linux c
  • Where do I put all these function-like #defines, in C?
  • Padding in 24-bits rgb bitmap
  • how to acess and change variable of kernel space from user space
  • Can a conforming C implementation #define NULL to be something wacky
  • send() always interrupted by EPIPE
  • Socket Select with empty fd set
  • Coding self similar fractals in computer graphics using C
  • Forking with Pipes
  • sscanf + c99 not working on some platforms?
  • Problem with C function of type char pointer, can someone explain?
  • Macros in C.... please give the solution
  • Unix Piping using Fork and Dup
  • Any difference between these two while loops?
  • Hashtable implementation for C
  • Problems with Linked List in C
  • Stack Allocation in C
  • Random function stuck on one number
  • printf following zeros to a certain degree
  • Measuring CPU clocks consumed by a process
  • How is a CRC32 checksum calculated?
  • Tips on redefining a register bitfield in C
  • Converting a program from c to bash script
  • Static library not included in resulting LLVM executable
  • What is the meaning of "wild pointer" in C?
  • Reading DWORD from binary file
  • Efficient way to find divisibility
  • Whats the fastest, portable way to hash pointers we know are pointer aligned to a fixed size int?
  • Why does my summation program behaves weird?
  • How to determine values saved on the stack?
  • Thread scheduling C
  • C - converting to 2s complement
  • wireless networks c program
  • From where does the uboot get the information about flash, RTC, RAM in uboot/kernel development
  • printf behaviour in C
  • Measure size and way-order of L1 and L2 caches
  • Efficient copy of entire directory
  • Efficient way to create/unpack large bitfields in C?
  • getnameinfo prototype asks for sockaddr not sockaddr_in?
  • Linked List Sorting with Strings In C
  • Typecasting a floating value or using the math.h floor* functions?
  • Problem implementing sorting algorithm in C with an array of structs
  • How to copy a ram_base file to disk efficiently
  • How to fix casting a pointer to an integer?
  • How to round floating point numbers to the nearest integer in C?
  • PIC C - Sending 200 values over USB, but it only sends 25 or so of them
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk