0

I work on Xcode and I have a simple function that opens a file using open in C.

 void mfs_workwith() {
  char *token, *temp_token;
  char *search = ".";
  temp_token = (char*)malloc(sizeof(char)*strlen(cc[1]));
  strcpy(temp_token, cc[1]);
  if ((token = strtok(temp_token, search)) == NULL) {
    printf("mfs_workwith command is only used with mfs type files e.g. example.mfs \n");
  } else if ((token = strtok(NULL, " \n\0")) == NULL) {
    printf("mfs_workwith command is only used with mfs type files e.g. example.mfs \n");
  } else if (strcmp(token, "mfs") == 0) {
    filename = malloc(sizeof(char)*strlen(cc[1]));
    strcpy(filename, cc[1]);
    if ((file_mfs = open(filename, O_RDWR)) == -1) {
      perror("open error");
    } else {
      printf("open successful \n");
    }
  }
}

The name of the file is stored in a global array and then copied into local buffers in order to tokenize and check if it has the right format (.mfs).

Then if everything is ok I make a fresh copy of the name of the file and call open with it.

My problem is that when I run my program in terminal it runs fine, prints open successful and then continues. But when I try to run it in Xcode it fails with this error:

No such file or directory

I am giving the input file.mfs which is the name of a file in the same directory.

Am I missing something obvious?

fenceop
  • 1,439
  • 3
  • 18
  • 29
Byte_Monster
  • 313
  • 3
  • 14
  • 1
    Is it the path of the file you are trying to open? have you tried to give an absolute path? and also [don't cast the return value from `malloc()`](http://stackoverflow.com/a/605858/1983495) – Iharob Al Asimi Feb 18 '15 at 13:55
  • 1
    Thanks a lot for the answer. I did try the absolute path and it works. So I guess my real problem is to open a relative path via xcode. I found this: http://stackoverflow.com/questions/4796943/open-method-opens-files-with-full-path-only-c wich answers my question. Thanks again – Byte_Monster Feb 19 '15 at 15:39

2 Answers2

1

I found the problem thanks to iharob's comment. It seems xcode has a hard time opening relative paths since it uses a different file while running the program. There is a relative discussion here:

Open method opens files with full path only C++

thanks again everyone.

Community
  • 1
  • 1
Byte_Monster
  • 313
  • 3
  • 14
0

This:

filename = malloc(sizeof(char)*strlen(cc[1]));
strcpy(filename, cc[1]);

is broken, it fails to allocate room for the string's terminator, so it causes buffer overflow and undefined behavior.

Also, you never need to scale by sizeof (char), that's always 1. It should be:

filename = malloc(strlen(cc[1]) + 1);
strpcy(filename, cc[1]);

or, if you have it, just:

filename = strdup(cc[1]);
unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    Thanks a lot for your answer but this does not seem to be my problem. I added the code: http://pastebin.com/CjZMVM26 in order to make sure there is a \0 at the end of my string but still terminal runs the program fine but xcode does not. – Byte_Monster Feb 19 '15 at 14:33