Opening files and reading from them

File size is always zero, even though I know that there is info in it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Homework 4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

int main()
{
	//Variables
	const std::string dataFileLocation = "C:\\Users\\cis.LABS\\Documents\\Visual Studio 2015\\Projects\\Homework 4\\Homework 4\\MP4election.txt"; //Path to file inside project
	std::ifstream dataFile;

	//Load data from .txt file.
	dataFile.open(dataFileLocation, std::ios::in);
	if (dataFile.is_open())
	{
		std::string line;
		
		//DEBUGGING
		dataFile.seekg(0, std::ios::end);
		std::cout << "Size of file is: " << dataFile.tellg() << std::endl;

		while (std::getline(dataFile, line))
		{
			std::cout << line << '\n';
		}
		dataFile.close();
	}
	else
	{
		std::cout << "Error opening file." << std::endl;
	}
	
	system("pause");
}



Am I not opening the file correctly?
closed account (E0p9LyTq)
Your hard-coded path might be the problem. I prefer to use relative pathing when dealing with files.

const std::string dataFileLocation = "../MP4election.txt";

I put the file to open one level up from where the source files and exe output files are, where the VS solution file is and the file is opened just fine.

Why go one level up? Why not use const std::string dataFileLocation = "MP4election.txt";

Depends on how you run your executable. Run from within the VS IDE and the file needs to be in the directory with the source code files. Run from a command prompt, or double click the file, your text file needs to be located where the exe file is.

One level up and no matter how you start your program the text file can be found.
Last edited on
closed account (E0p9LyTq)
One other thing, you forgot to resync the pointer to the contents of the file back to the beginning. You left it at the end when you retrieved the file size. Your program will never read the contents of the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <fstream>

int main()
{
   //Variables
   const std::string dataFileLocation = "../MP4election.txt"; //Path to file inside project
   std::ifstream dataFile;

   //Load data from .txt file.
   dataFile.open(dataFileLocation, std::ios::in);
   if (dataFile.is_open())
   {
      std::string line;

      //DEBUGGING
      dataFile.seekg(0, std::ios::end);
      std::cout << "Size of file is: " << dataFile.tellg() << std::endl;

      dataFile.seekg(0, std::ios::beg);  // seek back to the beginning of the file
      while (std::getline(dataFile, line))
      {
         std::cout << line << '\n';
      }
      dataFile.close();
   }
   else
   {
      std::cout << "Error opening file." << std::endl;
   }
}
Okay, so I made the changes to the code to use a relative path, and it falls to open. If I add the absolute path, putting the file on the desktop it works? My file structure is:

Project
Project/Source/Source.cpp
Project/MP4election.txt
Topic archived. No new replies allowed.