hello friends, trying to understand a little work with txt, and in c + +, the file I can not open and goes straight to the end, what will my mistake
the file is called nombres.txt and contains

carlos, alberto, raul, tomas,

and the code is

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
    string linea;
    string nombre;
    char a=',';
    cout<<"ingrese el nombre buscado"<<endl;
    getline(cin, nombre);
    ifstream miArchivo ("nombres.txt");
    if (miArchivo.is_open())
    {
        while (getline(miArchivo, linea,a ))
        {
            if (nombre==linea)
            {
                cout<<"el nombre buscado esta presente "<<endl;
            }
            else
            {
                cout<<"el nombre no esta"<<endl;
            }
        }
        miArchivo.close();
    }
    else
    {
        cout<<"algo paso"<<endl;
    }
    getchar();
    return 0;
}

If nombres.txt is in the same directory as the compiled executable and running under the debugger goes straight to cout<<"algo paso"<<endl - try running the executable outside the debugger.

Some suggestions for your code.
If you put cout << linea << endl at the top of your while loop, you'll notice that you end up with leading whitespace for all but the the first name. So you'll need to trim the whitespace before you do the (nombre == linea) comparison.

while (getline(miArchivo, linea,a ))
{
    cout << linea << endl;
    // Trim leading whitespace
    // Rest of your loop code goes here
}

good day to all, yesterday in a moment of sleep, I realized something very important, "the location of the txt", ok not the most excellent code but it works.
As input for those using visual c + +, when they make a console project like 20 folders and no one knows where it goes the famous txt, so here a picture for you to know where it is located
Click Here

This article has been dead for over six months. Start a new discussion instead.