Concatenating file with path to get full path in C

后端 未结 4 1200
执笔经年
执笔经年 2021-01-03 07:57

Using C, I\'m trying to concatenate the filenames in a directory with their paths so that I can call stat() for each, but when I try to do using strcat inside the loop it co

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-03 08:32

    Where is the memory that you are copying to? path is allocated on the stack to contain the arguments you need yo allocate the memory yourself e.g.

    char path[1024] ;   // or some other number
    strcpy( path, argv[1] );
    // then separator
    strcat( path, "/" ) ; // or "\\" in Windows
    strcat( path, ep->d_name);
    

    In production code se strncat etc to stop overflows

提交回复
热议问题