C++ help

Started by
17 comments, last by Xai 17 years, 8 months ago
If you want us to help, you'll have to provide information about what your code does, what your code is, and what your code should do.
Advertisement
When you say 'not working', do you mean after you type in the filename, the program exits so you can't see the output?

Steven Yau
[Blog] [Portfolio]

Let us know exactly what you want the code to do
the code:

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

int main()
{
std::string filename, text;

std::cout<<"What is the filename:";
std::getline(std::cin,filename);

std::ofstream file1 (filename.c_str());
std::cin>>text;

std::cout<< text;
std::cin.get();
}

I want the program to have you type in a file name (a .txt file) and then print the contents of that file.

what the program is doing is it lets you enter the file name, after you press enter nothing hapens except that the blinking underscore thing goes down to the next line.
Quote:Original post by fishfin
std::ofstream file1 (filename.c_str());
std::cin>>text;

std::cout<< text;
std::cin.get();
}


You never do anything with "file1" after it is created. Do you expect it to magically dump all its contents into "text" by calling cin?

I recommend you read the documentation for ofstream, cin, and cout.
well you see this line:

std::cin>>text;

that reads from stdin ... the console keyboard in your case ... NOT the file ..

your whole understanding of the iostream library is somewhat mistaken -

1. cout is stdout, which in a console app is usually for writing text to the screen. (you use this correctly)
2. cin is stdin, which is used to read input from the keyboard in a console app. . You use getline the first time, which is also ok. The middle use of cin is incorrect.
3. oftream stands for "output file stream" which will open a file for writing ... you claim you want to READ the contents of an existing file and display them on the screen. For that you need ifstream "input file stream".

change:

std::ofstream file1 (filename.c_str());
std::cin >> text;

to:

std::ifstream file1(filename.c_str());
file1 >> text;

and you should see the first line of the file.
so you are saying that it should be like this:

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

int main()
{
std::string filename, text;

std::cout<<"What is the filename:";
std::getline(std::cin,filename);

std::ifstream file1(filename.c_str());
file1>>text;

std::cout<< text;
std::cin.get();
}
off topic i know, but you could write:

using namespace std;

in the beginning once instead of writing std:: each time
or like this:

#include <iostream>#include <fstream>#include <string>using namespace std;int main(){  string filename, text;  // get a filename from user  cout << "What is the filename:";  getline(cin, filename);  // read and display file  ifstream sourceFile(filename.c_str());  while(sourceFile) // can't remember for sure if this returns false at file end  {    getline(sourceFile, text);    cout << text;  }  // wait at end of program  cin.get();}

This topic is closed to new replies.

Advertisement