如何读取确实存在但不包含数据的文件? [英] How to read a file that does exist but contains no data?

查看:71
本文介绍了如何读取确实存在但不包含数据的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,

如何读取确实存在但不包含数据的文件,显示错误消息?如果文件包含数据,如何计算总整数?



非常感谢您的时间和帮助





这里是我的代码\

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 \

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.

推荐答案

好吧,我不喜欢你的程序结构起初的方式 - 看看



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

        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())
	{
 
	}





假设的最后一个'else if'条款是什么要做什么?



你可以做(​​例如)





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

you could do (for example)

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;
}





这样可以避免使用if then else类型的东西,只需退出程序并显示一条消息如果文件不存在 - 你可以将我为文件显示的任何测试包装到一个单独的函数中,如果你不想退出程序而是做其他事情 - nb:还有很多其他方法可以检查文件是否存在,我假设你有一个'本地'而不是'网络'驱动器/路径到你的文件



......因为'有多少'整数在文件,你已经有了一个while循环 - 如果我只是写一个我自己的片段: -





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 :-

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





将整数读入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 :-

  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





这对你有帮助吗 - 我建议你多考虑一下如何将它们放在一起,这取决于你为什么要这样做?b $ b

a)得到的数量文件中的整数

b)你还要用文件/整数等做什么



hth



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


这篇关于如何读取确实存在但不包含数据的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆