0
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>

void load(char* fileName){
   FILE *inputs= fopen(fileName, "r");
   if(inputs){
     printf(":true:");
   }else{
     printf(":error:");
   }
   fclose(inputs); 
}

char* extract(char* name){
    char* input = (char*) malloc(sizeof(char*)*100);
    char* extract = (char*) malloc(sizeof(char*)*100);
    int i=0,j=0,k=0,endFlag=0;
    strcpy(input, name);

    for(i=0;i<strlen(input);i++){
        if(input[i] == '"'){
            if(endFlag==0){
                j =i+1;
                while(input[j]!='"'){
                     extract[k] = input[j];
                     k++;
                     j++;
                }
                endFlag = 1;
            }else{
               endFlag =0;
            }
        }
    }
   extract[k] = '\0';
   return extract;
}

int main(){
  char* file = (char*) malloc(sizeof(char*)*100);
  char* input = (char*) malloc(sizeof(char*)*100);
  strcpy(file, "\"file name\"");
  strcpy(input, extract(file));
  load(input);
  return 0;
}

what I want to do is read file with double quoted string like "\"file name\""

so I extract only file name inside of double quoted string by extract function and

put char* value for call text file by name of it.

But what I received was the segmentation fault, is there any one can fix this trouble?

  • 1
    You don't need to to include quotes around the file name directly in the string, even if it has spaces for file access to work in C. Also, your `malloc` statements should really say, `malloc(sizeof(char)*100);` since you are allocating 100 `char` items, not 100 `char*` (`char` pointer) items. And your `load` function does `fclose` on `input` even if `input` is null. – lurker Mar 29 '14 at 02:50
  • possible duplicate of [What's the best way to check if a file exists in C? (cross platform)](http://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c-cross-platform) – lurker Mar 29 '14 at 02:51

0 Answers0