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.
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.
(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:
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
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.
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.