File input and output?

I just learned about file input and output and Im still hazy. how would you go about solving this

a. Write a program which prompts a user for their first and last names, and writes the names to a file called “names.txt”. (The author shows how to read text into a string variable in chapter 1; the first sample program above does as well.)

b. Add code to your program to read the names back in from the same file, and print the names to cout. Be sure to close the file (output stream) after writing to it, otherwise you won’t be able to read anything from the file, then open it with something like ifstream my_input_stream(“names.txt”);
Hint in (a) mentions a code example. I bet is looks like
1
2
std::string text;
std::cin >> text;


std::cin is an istream. std::ifstream is an istream. Reading from cin or from a file are both "read from stream" operations. Output follows similar pattern.
Topic archived. No new replies allowed.