小编jet*_*jet的帖子

无法使用fopen()打开文件

我一直在尝试打开文件和输出文本,但我一直在收到错误.所以我想我会从一开始就尝试打开文件.这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#define CORRECT_PARAMETERS 3

int main(void)
{
    FILE *file;
    file = fopen("TestFile1.txt", "r");
    if (file == NULL) {
        printf("Error");
    }
    fclose(file);
}
Run Code Online (Sandbox Code Playgroud)

当我运行该文件时,"错误"被打印到控制台,就是这样.TestFile1.txt与我的.exe位于同一位置.我该如何解决?

c file-io fopen stdio

9
推荐指数
5
解决办法
11万
查看次数

一次从文件输出一行

我正在尝试一次一行地输出文件中的文本.我目前正在硬编码,到目前为止我有这个:

int main(int argc, char *argv[])
{
    int x;
    int k;
    int limit = 5;
    FILE *file;

    file = fopen("C:\\Documents and Settings\\jon\\My Documents\\Visual Studio 2008\\Projects\\Project1\\Assignment8_2\\Debug\\TestFile1.txt", "r");
    if (file == NULL) {
        perror("Error");
    }

    for (k = 1; k <= limit; k++) {
        while ((x = fgetc(file)) != '\n') {
            printf("%c", x);
        }
    }
    fclose(file);
}
Run Code Online (Sandbox Code Playgroud)

我想知道上面的代码在哪里,如果有的话,我可以检查EOF.我想我需要这样做,但不知道为什么.还在学习....谢谢!

c++ file-io

1
推荐指数
1
解决办法
394
查看次数

printf()调用产生什么价值?

在我的编程课上,我不明白它的价值printf("World")是什么(我教授问的问题)?什么价值printf回报?他说这是5,但我不知道为什么.

c printf

1
推荐指数
1
解决办法
197
查看次数

标签 统计

c ×2

file-io ×2

c++ ×1

fopen ×1

printf ×1

stdio ×1