我用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很好,所以不要在这里尝试阅读。真的,我唯一的问题是,是否有一种方法可以编译此项目以在保留工作目录的终端中运行。感谢您的帮助!
答案 0 :(得分:0)
您的代码对我有用。可执行文件的工作目录是运行它的位置,而不是可执行文件所在的位置。
您可以向getcwd
添加呼叫,以查看程序的当前工作目录设置为(from this Stack Overflow answer)。如果perror
返回fopen
,则向NULL
添加呼叫也很有帮助,这样您就可以了解失败的原因。
#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;
}