Cannot open file using variable name

I cannot open the file using variable name.
I tried the suggested answers to similar problems and still not working.
when doing this:

1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "Archivo para el Tipo de Modelo: " << ArchivoTipoRed << endl;
ifstream TipoRedFile(ArchivoTipoRed.c_str(),ifstream::in);
string LineaTipoRed;
if (TipoRedFile.is_open())
{
    cout << "Leo el archivo" << endl;
    getline(TipoRedFile,LineaTipoRed);        
}
else
{
    cout << "ERROR: No se puede abrir el archivo" << endl;
    exit(EXIT_FAILURE);
}

I end up in the else statement, but using the explicit name of the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "Archivo para el Tipo de Modelo: " << ArchivoTipoRed << endl;
ifstream TipoRedFile("archivo.txt",ifstream::in);
string LineaTipoRed;
if (TipoRedFile.is_open())
{
    cout << "Leo el archivo" << endl;
    getline(TipoRedFile,LineaTipoRed);        
}
else
{
    cout << "ERROR: No se puede abrir el archivo" << endl;
    exit(EXIT_FAILURE);
}


It goes right. I don't want to use the explicit name, I may not know it until runtime. I also tried with the full path in the variable name but still doesnt work.

Ty in advance
This should work, I don't see any problem with your code.
1
2
    string ArchivoTipoRed = "archivo.txt";
    ifstream TipoRedFile(ArchivoTipoRed.c_str());


I also tried with the full path in the variable name but still doesnt work

Remember to use "\\" to represent each single '\', for example:
string ArchivoTipoRed = "D:\\temp\\input.txt";
Last edited on
Well, now I found something new... it looks like it's more subtle. Doing what you say works, but im defining the string as an element of a string vector.

My actual code goes something like this:

1
2
3
4
5
6
7
8
vector<string> myVector;
string Linea;
int start_pos=0;
ifstream InstFile("Instructions.txt",ifstream::in);
getline(InstFile,Linea);
myVector.pushback(Linea.substr(start_pos));
string ArchivoTipoRed=myVector[0];
ifstream TipoRedFile(ArchivoTipoRed.c_str());


(assume that the file name stored is the entire line "Linea", so there's no actual problem with the end of the line "Linea")
This code cannot open the file.

I've been doing some test after your answer and if I do this:

1
2
3
4
5
vector<string> myVector;
string fileName="archivo.txt";
myVector.pushback(fileName.c_str());
string ArchivoTipoRed=myVector[0];
ifstream TipoRedFile(ArchivoTipoRed.c_str());


The code works. So, I guess the problem is with "Linea.substr(start_pos)", replacing it with "(Linea.substr(start_pos)).c_str()" does not work either.
Can you suggest another way of extracting the information from that line and make this code work?
Also look if your your compiler support C++11
If so:
a) you do not need to use .c_str() : std::ifstream TipoRedFile(ArchivoTipoRed);
b) You can use R"(your\path\here)" (note single backslashes)
http://en.cppreference.com/w/cpp/language/string_literal

Windows accepts forward slashes as directory separator if not in command line (in std::system() calls).
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
Note File I/O functions in the Windows API convert "/" to "\" as part of converting the name to an NT-style name, except when using the "\\?\" prefix as detailed in the following sections.
http://en.wikipedia.org/wiki/Filename
The MS-DOS command.com shell would consume it as a switch character, but Windows itself always accepts it as a separator.
Last edited on
I don't understand what you are attempting to achieve with Linea.substr(start_pos)

When start_pos == 0 the substring is simply a copy of the original string, so it isn't needed.

Are there cases where start_pos is given a different value? If so, how is this done?
Sorry, just tryed to simplify the code, it goes exactly like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
vector<string> VectorDeLinea;
string Linea;
int start_pos=0,end_pos;

ifstream InstFile("Instructions.txt",ifstream::in);
if (InstFile.is_open())
{
     getline(InstFile,Linea);
     end_pos = Linea.find_first_of(" ", start_pos);
     VectorDeLinea.push_back((Linea.substr(start_pos, end_pos-start_pos)).c_str());
     start_pos = end_pos+1;
     VectorDeLinea.push_back(Linea.substr(start_pos));
     TipoRed = atoi(VectorDeLinea[0].c_str());
     ArchivoTipoRed = VectorDeLinea[1];
     ifstream TipoRedFile(ArchivoTipoRed.c_str());
     string LineaTipoRed;
     if (TipoRedFile.is_open())
    {
        cout << "Leo el archivo" << endl;
        getline(TipoRedFile,LineaTipoRed);        
    }
    else
    {
        cout << "ERROR: No se puede abrir el archivo" << endl;
        exit(EXIT_FAILURE);
    }
}
else
{
    cout << "ERROR: No se puede abrir el archivo" << endl;
    exit(EXIT_FAILURE);
}


The file "Instructions.txt" is just:
 
18 archivo.txt


The file "Instructions.txt" is just:
18 archivo.txt
as long as there is just a single space between the number and the filename, that should work.

I suggest you temporarily insert a few extra cout statements to display the contents of the strings such as Linea and ArchivoTipoRed in order to confirm the contents. Such as:
 
    cout << '[' << ArchivoTipoRed << ']' << endl;

The use of the '[' and ']' (or any other displayable character) allows any leading or trailing spaces to be visible.
Thank you!

I had a cout but it was just:
 
cout << "Archivo para el Tipo de Modelo: " << ArchivoTipoRed.c_str() << endl;

and the output was coherent:
 
Archivo para el Tipo de Modelo: archivo.txt


When I added your suggestion:
 
cout << "Archivo para el Tipo de Modelo: [" << ArchivoTipoRed.c_str() << "]" << endl;

a strange output came:
 
]rchivo para el Tipo de Modelo: [archivo.txt


So I check and I had a "\r" at the end of the string. Still don't know why it appeared, but i solved by adding:
1
2
3
4
if (!ArchivoTipoRed.empty() && ArchivoTipoRed[ArchivoTipoRed.size() - 1] == '\r')
{
    ArchivoTipoRed.erase(ArchivoTipoRed.size() - 1);
}

before
 
ifstream TipoRedFile(ArchivoTipoRed.c_str());


Thanks again! Problem solved!
Do you know why that scape appeared?
That is strange. It looks like file were opened in binary mode. Or it was created on Windows machine and opened on Linux one.
Topic archived. No new replies allowed.