visual studio c++如何正确使用fopen()打开txt文件

visual studio c++ how to properly use fopen() to open txt file

我正在学习 C++,练习涉及打开一个 .txt 文件并从中读取。 项目结构如下:

int main()
{

    static const char* full_name = "C:\Users\Lukas\Desktop\Programming\file_system_test\file_system_test\items.txt";
    FILE* my_file = fopen(full_name , "r");
    if (my_file == NULL) {
        perror("error opening file");
    }

}

我正在尝试打开 items.txt 但还没有成功..

由于我提供了完整路径,所以我不太确定是什么问题..

目前尝试的方法:

  1. 在完整路径中使用双反斜杠
static const char* full_name = "C:\Users\Lukas\Desktop\Programming\file_system_test\file_system_test\items.txt";

错误仍然存​​在: error opening file: No such file or directory

已解决

似乎修复此代码的唯一方法是使用原始字符串文字,如下所示:

static const char* full_name3 = R"(C:/Users/Lukas/Desktop/Programming/file_system_test/file_system_test/items.txt)";
    FILE* my_file3 = fopen(full_name3, "r");
    if (my_file3 == NULL) {
        perror("error opening file");
    }

不再return任何错误。

我认为您的问题是文件名中的 \ 未正确转义。 您的文件名字符串应包含双反斜杠字符。

static const char* full_name = "C:\Users\Lukas\Desktop\Programming\file_system_test\file_system_test\items.txt";

您可以通过 std::cout 发送此字符串进行调试来测试它。

注意 escape sequences 字符串字面值,所以你的路径:

static const char* full_name = "C:\Users\Lukas\Desktop\Programming\file_system_test\file_system_test\items.txt";    

包含 \f 转义序列,被解释为 form feed - new page byte 0x0c in ASCII encoding。此字符不能是路径的一部分,因此会报告 Invalid argument 错误。

另外compilers complain其他转义序列未知。

可以通过三种方式修复。

  1. Luka Rahne 所建议,使用反斜杠转义序列 \
  2. 或者使用正斜杠(因为 C 假设是可移植的,标准库能够将 Unix 路径分隔符转换为平台特定的路径分隔符)。
static const char* full_name = "C:/Users/Lukas/Desktop/Programming/file_system_test/file_system_test/items.txt";
  1. 如果您使用的是 C++11 或更新版本(您的代码是 C 而不是 C++,但标签上写着 C++),您可以利用原始字符串文字:
static const char* full_name = R"(C:\Users\Lukas\Desktop\Programming\file_system_test\file_system_test\items.txt)";

这里我用 msvc 做了一些实时测试(文件名为:open.c):

#include <stdlib.h>
#include <stdio.h>

int main(int argc, const char argv[])
{
#if VERSION == 0
    // here '\f' is used to reproduce error "invalid argument":
    static const char name[] = "C:\fUsers\User\Downloads\open.c";
#elif VERSION == 1
    static const char name[] = "C:\Users\User\Downloads\open.c";
#elif VERSION == 2
    static const char name[] = "C:/Users/User/Downloads/open.c";
#elif VERSION == 3
    static const char name[] = R"(C:\Users\User\Downloads\open.c)";
#endif
    FILE* f = fopen(name, "r");
    
    if (!f) {
        perror("fopen");
        return 1;
    }
    
    char buf[256] = "";
    
    fgets(buf, sizeof(buf), f);
    printf("%s\n", buf);
    
    fclose(f);
    
    return 0;
}

这是编译的结果,运行 来自 cmd.exe:

C:\Users\User\Downloads>cl open.c /D VERSION=0 && open.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

open.c
Microsoft (R) Incremental Linker Version 14.28.29336.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:open.exe
open.obj
fopen: Invalid argument

C:\Users\User\Downloads>cl open.c /D VERSION=1 && open.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

open.c
Microsoft (R) Incremental Linker Version 14.28.29336.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:open.exe
open.obj
#include <stdlib.h>


C:\Users\User\Downloads>cl open.c /D VERSION=2 && open.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

open.c
Microsoft (R) Incremental Linker Version 14.28.29336.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:open.exe
open.obj
#include <stdlib.h>


C:\Users\User\Downloads>cl open.c /D VERSION=3 && open.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

open.c
open.c(11): warning C4129: 'm': unrecognized character escape sequence
open.c(11): warning C4129: 'D': unrecognized character escape sequence
open.c(11): warning C4129: 'o': unrecognized character escape sequence
open.c(11): error C2065: 'R': undeclared identifier
open.c(11): error C2143: syntax error: missing ';' before 'string'
open.c(11): error C2099: initializer is not a constant

所以一切都像我描述的那样工作,最后一个版本 3 失败了,因为我将代码编译为 C