gpt4 book ai didi

c - 编译成 exec 时如何保留 xcode 工作目录?

转载 作者:行者123 更新时间:2023-11-30 16:22:46 26 4
gpt4 key购买 nike

我在 XCode 中编写了一个相对简单的 C 程序,我可以将带有词汇表列表的未格式化文本文件输入其中,它会返回一个 html 文件,其中包含一个漂亮的表格。在 Xcode 中运行时它工作得很好,它从我设置的工作目录中提取文件并将 html 文件放回那里。不过,我不想每次运行这个程序时都必须打开 XCode。我尝试通过命令行编译它,但完成的 exec 找不到它应该打开的文件,无论它们是在 Xcode 中设置的工作目录中还是与 exec 位于同一目录中。我尝试输入整个文件路径,但这也无助于找到它们。这是该项目的代码:

#include <stdio.h>
#include <stdlib.h>

int main() {
char name1[50], name2[50], Eng[15][256], Kanji[15][256], Roma[15][256]; //declare file names and string arrays
FILE *fptr; //declare file pointer

printf("Input: ");
scanf("%s",name2); //input input file, has to be .txt

printf("\nOutput: ");
scanf("%s",name1); //input output file, has to be .html

int i, m;
printf("\nNumber: ");
scanf("%d",&m); //input number of kanji in list

if ((fptr = fopen(name2,"r")) == NULL){
printf("Error! opening file");
// error in case input file doesn't exist
exit(1);
}

for (i=0;i<m;i++) {
fscanf(fptr,"%s%s%s", Eng[i], Kanji[i], Roma[i]); //reads the list into the arrays
}

fptr = fopen(name1,"w");

fprintf(fptr,"<!DOCTYPE html>\n<head>\n<meta charset='utf-8'>\n<script src='/Volumes/PRIVAT/Hobbies/Code/ressources/jquery-3.3.1.min.js'></script>\n\n<link href='https://fonts.googleapis.com/css?family=Kosugi+Maru' rel='stylesheet'>\n\n<link rel='stylesheet' href='style.css'>\n<script src='script.js'></script>\n\n</head>\n<body>\n<table>\n<colgroup> <col id='col1' span='1'> </colgroup>\n<colgroup> <col id='col2' span='2'> </colgroup>\n\n<caption>WEEK 1</caption>\n<tr>\n<th>English</th>\n<th>Kanji</th>\n<th>Reading</th>\n</tr>"); //writes constant part of html file

for (i=0;i<m;i++) {
fprintf(fptr,"\n<tr>\n<td>%s</td>\n<td>%s</td>\n<td>%s</td>\n</tr>",Eng[i],Kanji[i],Roma[i]); // writes variable part of html file
}

fprintf(fptr,"\n</body>"); //closing html

fclose(fptr); //closes output


return 0;
}

html 很好,所以不用费心在这里阅读。实际上,我唯一的问题是是否有一种方法可以编译该项目以在保留工作目录的终端中运行。感谢您的帮助!

最佳答案

你的代码对我有用。可执行文件的工作目录是您运行它的位置,而不是可执行文件所在的位置。

您可以添加对 getcwd 的调用,以查看程序的当前工作目录设置为 ( from this Stack Overflow answer )。如果 fopen 返回 NULL,添加对 perror 的调用也很有帮助,这样您就可以看到失败的原因。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>

int main() {
char name1[50], name2[50], Eng[15][256], Kanji[15][256], Roma[15][256]; //declare file names and string arrays
FILE *fptr; //declare file pointer

// print current working directory
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working dir: %s\n", cwd);
} else {
perror("getcwd() error");
return 1;
}

printf("Input: ");
scanf("%s",name2); //input input file, has to be .txt

printf("\nOutput: ");
scanf("%s",name1); //input output file, has to be .html

int i, m;
printf("\nNumber: ");
scanf("%d",&m); //input number of kanji in list

if ((fptr = fopen(name2,"r")) == NULL){
perror("Error! opening file");
// error in case input file doesn't exist
exit(1);
}

for (i=0;i<m;i++) {
fscanf(fptr,"%s%s%s", Eng[i], Kanji[i], Roma[i]); //reads the list into the arrays
}

fptr = fopen(name1,"w");

fprintf(fptr,"<!DOCTYPE html>\n<head>\n<meta charset='utf-8'>\n<script src='/Volumes/PRIVAT/Hobbies/Code/ressources/jquery-3.3.1.min.js'></script>\n\n<link href='https://fonts.googleapis.com/css?family=Kosugi+Maru' rel='stylesheet'>\n\n<link rel='stylesheet' href='style.css'>\n<script src='script.js'></script>\n\n</head>\n<body>\n<table>\n<colgroup> <col id='col1' span='1'> </colgroup>\n<colgroup> <col id='col2' span='2'> </colgroup>\n\n<caption>WEEK 1</caption>\n<tr>\n<th>English</th>\n<th>Kanji</th>\n<th>Reading</th>\n</tr>"); //writes constant part of html file

for (i=0;i<m;i++) {
fprintf(fptr,"\n<tr>\n<td>%s</td>\n<td>%s</td>\n<td>%s</td>\n</tr>",Eng[i],Kanji[i],Roma[i]); // writes variable part of html file
}

fprintf(fptr,"\n</body>"); //closing html

fclose(fptr); //closes output

return 0;
}

关于c - 编译成 exec 时如何保留 xcode 工作目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54292231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com