Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello All,
How to read a file that does exist but contains no data, Display an error message?and how to calculate the total integers if the file contains data?

I really appreciate your time and help


Here's my code \
C++
include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <string>

using namespace std;
int numIntElement(ifstream &x);
int main() {
	string filename;
	cout << "Please enter the name of the file = ";
	cin >> filename;
	//char filename[50];
	ifstream myfile;
	//cin.getline(filename, 50);
	myfile.open(filename);

	if (!myfile.is_open()) {

		cout << "Error: The file does not exist \n" << endl;
		cout << " ";

	}
	else if (myfile.is_open())
	{

		ifstream x;
		numIntElement(x);
	}
	else if (myfile.is_open())
	{

	}


	system("pause");
	return 0;
}
int numIntElement(ifstream &x) {
	int m;
	ifstream myfile("file.txt");
	char word[50];
	myfile >> word;
	while (!
		myfile.eof())
	{
		cout << word << " " << endl;
		myfile >> word;
	}
	cout << " Please press any key to close the program = " << endl;
	cin >> m;
	return m;
}


What I have tried:

I have created a file and i can access to the file and display what inside the file but i don't know how to return the total number of integers stored in the file, also i would like to display a error message when the file exist but contain no data.
Posted
Updated 4-Feb-16 15:04pm
v3

1 solution

Well, I dont like the way your program is structured for a start - looking at

C++
        if (!myfile.is_open()) {
 
		cout << "Error: The file does not exist \n" << endl;
		cout << " ";
 
	}
	else if (myfile.is_open())
	{
 
		ifstream x;
		numIntElement(x);
	}
	else if (myfile.is_open())
	{
 
	}


what is the last 'else if' clause supposed to do ?

you could do (for example)

C++
std::ifstream myfile(filename);

// Make sure the file stream is good
// Another way could be bool fileIsGood = std::ifstream(filename).good();
//
if(!myfile) {
  cout << endl << "Failed to open file, check that it exists and you have access " << filename << endl;
  return 1;
}


which avoids the if then else type stuff you have and just exits the program with a message if the file doesnt exist - you could wrap either of the tests Ive shown for file exists into a separate function if you didnt want to exit the program but do something else instead - nb: there are plenty of other ways to check if a file exists, Im assuming you have a 'local' rather than 'network' drive/path to your file

...... for 'how many' integers are in the file, you already have a while loop - if I just write a snippet of my own :-

C++
int n = 0;
while(!myfile.eof()) {
  myfile >> n;
  // do something with n
}


That reads integers into n while not at the end of file - so, one way of getting the number of integers in the file might be to add a count to the loop, something like this :-

C++
  int n = 0;
  int countIntsInFile = 0;
  while (!myfile.eof()) {
    myfile >> n;
    // count the number of loops ie integers read
    countIntsInFile += 1;
 }
// now countIntsInFile has 
//     the number of Integers in the file, or, 
//     0 if the file existed but was empty


does that help you - I'd suggest you think a bit more about how you put this together, which depends on why you're

a) getting the number of integers in the file
b) what else you'll be doing with the file/integers etc

hth
 
Share this answer
 
v2
Comments
CPallini 5-Feb-16 2:33am    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900