【问题标题】:How do I preserve the xcode working directory when compiling into an exec?编译成 exec 时如何保留 xcode 工作目录?
【发布时间】:2019-01-21 14:37:34
【问题描述】:

我在 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 很好,所以不要费心尝试在这里阅读。真的,我唯一的问题是是否有办法编译这个项目以在保留工作目录的终端中运行。感谢您的帮助!

【问题讨论】:

  • 从命令行运行可执行文件时遇到什么错误?
  • 代码中的“错误!打开文件”,当指针为空时。这意味着找不到输入文件。

标签: c xcode compilation


【解决方案1】:

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

您可以添加对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;
}

【讨论】:

    猜你喜欢
    • 2012-03-02
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多