Thread: Reading file in but receive segmentation fault

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    7

    Reading file in but receive segmentation fault

    I'm sorry if I violate any rule since I am new here, but I do have a question regarding some segmentation fault I receive while running the code I have been working on.
    I am working on a card program and this is my input:
    Code:
    TS QC 8S 8D QH 2D 3H KH 9H 2H TH KS KC
    9D JH 7H JD 2S QS TD 2C 4H 5H AD 4D 5D
    6D 4S 9S 5S 7S JS 8H 3D 8C 3S 4C 6S 9C
    AS 7C AH 6H KD JC 7D AC 5C TC QD 6C 3C
    3C 6C QD TC 5C AC 7D JC KD 6H AH 7C AS
    9C 6S 4C 3S 8C 3D 8H JS 7S 5S 9S 4S 6D
    5D 4D AD 5H 4H 2C TD QS 2S JD 7H JH 9D
    KC KS TH 2H 9H KH 3H 2D QH 8D 8S QC TS
    What I am trying to do is read in 52 cards name into array, set up the game and play it, when the game is done, I read the next 52 cards and play it again.

    However, I encounter segmentation fault while running the program. Here is my code
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include "header.h"
    
    
    extern FILE *open_file();
    
    
    //function prototype                                                                  
    void readData(FILE *input_file);
    
    
    //main                                                                                
    main(int argc, char *argv[]){
      FILE *input_file;
      char file_name[32];
    
    
      //command to run the lab and data: lab1 data                                        
      input_file = fopen(argv[1],"r");
      readData(input_file);
    
    
    
    
      fclose(input_file);
    
    
    }
    //other functions here                                                                
    //read in the data                                                                    
    void readData(FILE *input_file){
      char card_list[104];
      FILE *current_fpt;
      current_fpt = input_file;
      //check if file error or not                                                        
      if(input_file == NULL){
        printf("Error opening input file");
      }else{
        do{
          int i;
          for(i=0;i<52;i++){
            fscanf(current_fpt,"%s",&card_list[i]);
            printf("%s\n",card_list[i]);
          }
          input_file = current_fpt;
        }while(!feof(input_file));
      }
    }
    I appreciate your help and please tell me where I did it wrong and if there are other ways to do this.
    Last edited by RozenKristal; 09-28-2013 at 11:52 PM.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
      }else{
        do{
          int i;
          for(i=0;i<52;i++){
            fscanf(current_fpt,"%s",&card_list[i]);
            printf("%s\n",card_list[i]);
          }
    You are trying to read a string into a single character space. You probably want just "card_list" as your argument but really using fscanf() without a width specifier and such a small 104 character buffer you are definitely going to run into buffer overflows as well.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > FILE *current_fpt;
    > current_fpt = input_file;
    You're not achieving anything by reassigning the file pointer to other variables...
    > input_file = current_fpt;
    and then assigning it back again.

    > fscanf(current_fpt,"%s",&card_list[i]);
    > printf("%s\n",card_list[i]);
    > You are trying to read a string into a single character space.
    It's actually reading into the middle of a char array - but that's not the problem.
    It's the printf which follows that is expecting a pointer, and is only getting a single char.


    > while(!feof(input_file));
    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    7
    Quote Originally Posted by nonpuz View Post
    Code:
      }else{
        do{
          int i;
          for(i=0;i<52;i++){
            fscanf(current_fpt,"%s",&card_list[i]);
            printf("%s\n",card_list[i]);
          }
    You are trying to read a string into a single character space. You probably want just "card_list" as your argument but really using fscanf() without a width specifier and such a small 104 character buffer you are definitely going to run into buffer overflows as well.
    My background is Java, so I am really inexperience in dealing with this in C. In java, I can make an array and read in String. What I am trying to do here is read in the input into an array and store it there so I can set up the struct later. Is there something else I can do to resolve this?


    > FILE *current_fpt;
    > current_fpt = input_file;
    You're not achieving anything by reassigning the file pointer to other variables...
    > input_file = current_fpt;
    and then assigning it back again.

    > fscanf(current_fpt,"%s",&card_list[i]);
    > printf("%s\n",card_list[i]);
    > You are trying to read a string into a single character space.
    It's actually reading into the middle of a char array - but that's not the problem.
    It's the printf which follows that is expecting a pointer, and is only getting a single char
    Thank you. I thought that was the way to pass the current location of the file pointer in the stream, but apparently I am wrong....
    Last edited by RozenKristal; 09-29-2013 at 07:11 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from file, segmentation fault
    By atac in forum C Programming
    Replies: 2
    Last Post: 05-08-2013, 10:34 PM
  2. Reading from stdin and printing (Segmentation Fault)
    By auxfire in forum C Programming
    Replies: 3
    Last Post: 03-08-2013, 03:33 AM
  3. Segmentation fault reading and parsing data from a text file
    By deathseeker25 in forum C Programming
    Replies: 4
    Last Post: 05-19-2012, 12:33 PM
  4. Segmentation fault when reading very large text files
    By sapogo06 in forum C Programming
    Replies: 8
    Last Post: 12-07-2009, 03:19 PM
  5. Segmentation fault reading integers into an array
    By bolivartech in forum C Programming
    Replies: 4
    Last Post: 10-23-2009, 08:06 PM