Open file with fopen, given absolute path on Windows

franvergara66 picture franvergara66 · Jul 13, 2012 · Viewed 55.8k times · Source

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?

Answer

SingerOfTheFall picture SingerOfTheFall · Jul 13, 2012

Use double slashes:

"C:\\Documents and Settings\\juegos psps.txt"