如何从命令行正确传递文件路径? [英] How to properly pass file path with from the command line?

查看:22
本文介绍了如何从命令行正确传递文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于将某些文件从一个目录移动到另一个目录的项目.我已经完成了,只是输出有点奇怪.我需要在 argsv 数组中提供目标路径,但是当我尝试执行我的代码时,它会编译并工作,但显示包含许多路径的错误路径!这是相关部分,如果您需要更多代码,我会添加!提前谢谢你!

I've got a project about moving certain files from one directory to another. I have all finished except that the output is kind of strange. I am required to provide the destination path in the argsv array, but when I try to execute my code, it compiles and works but shows the wrong path containing many paths in one! Here is the relevant part, if you need more code I will add! Thank you in advance!

int main(int argc, char **argv)
{

int size = NFILES;
int index = -1;
file * files = malloc(size * sizeof(file));

listFilesRecursively(argv[1], &files, &size, &index);

if (index != -1) {
      int N = atoi(argv[2]);

if(N==1) qsort(files, index + 1, sizeof(file), compPathname);
else if(N==2) qsort(files, index + 1, sizeof(file), compPathsize);

for (int i = 0; i <= index; ++i) {
        char *dest = argv[3];
                strcat(dest, "/");
                strcat(dest, files[i].justname);
  printf("%s : %s : %ld\n", files[i].name, dest , (long)   files[i].file_info.st_size);
//  if(rename(files[i].name, dest)==0) printf("Success!\n"); else     printf("Failed!/n");
}

所以这是主要的.想要的输出是这样的(我有很多文件):

So this is the main. The desired output is like this (I have many files):

./copyto.c : /home/nik/copyto.c : 676
Success!
./mvfilrd.c : /home/nik/mvfilrd.c : 957
Success!
./sortall.c : /home/nik/sortall.c : 992
Success!

等等......但我得到了

and so on... but instead I get

./newdir/newfile.txt : /home/nik/Music/newfile.txt : 0
Success!
./newdir/3.exe : /home/nik/Music/newfile.txt/3.exe : 0
Failed!/n./newdir/compil : /home/nik/Music/newfile.txt/3.exe/test :     0
Failed!/n./newdir/2.c : /home/nik/Music/newfile.txt/3.exe/test/exe :         0 

然后更多的垃圾

Failed!/n./newf.exe : /home/nik/Music/newfile.txt/3.exe/test/exe    /1//Q�/~�dZ /�l�G^ /
                                                                                    ��`(/4�a^d /a.txt/range/1.txt/1.exe/print.exe/filrd.exeC/2.exre/filrd.exe/2.exe/fi.txt/fil.txt/dest.txt/sorcopy.c/filew.exe/.filer.c.swp    /progfilrd.exe/compile/myfile/.m

第一个参数似乎也崩溃了......

and the first argument seems to have crashed as well...

推荐答案

char *dest = argv[3]; 
strcat(dest, "/"); 
strcat(dest, files[i].justname); 

哎呀,你修改了一个你不拥有的字符串,不要那样做,你可能写出了字符串,在副本上工作

ouch you modify a string you do not own, do not do that, you probably write out of the string, work on a copy

替换

for (int i = 0; i <= index; ++i) {
    char *dest = argv[3];
    strcat(dest, "/");
    strcat(dest, files[i].justname);
    printf("%s : %s : %ld\n", files[i].name, dest , (long)   files[i].file_info.st_size);
    if(rename(files[i].name, dest)==0) 
       printf("Success!\n");
    else
       printf("Failed!/n");
}

for (int i = 0; i <= index; ++i) {
    size_t sz = strlen(argv[3]);
    char *dest = malloc(sz + strlen(files[i].justname) + 2);

    strcpy(dest, argv[3]);
    dest[sz] = '/';
    strcpy(dest + sz + 1, files[i].justname);

    printf("%s : %s : %ld\n", files[i].name, dest , (long)   files[i].file_info.st_size);
    if(rename(files[i].name, dest)==0)
      printf("Success!\n");
    else
      printf("Failed!/n");

    free(dest);
}

这篇关于如何从命令行正确传递文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆