Like Tree1Likes
  • 1 Post By Matticus

Seg Fault with fscanf

This is a discussion on Seg Fault with fscanf within the C Programming forums, part of the General Programming Boards category; I'm sure I'm just making a stupid error but I am getting a seg fault when I fscanf on line ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    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

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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?

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,565
    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.
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fscanf segmentation fault issue (I think)
    By zouru in forum C Programming
    Replies: 5
    Last Post: 04-03-2011, 03:00 PM
  2. fscanf seg fault
    By mercuryfrost in forum C Programming
    Replies: 19
    Last Post: 08-20-2009, 01:22 PM
  3. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  4. fscanf---segmentation fault?
    By breanne in forum C Programming
    Replies: 7
    Last Post: 08-23-2002, 02:20 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21