fopen() returns NULL,文件存在于当前目录
fopen() returns NULL, file exists in current directory
我无法弄清楚 - 我的工作目录中有一个文件 input1.txt
,但我无法 fopen()
它。
我的目录结构(Xcode):
main():
const char* fileName = "input1.txt";
if (argc > 1)
{
fileName = argv[1];
}
printf("Opening file: %s\n", fileName);
clock_t timer = clock();
HashMap* map = hashMapNew(10);
// --- Concordance code begins here ---
// Be sure to free the word after you are done with it here.
FILE *in;
if ( (in = fopen(fileName, "r") ) == NULL ) {
printf ("Can’t open %s for reading. %s\n", fileName, strerror(errno));
}
char* w = nextWord(in);
printf("%s",w);
下一个单词():
char* nextWord(FILE* file)
{
int maxLength = 16;
int length = 0;
char* word = malloc(sizeof(char) * maxLength);
while (1)
{
char c = fgetc(file);
if ((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
c == '\'')
{
if (length + 1 >= maxLength)
{
maxLength *= 2;
word = realloc(word, maxLength);
}
word[length] = c;
length++;
}
else if (length > 0 || c == EOF)
{
break;
}
}
if (length == 0)
{
free(word);
return NULL;
}
word[length] = '[=12=]';
return word;
}
我收到错误:
Can’t open input1.txt for reading. No such file or directory
为什么这不起作用?文件肯定在那里...
在 Xcode 中,您的项目 "folders" 最好称为 "groups,",它可能指也可能不指磁盘上的实际文件夹。组目录层次结构存储在您的项目设置中,不一定对应于您的文件系统层次结构(想想符号链接)。尝试右键单击有问题的文件之一,然后选择 "Show in Finder" 查看其在磁盘上的实际位置。
如果 input1.txt 实际上与调用函数文件不在磁盘上的同一目录中,请尝试通过 Finder 移动它,然后通过 Xcode 将其重新添加到项目中(文件-> "Add files to...").
编辑:我刚找到一个工具来同步您的组和文件结构。 Take a look at synx
我无法弄清楚 - 我的工作目录中有一个文件 input1.txt
,但我无法 fopen()
它。
我的目录结构(Xcode):
main():
const char* fileName = "input1.txt";
if (argc > 1)
{
fileName = argv[1];
}
printf("Opening file: %s\n", fileName);
clock_t timer = clock();
HashMap* map = hashMapNew(10);
// --- Concordance code begins here ---
// Be sure to free the word after you are done with it here.
FILE *in;
if ( (in = fopen(fileName, "r") ) == NULL ) {
printf ("Can’t open %s for reading. %s\n", fileName, strerror(errno));
}
char* w = nextWord(in);
printf("%s",w);
下一个单词():
char* nextWord(FILE* file)
{
int maxLength = 16;
int length = 0;
char* word = malloc(sizeof(char) * maxLength);
while (1)
{
char c = fgetc(file);
if ((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
c == '\'')
{
if (length + 1 >= maxLength)
{
maxLength *= 2;
word = realloc(word, maxLength);
}
word[length] = c;
length++;
}
else if (length > 0 || c == EOF)
{
break;
}
}
if (length == 0)
{
free(word);
return NULL;
}
word[length] = '[=12=]';
return word;
}
我收到错误:
Can’t open input1.txt for reading. No such file or directory
为什么这不起作用?文件肯定在那里...
在 Xcode 中,您的项目 "folders" 最好称为 "groups,",它可能指也可能不指磁盘上的实际文件夹。组目录层次结构存储在您的项目设置中,不一定对应于您的文件系统层次结构(想想符号链接)。尝试右键单击有问题的文件之一,然后选择 "Show in Finder" 查看其在磁盘上的实际位置。
如果 input1.txt 实际上与调用函数文件不在磁盘上的同一目录中,请尝试通过 Finder 移动它,然后通过 Xcode 将其重新添加到项目中(文件-> "Add files to...").
编辑:我刚找到一个工具来同步您的组和文件结构。 Take a look at synx