C程序打不开文件

标签 c file netbeans

我在同一个文件夹中有程序 proba2.exe 和文件 text.txt,当我在项目属性中设置命令行参数时它无法打开文件,但是当我从命令提示符运行程序时它工作正常。

/* count.c -- using standard I/O */
#include <stdio.h>

#include <stdlib.h> // ANSI C exit() prototype
int main(int argc, char *argv[])
{
    int ch;         // place to store each character as read
    FILE *fp;       // "file pointer" 
    long count = 0;

    if (argc != 2)
    {
        printf("Usage: %s filename\n", argv[0]);
        exit(1);
    }
    if ((fp = fopen(argv[1], "r")) == NULL)
    {
        printf("Can't open %s\n", argv[1]);
        exit(1);
    }
    while ((ch = getc(fp)) != EOF)
    {
       putc(ch,stdout);  // same as putchar(ch);
       count++;
    }
    fclose(fp);
    printf("File %s has %ld characters\n", argv[1], count);

    return 0;
}

enter image description here

enter image description here enter image description here

从 cmd.exe 运行它工作正常 enter image description here 当我在命令参数项目属性窗口中指定完整路径时也可以正常工作(但写入文件名的完整路径) enter image description here

最佳答案

确保运行程序的工作目录与从资源管理器启动时程序文件所在的目录相同。我不确定在 Windows 中可以在哪里设置,但我认为这就是问题所在..

关于C程序打不开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7939018/

相关文章:

c# - 创建文件后删除文件

linux - 在Linux中跨服务器同步文件处理线程

java - 已拾取_JAVA_OPTIONS : -Xmx512M message

java - 在 java netbeans swing 中使用 RSyntaxTextArea 库进行语法高亮显示

c - 对 sqlite_open 的 undefined reference

克隆和 CLONE_CHILD_CLEARTID 标志

c - 没有大括号的嵌套 IF-Else 的工作

c++ - 函数指针的意义何在?

java - 尝试读取 jar 中的文件时出错

java - NetBeans 项目中的 Web 服务引用