Open file with fopen, given absolute path on Windows
Asked Answered
C

6

13

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:

#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?

Corpuz answered 13/7, 2012 at 7:38 Comment(2)
try "C:\\Documents and Settings\\juegos psps.txt"Shattuck
Or "C:/Documents and Settings/juegos psps.txt", which also works.Habituate
L
21

Use double slashes:

"C:\\Documents and Settings\\juegos psps.txt"
Logging answered 13/7, 2012 at 7:39 Comment(4)
@SingerOfTheFail I already try that but still not working, may depend on the version of my operating system?. I've been stuck with this hour.Corpuz
@Melkhiah66, under what OS are you running your program? Also, are you sure there is no typo in the filename (like an extra space maybe)?Logging
@SingerOfTheFail Im running the program in Windows 7, using code::blocks, and not theres no typo in the filenameCorpuz
@Melkhiah66, well that's very weird in this case, because this code does work fine for me. Maybe your program doesn't have the permission to read the file?Logging
C
7

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:

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");
Cyrillus answered 13/7, 2012 at 7:45 Comment(2)
yeah man, i know that. the problem is that when I put double slash to the path just as I can not open the file and the program enters the condition if (file == NULL)Corpuz
@Melkhiah66: There's nothing wrong with the code if the filename is correctly specified, so you should be looking if the file is there, permissions etc.Cyrillus
B
3

works as well on windows and linux: / instead of escaping backslashes \\

"C:/dir1/dir2/file.ext"
Benkley answered 31/1, 2014 at 17:34 Comment(0)
C
1

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")

Cutch answered 9/2, 2013 at 21:55 Comment(0)
V
0

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

string nom_fichier;
nom_fichier = "exemple1/file.txt" ;
fichier = fopen(nom_fichier.c_str(), "r+");
Vocalize answered 6/3, 2018 at 15:42 Comment(1)
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.Coquetry
N
-1

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

Naivete answered 8/10, 2015 at 2:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.