-
Seg Fault with fscanf
I'm sure I'm just making a stupid error but I am getting a seg fault when I fscanf on line 15. Do you know the reason? Btw I'm not done with the program.
Code:
#include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 void readnames();
7 void sort();
8 void write_names();
9 void free_memory();
10
11 int main(int argc, char *argv[])
12 {
13 int amount;
14 FILE *fp = fopen(argv[1], "r");
15 fscanf(fp, "%d", &amount);
16 char** last = (char **)malloc(amount*sizeof(char*));
17 int i;
18 for (i=0; i<amount; i++)
19 {
20 last[i] = (char*)malloc(15*sizeof(char));
21 }//creating last array
22 char** first = (char **)malloc(amount*sizeof(char*));
23 for (i=0; i<amount; i++)
24 {
25 first[i] = (char*)malloc(15*sizeof(char));
26 }//creating first array
27 char* middle = (char*)malloc(sizeof(char));
28 readnames(amount, argv[1], last, first, middle);
29
30
31
32 return 0;
33 }//main
-
Are you sure that fp is not null?
Are you sure that the argument from the command line is there?
Are you sure that the file you specify exists?
-
Some posting tips:
- Make sure you don't paste the line numbers - the forum includes those automatically
- If you're reading from a file, post the contents of the file so we can see what you're trying to read
- Post a cut-down version of your program that we can compile, and that demonstrates the problem. Not only will it help others review your code, but you might figure it out yourself in the process of preparing it for posting.