-2

I have created a file called mahmoud.txt in the direceotry: /Users/mahmoudhamra/Desktop/C language/

I want to open it in Xcode.

I created the directory and the name of the file into a string each. Then I concatenated the file name to the directory and tried to open it to read it but it always gives me an error: "Thread 1: signal SIGBART".

Can someone help me please? Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



int main(int argc, const char * argv[]) {


    FILE *inFile;
    char fileName[13];

    printf("enter file name: ");
    scanf("%s",fileName);

    char new[40]="/Users/mahmoudhamra/Desktop/C language/";
    strcat(new, fileName);

    inFile=fopen("new", "r");

    if (inFile== NULL) {
        printf("file %s was not opened!\n", fileName);
        printf("check that the file exists!\n");
        exit(1);


    }
    else
        printf("the files has successfully been opened!\n");




    return 0;
}
LPs
  • 16,045
  • 8
  • 30
  • 61
RedDoumham
  • 51
  • 7
  • 1
    "I want to open it in Xcode". Are you *sure* that's what you want to do? Where is the stacktrace which will help us determine where the problem is? – trojanfoe May 25 '16 at 06:36

2 Answers2

3

First of all this

char new[40]="/Users/mahmoudhamra/Desktop/C language/";

should be at least

char new[41]="/Users/mahmoudhamra/Desktop/C language/";

to leave space for null terminator. A C-string is an array of chars with a null-terminator (0x00, '\0', 0) as last char.

Best would be:

char new[]="/Users/mahmoudhamra/Desktop/C language/";

BTW your problem is that you have no space to add filename chars, so at least you should define it as

char path_and_file[128] = {0};
strncpy(path_and_file, "/Users/mahmoudhamra/Desktop/C language/", sizeof(path_and_file)-1);

If you want to learn something about dynamic allocation you can:

char *directory = "/Users/mahmoudhamra/Desktop/C language/";
char *path_and_file = malloc(strlen(directory)+1);
if (path_and_file != NULL)
{
   strcpy(path_and_file, directory);

   printf("enter file name: ");
   scanf("%s",fileName);

   path_and_file = realloc(path_and_file,strlen(directory)+strlen(filename)+1);
   if (path_and_file != NULL)
   {
      strcat(path_and_file, filename);

      // YOUR STUFF

   }
}


free(path_and_file);

Another way with dynamic allocation is using strdup to create your first string:

char *path_and_file = strdup("/Users/mahmoudhamra/Desktop/C language/");

EDIT

Last thing, as @visibleman pointed out, the call to fopen have to be changed to

inFile=fopen(new, "r");

or according to my examples:

inFile=fopen(path_and_file, "r");
LPs
  • 16,045
  • 8
  • 30
  • 61
  • 1
    This is a good answer. Additionally, I think the OP will want to remove the quotes around "new" in the fopen call. – visibleman May 25 '16 at 06:44
  • That `strncpy()` call is broken as the copy length is the size of the destination buffer. That cannot be right. – trojanfoe May 25 '16 at 06:59
  • It's still broken. What happens when the copy accesses memory in the source buffer that is beyond the size of the source buffer? Undefined I would say. Also WRT `malloc()` and `strcpy()`; have you heard of `strdup()`? – trojanfoe May 25 '16 at 07:05
  • @trojanfoe 1st) I'm giving a solution for OP, not a solution for whatever. 128 chars are more than enough. 2nd) As I wrote: _If you want to learn something about dynamic allocation_. But, well, I add an example with `strdup`. – LPs May 25 '16 at 07:11
  • But the solution is unsound. I am not convinced you understand the issue with `strncpy()`. – trojanfoe May 25 '16 at 07:12
  • @trojanfoe Probably I need some coffee. Buffer is zeroed. So `strncpy` copy at most `sizeof(path_and_file)-1` bytes of literal C-string. So the last byte will be left unchanged: '\0'. After that, code has to take care about what happened with the `strncpy`: if it fails for length or there is no more space to add `filename` program must be killed... Am I wrong? – LPs May 25 '16 at 07:23
  • 1
    No actually you are right; apologies. That is the correct use of `strncpy()`. – trojanfoe May 25 '16 at 07:24
1

The issue is almost certainly the size of the new character array as it does not have the capacity to hold the complete filename and will cause a stack overflow:

char new[40]="/Users/mahmoudhamra/Desktop/C language/";
strcat(new, fileName);

Change the 40 to 1024:

char new[1024] = ...;
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • 1
    @Lundin It's a reasonable size to accommodate any file path and is the size of the POSIX `PATH_MAX` on most systems. – trojanfoe May 25 '16 at 06:44