I am having a problem with inputs.

I can get the input using the cin command but i want to use the input to open a file with the inputs name

ie the user types in FRED.
cin>>name
i want to open a file called fred.txt

ifstream in("name.txt");
the above will open a file called name not fred
Does anyone know where I am going wrong

Recommended Answers

All 2 Replies

I am having a problem with inputs.

I can get the input using the cin command but i want to use the input to open a file with the inputs name

ie the user types in FRED.
cin>>name
i want to open a file called fred.txt

ifstream in("name.txt");
the above will open a file called name not fred
Does anyone know where I am going wrong

Hi, I am new to c++ so be warned that this my not be the best way to do

this it complided on my machine okay I use Dev Bloodshed Hope it
#include <cstdlib>
#include <iostream>
using namespace std;

#include <fstream>
int main () {
char myfile[128];
char buffer[256];
cout << "Enter the path and name of the file to open ";
cin >> myfile;

ifstream examplefile (myfile);
if (! examplefile.is_open())
{ cout << "Error opening file"; exit (1); }
while (! examplefile.eof() )
{
examplefile.getline (buffer,100);
cout << buffer << endl;
}
return 0;
}

I am having a problem with inputs.

I can get the input using the cin command but i want to use the input to open a file with the inputs name

ie the user types in FRED.
cin>>name
i want to open a file called fred.txt

ifstream in("name.txt");
the above will open a file called name not fred
Does anyone know where I am going wrong

Use strcat(name,".txt") to concatenate .txt to your name string. Then use ifstream in(name);

Be a part of the DaniWeb community

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