我正在使用XCode在C中创建命令行程序.在运行程序时,它最初会执行它应该执行的操作(询问我的文件路径).但是,当我键入有效的和现有的文件路径时,它会给我以下错误:
Program received signal: “EXC_BAD_ACCESS”. sharedlibrary apply-load-rules all (gdb)
Run Code Online (Sandbox Code Playgroud)
我的程序中有两个警告,这两个警告都与strcat函数有关.警告是:
warning: implicit declaration of function 'strcat'
Run Code Online (Sandbox Code Playgroud)
和
warning: incompatible implicit declaration of built-in function 'strcat'
Run Code Online (Sandbox Code Playgroud)
我想知道为什么我的程序没有正常执行.
谢谢,迈克
我的代码发布在下面:
#include "stdlib.h"
int main (void)
{
char *string1;
printf("Type in your file path: ");
scanf("%s", string1);
char *string2 = "tar czvf YourNewFile.tar.gz ";
strcat(string2, string1);
system(string2);
}
Run Code Online (Sandbox Code Playgroud)
也许它与分配字符有关?
是否可以使用其他程序打开程序?例如:我想在C中创建一个命令行应用程序,它将提示用户输入程序名称(比如说Microsoft Word.app),该程序将启动.我会做这样的事情:
#include <stdio.h>
#include <time.h>
int main (int argc, const char * argv[]) {
char programName[1000];
printf("Type in the name of the program you would like to open: ");
scanf("%s", programName);
popen(programName);
}
Run Code Online (Sandbox Code Playgroud)
但是,popen()要求我另一个char.我如何使用popen()打开程序?
编辑:以下代码有效!
#include <stdio.h>
#include <time.h>
int main (int argc, const char * argv[]) {
char programName[1000];
char app[100] = ".app";
char openApp[100] = "open /Applications/";
printf("Type in the name of the program you would like to open: ");
scanf("%s", programName);
strcat(openApp, programName);
strcat(openApp, app);
system(openApp); …Run Code Online (Sandbox Code Playgroud)