Hello everyone,

I have an assignment that I am working on for a class at my school. I have to write a program that will open a .txt file and read/copy the code to a string. This is probably really easy, but I guess my knowledge thus far is a bit lacking. I am not asking for you guys to finish my code by any means, I enjoy figuring most things out for myself, but have been stuck for a bit, and just need to be pointed in the right direction.

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

using namespace std;

int main()
{
	ifstream inFile;
	string fileName;
	string inputString;

	cout << "Please enter the first input file name:\n";
	cin >> fileName;
	cout << fileName << endl << endl;

	inFile.open(fileName.c_str());

	getline(inFile, inputString);
	
	cout << "First line of file: " << inputString << endl;

	return 0;
}

I am using Visual Studio 2008 and I have all of my files(the source files and the .txt files added to my working directory. I am not sure if I have to specify the path as to where the files are located or what is going on. When I run the code, All I get on the final cout statement is the "First line of file:", and nothing where the inputString is supposed to print. Any help you guys can offer would be great. Thanks in advance.

Joshua

The file is probably not in the current directory. either use getline() so that you can enter the complete path to the file, or move the file into the current working directory, which is where the *.cpp file is located. Also make sure you spell the file name correctly -- I like to just hard code them for testing purposes.

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.