-1

I am very new in C, I am currently just trying to read a file with the contents "6" and nothing else. Whenever I run the file, I get: Bus Error:10.

#include <stdio.h>
#include <stdlib.h>
char input(void);

int main(int argc, char** argv)
{


    input();

    return (EXIT_SUCCESS);
}

char input(void)
{
    FILE *fp;
    char *score;

    fp = fopen("data.bin", "rt");


    fscanf(fp,"%s", score);

    printf("%s", score);

    fclose(fp);

}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
cpep
  • 27
  • 1

1 Answers1

2

I modified your code like this:

#include <stdio.h>
#include <stdlib.h>

void input(void);

int main(int argc, char** argv) {
    input();
    return(EXIT_SUCCESS);
}

void input(void) {
    char buffer[10];
    FILE *ptr;
    ptr = fopen("data.bin","rb");  // r for read, b for binary
    fread(buffer, sizeof(buffer), 1, ptr); // read 10 bytes to our buffer
    printf("%s", buffer);

    fclose(ptr);
}

Output:

6

and for more, read this: Read/Write to binary files in C.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305