C++ 使用 fopen 打开文件,在 Windows 上给出绝对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11466139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open file with fopen, given absolute path on Windows
提问by franvergara66
I'm trying to make a program that counts the number of lines of a file, when I try to pass the absolute path to the fopen function, is simply tells me that is not found, here is my code:
我正在尝试制作一个计算文件行数的程序,当我尝试将绝对路径传递给 fopen 函数时,只是告诉我找不到,这是我的代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(int argc, char *argv[])
{
int i=0;
char array[100];
char caracteres[100];
FILE *archivo;
archivo = fopen("C:\Documents and Settings\juegos psps.txt","r");
if (archivo == NULL){cout<<"Dont Work";}
while (feof(archivo) == 0)
{
fgets(caracteres,100,archivo);
i++;
}
cout << "Number of lines:" << i ;
return 0;
}
How should I pass the absolute path to my program so you can open the file?
我应该如何将绝对路径传递给我的程序,以便您可以打开文件?
回答by SingerOfTheFall
Use double slashes:
使用双斜线:
"C:\Documents and Settings\juegos psps.txt"
回答by Jon
It is not working because the compiler examines a backslash in a literal string together with the next character and usually interprets them as one character in all. Such two-char sequences in string literals are called escape sequences.
它不起作用,因为编译器检查文字字符串中的反斜杠和下一个字符,并且通常将它们解释为一个字符。字符串文字中的这种双字符序列称为转义序列。
The sequences \D
and \j
do not map to anything (contrast this with \n
which maps to the newline character), and in this case the standard says that the compiler can interpret them as it chooses. Some compilers choose to ignore the backslash, which in your case would result in the equivalent:
序列\D
和\j
不映射到任何东西(对比 this\n
映射到换行符),在这种情况下,标准说编译器可以按照它的选择来解释它们。一些编译器选择忽略反斜杠,在您的情况下会导致等效:
archivo = fopen("C:Documents and Settingsjuegos psps.txt","r");
(You can try creating a file with this name to test if this is what your compiler does).
(您可以尝试使用此名称创建一个文件来测试这是否是您的编译器所做的)。
The correct escape sequence for a backslash is a double backslash, so you should write it as
反斜杠的正确转义序列是双反斜杠,所以你应该把它写成
archivo = fopen("C:\Documents and Settings\juegos psps.txt","r");
回答by Preexo
works as well on windows and linux: /
instead of escaping backslashes \\
在 windows 和 linux 上也能正常工作:/
而不是转义反斜杠\\
"C:/dir1/dir2/file.ext"
回答by Greg S
Check the spaces in the filename. The slashes were properly escaped, but the blank space was not.
检查文件名中的空格。斜线被正确转义,但空格没有。
Try:
尝试:
fopen("C:\\\\Documents\ and\ Settings\\\juegos psps.txt","r")
fopen("C:\\\\Documents\ and\ Settings\\\juegos psps.txt","r")
回答by Athimni Iheb
make a new folder in the code blocks folder
在代码块文件夹中创建一个新文件夹
in the project folder next to main and header file make new file "exemple1" and put your file in it "file.txt",then
在主文件和头文件旁边的项目文件夹中创建新文件“example1”并将您的文件放入其中“file.txt”,然后
string nom_fichier;
nom_fichier = "exemple1/file.txt" ;
fichier = fopen(nom_fichier.c_str(), "r+");
回答by liyi3c
Given all codes and paths are correctly typed, another possible reason that causes fopen doesn't work could be your Project Properties-> C/C++ Code Generation->Runtime Library set to /MD rather than /MDd for Debug build, this setting should correspond the project configuration, XX to Release XX(d) to Debug
鉴于所有代码和路径都正确键入,导致 fopen 不起作用的另一个可能原因可能是您的项目属性-> C/C++ 代码生成-> 运行时库设置为 /MD 而不是 /MDd 用于调试构建,此设置应该对应项目配置,XX to Release XX(d) to Debug