Hi, I am writing a program to read from a file.

I want to ask the user for the file name to open.

I have tried to use the basic open file code from cplusplus.com and it won't work unless I specify the file name in the code. If I have them enter a file name, I then need to add "'s to it?
Either way, I get errors if I use char or string for the input file name. Can anyone show me how to do this?

Code from cplusplus.com with my variable for the fileName:

#include <iostream>
#include <fstream>
using namespace std;

int main () 
char fileName[256];
cout << "Enter a file";
cin >> fileName;             //tried string instead of char with getline(cin, fileName)
                                      //but same problem as below.
  ofstream myfile;
  myfile.open (fileName);  //should have quotes around the file name
                                       // ex. ("file.text")
                                       // but how to put 
                                       //them in? Won't let me concatenate them in 
  myfile << "Writing this to a file.\n";
  return 0;

Thanks for any help understanding this

Recommended Answers

All 8 Replies

sorry, comments didn't keep format when I posted.

You don't need to put the name of the file to open in quotes unless you hard code it at time of compilation.

yeah sorry, I just figured that out.

Thanks :-)

You'll also improve your program's safety by using the std::string type:

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

using namespace std;

int main()
  string filename;
  cout << "Name your file> ";
  getline( cin, filename );

  fstream file( filename.c_str() );
  if (!file)
    cout << "I could not open the file. Fooey.\n";
    return EXIT_FAILURE;
    string line;
    int count = 10;
    while ((count > 0) && getline( file, line ))
      cout << line << '\n';

  return EXIT_SUCCESS;

Hope this helps.

Thanks for the advise on safety. Can you tell me what is the deal with

int count = 10;
while (count > 0;


I know what they are but what is there purpose? Why count down from a number instead of just write the file? Is that just an example if I want to specify the number of lines to read? If so, what if I don't know how many lines the file is?

It is just an example.

It is a really simple version of a handy little Unix utility called head. It just displays the first ten lines of a file and quits.

OK thanks a lot Duoas :-)

I was able to incorporate using a vector and am working on adding in a string stream now so I can parse it twice for the purpose of reading and writing csv files from user input or reading from an allready existing csv.

Enter name:
Enter age:
Enter filename for name:
Enter filename fir age:
Displayy all names:
Display all age:

How to do this? What are the codes gatiing this output? Hope someone will help me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.