why is my file opening with an error

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 4639 Views - Last Post: 20 December 2012 - 12:41 PM Rate Topic: -----

#1 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

why is my file opening with an error

Posted 20 December 2012 - 11:18 AM

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






Is This A Good Question/Topic? 0
  • +

Replies To: why is my file opening with an error

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5411
  • View blog
  • Posts: 27,415
  • Joined: 10-May 07

Re: why is my file opening with an error

Posted 20 December 2012 - 11:19 AM

2 quick questions :

1.) What file name are you typing in?
2.) Is that file in the same directory as your exe (from your code above)?
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: why is my file opening with an error

Posted 20 December 2012 - 11:24 AM

Also what do you mean by "my file opening with an error"?

Jim
Was This Post Helpful? 0
  • +
  • -

#4 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: why is my file opening with an error

Posted 20 December 2012 - 11:28 AM

I'm just typing in something simple for the file my name "Lynette" and yes they are all in the same file. So I don't know why it is not opening up.

View Postjimblumberg, on 20 December 2012 - 11:24 AM, said:

Also what do you mean by "my file opening with an error"?

Jim


Where I have coded to return 1 if there is an error


if (!file)
	{
		cout<<endl;
		cout<< "ERROR: Could not open file. " ;
		exit(1);
	}



It won't go any further than this it just says "ERROR: Could not open file. " and shuts down fast.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: why is my file opening with an error

Posted 20 December 2012 - 11:33 AM

What compiler/IDE/Operating system are you using?

Jim
Was This Post Helpful? 0
  • +
  • -

#6 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: why is my file opening with an error

Posted 20 December 2012 - 11:37 AM

I am using Microsoft Visual C++, Windows 7

The error says "The program '[3224] wk10.exe: Native' has exited with code 1 (0x1)."
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: why is my file opening with an error

Posted 20 December 2012 - 11:43 AM

Okay, let's try to open a file for output.

#include <fstream>
#include <iostream>

using namespace std;

int main()
{
   ofstream fout("MySpecialFile.txt");

   if(!fout)
      cout << "Error creating file." << endl;
   else
      cout << "File created!!!" << endl;

   return(0);
}


Please enter and run the above program. If the program produces the "File created" message the use your operating system find functionality to locate the MySpecialFile.txt file. Is the file you are trying to open in the same directory as that file?

If you get the "Error creating file" message then you probably have a problem with permissions from your operating system.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: why is my file opening with an error

Posted 20 December 2012 - 11:48 AM

This compiled fine!!
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: why is my file opening with an error

Posted 20 December 2012 - 11:55 AM

It compiles but does it run properly?

Do you get the "File created" message?

Is your file in the same directory as MySpecialFile.txt?

Jim
Was This Post Helpful? 0
  • +
  • -

#10 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: why is my file opening with an error

Posted 20 December 2012 - 12:00 PM

I close visual c++ and reopened and copied and pasted and it actually did not give me an error and compiled but it did not give me the number of characters in the file either.
Was This Post Helpful? 0
  • +
  • -

#11 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5411
  • View blog
  • Posts: 27,415
  • Joined: 10-May 07

Re: why is my file opening with an error

Posted 20 December 2012 - 12:04 PM

No where in his code does it count the number of characters in the file. It creates a new file. He's trying to help you with the problem of if the file exists or not.

Your being dodgy with the Q & A here... not on purpose, but there is a misunderstanding somewhere.

All his example does is create the file, & report if it failed or succeed. Because your example fails on the file name.
Was This Post Helpful? 0
  • +
  • -

#12 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: why is my file opening with an error

Posted 20 December 2012 - 12:11 PM

I understand what he was doing and I said that it did compile correctly. Then I just completely shut down the application and copied and pasted my program I had to the fresh project. This for some reason compiled correctly but only when I put the filename "car" in not actually "carla". So I don't know what that was all about. But I did realize when it compiled it didn't have the number. This was why I responded the way I did. I understand what he was doing but the question I asked was back to my program not the example program he gave me.

View Postno2pencil, on 20 December 2012 - 12:04 PM, said:

No where in his code does it count the number of characters in the file. It creates a new file. He's trying to help you with the problem of if the file exists or not.

Your being dodgy with the Q & A here... not on purpose, but there is a misunderstanding somewhere.

All his example does is create the file, & report if it failed or succeed. Because your example fails on the file name.


thanks i guess i was rushing. i will add this and sees if it works
Was This Post Helpful? 0
  • +
  • -

#13 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5411
  • View blog
  • Posts: 27,415
  • Joined: 10-May 07

Re: why is my file opening with an error

Posted 20 December 2012 - 12:14 PM

Then my mistake. I was the one that misunderstood :P

My apologies.
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: why is my file opening with an error

Posted 20 December 2012 - 12:26 PM

Also note that fstream doesn't create a new file it the file doesn't exist with the open mode you provided.

Why are you trying to open the file in binary mode?

Jim
Was This Post Helpful? 0
  • +
  • -

#15 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: why is my file opening with an error

Posted 20 December 2012 - 12:30 PM

View Postno2pencil, on 20 December 2012 - 12:14 PM, said:

Then my mistake. I was the one that misunderstood :P/>

My apologies.

no problem :)

View Postjimblumberg, on 20 December 2012 - 12:26 PM, said:

Also note that fstream doesn't create a new file it the file doesn't exist with the open mode you provided.

Why are you trying to open the file in binary mode?

Jim


well this was what we were working on this week.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2