Code Error !! Opening file problem !!

Hello !

I'm trying to do an exercise where I've to open a txt file, but I don't know why it doesn't open, and the program stops doing nothing during its execution

Part of my code is:

1
2
3
4
5
6
7
8
9
char * ruta_archivo;
	string ruta;

cout << endl << endl << "--> Ruta del fichero : ";
	
	cin >> ruta;
	
	ruta_archivo = new char [ruta.size()-1];
	strcpy (ruta_archivo, ruta.c_str());


Now I call a function with ruta_archivo like file's name, and...

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
        ifstream Puntos_Muestra (route);
 
        
        if (!Puntos_Muestra)
        {

// HERE, THE EXECUTION IS CORRECTLY

            cout << endl << endl;
            cout << "No se pudo abrir el fichero. No existe o la ruta no es valida.";
            cout << endl << endl;
            exit(1);
        }
        
        else
        {

// HERE, THE EXECUTION STOPS.......

            cout << "hola";  // Ensuring that the file is open
           
            float x, f_x_y;
            

            vector<float> componentes (n_var);

            int n_puntos_muestra = 0;
           
            float valor_fitness = 0;
 
            Puntos_Muestra >> x;
 
            while (!Puntos_Muestra.eof()) 
            {
                
                for (int j = 0; j <  n_var; j++)
                {
                    componentes[j] = x;
                    Puntos_Muestra >> x;
                }
                
                float output;
                
                output = pob[i].Calcula_Output (componentes, straight_line);
                
                float t = output - f_x_y;
                valor_fitness += pow (t, 2);
                
                n_puntos_muestra++;
                
                Puntos_Muestra >> x;
            }
            
            valor_fitness /= n_puntos_muestra;
            
            pob[i].fitness = valor_fitness;
        }
    }


Thanks for helping me
Last edited on
1
2
ruta_archivo = new char [ruta.size()-1];
strcpy (ruta_archivo, ruta.c_str());
A char * must end with '\0', so you need to reserve size+1.
However you could avoid the dynammic allocation ifstream Puntos_Muestra (ruta.c_str());
I did what you said... but the same... when I type, for example, 'a.txt' (the file's name) program stops...

It seems that the program recognizes the file but does not open it because the first sentence also (the cout) does not execute.. :(
My bet is that your if (!Puntos_Muestra) is executing too quick for you to see that it is failing out. In this case your file is not open and your else codition is never met. Try piping the failure code to an ifstream object if it fails.
I don't understand you at all... If my file isn't open, de if condition shows me that and If the name is correct, the program stops... :S I don't understand...
I'm not trying to insult anyone my spanish is truley this horrific but I'm making an honest effort to learn and bring my little one up bi-lingual. If you include programming this makes four languages for me so it's pretty difficult.

Tu archievo es no abierto, tu programa es no ver el archievo. Es 'a.txt' en mismo carpeta?
The file is not open, the program is not seeing the file. Is 'a.txt' in the same folder?

Yes, it's in the same folder... Anyway, my code must open a file in any folder, given its path... But I'm trying it with the file in the same folder to make it easier...

I don't understand why if i give a wrong path, or a wrong file name, the if condition executes correctly saying that the file was not found... and why if I give the correct file name, the else condition stops...
I found an error. The file is open correctly, the problem is that numbers aren't read..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ifstream Puntos_Muestra (route.c_str());

		if (Puntos_Muestra.is_open())
		{
			cout << "Hello" << endl;
			float x;
			Puntos_Muestra >> x;
			cout << x;
			
		}		
		// Se comprueba que el fichero se ha abierto correctamente
		//if (!Puntos_Muestra)
		else
		{
			cout << endl << endl;
			cout << "No se pudo abrir el fichero. No existe o la ruta no es valida.";
			cout << endl << endl;
			exit(1);
		}

In the execution, Hello is printed, but no x !!
Last edited on
Sorry, the numbers are read... but they aren't shown unless I do:

if (Puntos_Muestra.is_open())
{
cout << "Hello" << endl;
float x;
Puntos_Muestra >> x;

cout << x; -----------> Number isn't shown
cout << x << endl; ---------> Number is shown
}


Anyone can give me an explanation about this? :S Thanks
Some implementations require the programmer to call flush() on a std::cout stream. Some do it automatically. std::endl includes a call to flush().
Yes, it was exactly this. Thanks !!
Topic archived. No new replies allowed.