目录修改的分段错误?
这是我的代码:
/*
* main.c
*
* Created on: 15 Oct 2010
* Author: mohit
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/dir.h>
#include <sys/unistd.h>
void print_usage();
int main(int argc, char *argv[])
{
int i;
char *directory;
char *name;
//Iterate through command line arguments
for (i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--directory"))
{
memcpy(directory, argv[i + 1], sizeof (argv[i + 1]));
continue;
}
else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--name"))
{
memcpy(name, argv[i + 1], sizeof (argv[i + 1]));
continue;
}
}
if (!name)
{
perror("\n\nIncorrect Usage! Name was not provided!\n\n");
print_usage();
}
if (directory)
chdir(directory);
printf("\nCreating %s.theme directory...\n", name);
mkdir(strcat(name, ".theme"), "a+rw");
printf("Created %s.theme...\n", name);
printf("Entering %s.theme...\n", name);
chdir(strcat(name, ".theme"));
printf("Creating Icons directory...\n");
mkdir("Icons", "a+rw");
printf("Created Icons...\n");
printf("Creating Bundles directory...\n");
mkdir("Bundles", "a+rw");
printf("Created Bundles...\n");
printf("Creating UIImages directory...\n");
mkdir("UIImages", "a+rw");
printf("Created UIImages...\n");
printf("Creating Folder directory...\n");
mkdir("Folder", "a+rw");
printf("Created Folder...\n");
return 0;
}
void print_usage()
{
printf("\n\nUsage: wbt-create [--directory directory] --name theme_name\n");
printf("\n\n\t wbt-create [-d directory] -n theme_name\n");
}
Here is my code:
/*
* main.c
*
* Created on: 15 Oct 2010
* Author: mohit
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/dir.h>
#include <sys/unistd.h>
void print_usage();
int main(int argc, char *argv[])
{
int i;
char *directory;
char *name;
//Iterate through command line arguments
for (i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--directory"))
{
memcpy(directory, argv[i + 1], sizeof (argv[i + 1]));
continue;
}
else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--name"))
{
memcpy(name, argv[i + 1], sizeof (argv[i + 1]));
continue;
}
}
if (!name)
{
perror("\n\nIncorrect Usage! Name was not provided!\n\n");
print_usage();
}
if (directory)
chdir(directory);
printf("\nCreating %s.theme directory...\n", name);
mkdir(strcat(name, ".theme"), "a+rw");
printf("Created %s.theme...\n", name);
printf("Entering %s.theme...\n", name);
chdir(strcat(name, ".theme"));
printf("Creating Icons directory...\n");
mkdir("Icons", "a+rw");
printf("Created Icons...\n");
printf("Creating Bundles directory...\n");
mkdir("Bundles", "a+rw");
printf("Created Bundles...\n");
printf("Creating UIImages directory...\n");
mkdir("UIImages", "a+rw");
printf("Created UIImages...\n");
printf("Creating Folder directory...\n");
mkdir("Folder", "a+rw");
printf("Created Folder...\n");
return 0;
}
void print_usage()
{
printf("\n\nUsage: wbt-create [--directory directory] --name theme_name\n");
printf("\n\n\t wbt-create [-d directory] -n theme_name\n");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当程序运行时,
argv
中的参数将保持分配状态。因此,您可以避免memcpy(以及内存分配)并简单地分配指针。下面的示例:但是,我强烈建议使用 getopt 。它是一个相当标准的库,并且可以很好地处理此类事情。
Arguments in
argv
will stay allocated while your program is running. Therefore, you can avoid thememcpy
(and the allocation of memory) and simply assign the pointers. Example below:However, I would highly recommend the use of getopt. It's a fairly standard library and handles these kind of things quite well.
这些声明了指针,但没有为它们分配任何内存。当您执行 memcpy() 将数据复制到这些指针时,您将遇到 Seg 错误。
您可以在堆栈上声明 char 数组,例如:
或者您必须使用 malloc() 自己分配内存。
Those are declaring pointers, but not allocating any memory for them. When you do the memcpy() to copy data to those pointers you will get the Seg fault.
You could declare char arrays on the stack instead, something like:
or you will have to use malloc() to allocate the memory yourself.
更改:
至:
Change:
to: