Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

why is my file opening with an error

$
0
0
My assignment this week is to simply "Write a program that asks the user to enter a name of a file. The program should display the number of characters in the program.



#include <fstream>
#include <iostream>
#include <cstdlib>

#include <iomanip>

using namespace std;

int main()
{
	char file_name[81];
	char ch;
	
	cout<<"Enter the name of a file: ";
	cin.getline(file_name, 81);
	

	fstream file(file_name, ios::in | ios::out | ios::binary);
	
	if (!file)
	{
		cout<<endl;
		cout<< "ERROR: Could not open file. " ;
		exit(1);
	}

	 while( (ch = file.get()) !=EOF)
		 {
			 file.seekg(0, ios::end);
			 cout<<file.tellg();              

                }
	 file.close();

	 cout<<"Program Terminated."<<endl;

system("PAUSE");

return 0;
}





Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>