【问题标题】:getting error in taking input from file in c在c中从文件中获取输入时出错
【发布时间】:2016-09-17 19:19:03
【问题描述】:

我想从 C++ 文件中获取输入。我正在使用 Visual Studio 2015。我用于从文件中获取输入的示例代码函数是:

#define INPUT_FILE_NAME "input.txt"
#define V 15
int total number=0;   
double px[V];
double py[V];      
void fileInput()
{
    FILE *fp;
    char cwd[1024];
    if (getcwd(cwd, sizeof(cwd)) == NULL)   //Directory not found
    {
        perror("getcwd() error - Current directory not found !!");
        exit(0);
    }
    strcat(cwd, "\\");
    strcat(cwd, INPUT_FILE_NAME);
    fp = fopen(cwd, "r");

    int i = 0;
    fscanf(fp, "%d", &start);
    while (fscanf(fp, "%lf", &px[i]) != EOF)
    {
        fscanf(fp, "%lf", &py[i]);
        i++;
        total_number++;
    }
}

我的输入文件格式如下:

2
0 1
2 1

但是当我编译时,我得到一个错误,比如:标识符 'getcwd' 是未定义的。有什么办法可以替换这个函数,让我得到同样的输出?

【问题讨论】:

  • 根据MSDN getcwd 已弃用,建议替换_getcwd<direct.h> 中定义
  • @UnholySheep: void fileInput() 可能是 C++,在 C 中应该是 void fileInput(void),除非当然不需要编译时参数检查。
  • 你为什么需要getcwd?只需使用INPUT_FILE_NAME 作为您的文件名:fopen(INPUT_FILE_NAME, "r");
  • C 和 C++ 是不同的语言。如果您使用 C++ 编写,请扔掉所有这些并学习 C++ API。如果您使用 C 语言编写,请使用 void fileInput(void)
  • @cdarke 你是对的,我错过了(因为我主要看函数的主体)。虽然我猜这也是 C 初学者更常见的错误之一。

标签: c visual-studio file unistd.h


【解决方案1】:

试试标头#include "direct.h" 这可能是为什么...

【讨论】:

  • 这类答案比真实答案更适合 cmets。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-11
  • 1970-01-01
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多