#include<stdio.h>
#include<zlib.h>
#include<unistd.h>
#include<string.h>
int main(int argc, char *argv[])
{
char *path=NULL;
size_t size;
int index ;
printf("\nArgument count is = %d", argc);
printf ("\nThe 0th argument to the file is %s", argv[0]);
path = getcwd(path, size);
printf("\nThe current working directory is = %s", path);
if (argc <= 1)
{
printf("\nUsage: ./output filename1 filename2 ...");
}
else if (argc > 1)
{
for (index = 1; index <= argc;index++)
{
printf("\n File name entered is = %s", argv[index]);
strcat(path,argv[index]);
printf("\n The complete path of the file name is = %s", path);
}
}
return 0;
}
В приведенном выше коде, вот вывод, который я получаю во время выполнения кода:неисправностей Сегментация аргументы команды при чтении строки
$ ./output test.txt
Argument count is = 2
The 0th argument to the file is ./output
The current working directory is = /home/welcomeuser
File name entered is = test.txt
The complete path of the file name is = /home/welcomeusertest.txt
Segmentation fault (core dumped)
Может кто-нибудь пожалуйста мне понять, почему я получаю ядро сбрасывали ошибку ?
Возможно, инициализация 'size' была бы еще одной хорошей идеей. –
getcwd() будет malloc-буфером, если вы передадите ему нулевой указатель. –
@honk: Hah, true :). – slacker