Vectors, Searching and Files

Hello, I am new to C++ and very lost. I have attempted this program but my output displays file was unable to open. I am not sure I am even on the right track. Here is my project:

Write a program that reads strings from a file into a vector. Ask a user to enter a string to be found. The program should display the index in the vector where that string was foud and the string, or display a messag telling teh user that the string was not found.

Any help would be appreciated!!
Here's what I have so far:

#include <vector>
#include <fstream>
#include <conio.h>
#include <iostream>
using namespace std;

int main()
{
vector<string> names;
string aName = "";//means empty

ifstream file;

file.open("names2.txt");

if( file.fail() )
{
cout << "File failed to open.";
getch();
return 1;
}

while( getline(file,aName) )//use comma its 2 perameters
{
names.push_back(aName);
}

file.close();

for(int i=0; i<names.size(); i++)
cout << i << " " << names[i] << endl;

getch();
return 0;
}

Is the file you're trying to open in the same directory as your executable?
I don't think so because I found a similar program in the book that i used as a guide. Do I have to create the text file before I begin? I thought I could create and write it at once. I am so lost!
Sorry, didn't read it well enough:p I thought you were trying to read an already existing file. Does it display the very same error-message that you created, the "File failed to open"?
Yes it is the exact message that I created within the program.
If you want to write to a file, you need to use ofstream, not ifstream like you are doing here. Right now, it's trying to read a file that's not there, which is why it's giving you the error.
Ok thanks fafner. I will make some changes and keep plugging along.
Topic archived. No new replies allowed.