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

Dani AI

Generated

— the empty output almost always means the program never successfully opened the file (or the file’s first line really is empty). was correct to point at a working-directory issue; the debugger may be launching the program from a different folder than where the .cpp or .txt file sits. The following checklist and short example show how to determine where the program is running, verify the open succeeded, and safely pull the whole file into a string for inspection.

A compact diagnostic example (MSVC-friendly) that prints the current working directory, accepts a filename (allows spaces), checks the stream, and reads the file into a std::string:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <direct.h> // _getcwd on MSVC

int main()
{
    char cwd[1024];
    if (_getcwd(cwd, sizeof(cwd)))
        std::cout << "Working directory: " << cwd << '\n';

    std::string filename;
    std::cout << "File name: ";
    std::getline(std::cin, filename);

    std::ifstream in(filename.c_str());
    if (!in.is_open()) {
        std::cerr << "Failed to open '" << filename << "'\n";
        return 1;
    }

    std::ostringstream ss;
    ss << in.rdbuf();
    std::string contents = ss.str();
    std::cout << "Read " << contents.size() << " bytes\n";
    std::cout << contents.substr(0, 400) << '\n';
    return 0;
}

Extra tips: confirm the exact filename printed by the program (typos and stray quotes cause failures), use an absolute path if needed, and check Visual Studio’s Project Properties -> Configuration Properties -> Debugging -> Working Directory to see where the program runs under the debugger. For very large files, avoid loading the entire file into memory; process line-by-line instead. For reference on stream checks and reading a streambuf into a string, see the C++ reference on basic_ifstream and basic_istream::rdbuf.

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.