我正在尝试连接两个字符串以用于 stat 函数,但是当我 strcat char* 然后打印时,它会像这样显示两行。
./
boot
tmp
./
tmp
lib
./
lib
lost+found
./
lost+found
sbin
./
sbin
etc
我已经尝试了一些方法,但我可能做错了。这是我的代码。
#include <dirent.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>
int printDetails(char *path, char *name){
struct stat fileInfo;
char file[100];
strcpy(file, path);
strcat(file, name);
printf("%s\n", file);
return 1;
}
int main(int argc, char *argv[]) {
struct dirent *direntp;
DIR *dirp;
printf("Please Enter directory path\n");
char data[64];
if (fgets(data, sizeof data, stdin)) {
if ((dirp = opendir("/")) == NULL) {
perror ("Failed to open directory");
return 1;
}
while ((direntp = readdir(dirp)) != NULL)
{
printf("%s\n", direntp->d_name);
if(direntp->d_name != "." && direntp->d_name != ".."){
printDetails(data, direntp->d_name);
}
}
while ((closedir(dirp) == -1) && (errno == EINTR)) ;
}
return 0;
}
关于这个问题的任何帮助都会很棒。
非常感谢
最佳答案
fgets
将在每行末尾包含 \n
字符。也就是说,假设文本行不长于您传入的大小 - 否则您将只获得该行的一部分。
关于c - c 中的字符串连接分为两行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42419739/