having trouble getting the file.txt to work

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »

70 Replies - 2776 Views - Last Post: 12 November 2014 - 10:57 PM Rate Topic: -----

#16 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 11:22 AM

Does the file exist in the current working directory?


Jim

This post has been edited by jimblumberg: 12 November 2014 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#17 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 11:46 AM

Did you enter postfix.txt? Windows can hide the file extension, notepad files usually have a .txt extension.
Was This Post Helpful? 1
  • +
  • -

#18 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 12:02 PM

yes the file is exists and i entered .txt
Was This Post Helpful? 0
  • +
  • -

#19 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 12:49 PM

Okay, make sure the file isn't name postfix.txt.txt many times notepad adds the extension even when you specify one.

Next post your current code.

Jim
Was This Post Helpful? 0
  • +
  • -

#20 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 01:10 PM

#include    <iostream>
#include    <fstream>
#include    <cctype>
#include    <string>
#include    <stack>
using namespace std;

// function prototypes

ifstream& get_ifs(ifstream& ifs);


int main()
{
	ifstream ifs;	
	char c;   // input character
	if (!get_ifs(ifs))
	{ //File failed to open.
		return(1);
	}


	while ((c = ifs.peek()) != EOF)          // as long as there is input
	{
		stack< int > stk;                       // stack of ints
		stk.push(c);// handle one expression
		while ((c = ifs.get()) != '\n')
		{
			if (isspace(c))
			{
				putchar(c);
				c++;
				// handle whitespace character
			}
			else if (isdigit(c))             // is it a digit ?
			{
				ifs.putback(c);               // put it back in the input
				int number;                     // an incoming number
				ifs >> number;                  // read the number
				stk.push(number);
				cout << number << ' ';
			}
			else                               // it must be an operator
			{
				int a;
				int b;
				switch (c) 
				{
					case '+':return a + b;
					case '-':return a - b;
					case '*':return a*b;
					case '/':return a / b;
					default: return 0;
				}
				// handle operators +, -, *, and /
				// flag invalid operators
			}
		}

		cout << "result is " << stk.top() << '\n';
	}

	return 0;
}


ifstream& get_ifs(ifstream &ifs)                               // get input file stream
{
	string filename;                            // input file name

	cout << "name of file to read from? ";
	cin >> filename;
	ifs.open(filename.c_str());

	
	if (!ifs)                                 // cannot open file infilen
	{
		cerr << "cannot open input file '" << filename << "'\n";
	}

	return ifs;                                 // return input file stream

}


Was This Post Helpful? 0
  • +
  • -

#21 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 01:35 PM

Okay the program successfully opens the input file for me, so the problem is probably that the file name you entered doesn't exist in the working directory of the your program. To see if this is the problem let's modify your main()

#include    <iostream>
#include    <fstream>
#include    <cctype>
#include    <string>
#include    <stack>
using namespace std;

// function prototypes

ifstream& get_ifs(ifstream& ifs);


int main()
{
   // Insert the following lines at the very beginning of main().
   ofstream fout("MySpecialFile.txt");
   if(!fout)
   {
     cerr << "Couldn't open the file for output.";
     return(1);
   }
   cout << "SUCCESS" << endl;
   return(0);

// ... The rest of your program follows.

	ifstream ifs;	



Insert the lines indicated at the beginning of main, compile and run the program and tell me what message appears.

Jim

This post has been edited by jimblumberg: 12 November 2014 - 01:51 PM

Was This Post Helpful? 0
  • +
  • -

#22 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 01:41 PM

says success when i run it
Was This Post Helpful? 0
  • +
  • -

#23 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 01:51 PM

Okay great. Now use your operating systems find functionality and locate the directory where that file is located (MySpecialFile.txt). Then do the same for your file postfix.txt. Does the file postfix.txt file exist in the same directory as MySpecialFile.txt? If not then that's your problem.

You can not take out those added lines, they won't be needed any longer.

Jim
Was This Post Helpful? 0
  • +
  • -

#24 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 02:05 PM

Now when i add the name of the file, "postfix.txt" i dont get the error message it just says "press any key to continue"

All i changed was that i put the postfix.txt file directly into visual studios

This post has been edited by RFernandez: 12 November 2014 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

#25 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 02:18 PM

Now you need to set a breakpoint somewhere before your loop, and run the program through your debugger. Single step through the program and watch the program flow, the problem is probably with that while loop so you want to see if it actually executes.

You may also want to post your current code again, so I can see that it is the same as your previous code.

Jim
Was This Post Helpful? 0
  • +
  • -

#26 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 02:21 PM

#include    <iostream>
#include    <fstream>
#include    <cctype>
#include    <string>
#include    <stack>
using namespace std;

// function prototypes

ifstream& get_ifs(ifstream& ifs);


int main()
{
	
	ifstream ifs;	
	char c;   // input character
	if (!get_ifs(ifs))
	{ //File failed to open.
		return(1);
	}
	



	while ((c = ifs.peek()) != EOF)          // as long as there is input
	{
		stack< int > stk;                       // stack of ints
		stk.push(c);// handle one expression
		while ((c = ifs.get()) != '\n')
		{
			if (isspace(c))
			{
				putchar(c);
				c++;
				// handle whitespace character
			}
			else if (isdigit(c))             // is it a digit ?
			{
				ifs.putback(c);               // put it back in the input
				int number;                     // an incoming number
				ifs >> number;                  // read the number
				stk.push(number);
				cout << number << ' ';
			}
			else                               // it must be an operator
			{
				int a;
				int b;
				switch (c) 
				{
					case '+':return a + b;
					case '-':return a - b;
					case '*':return a*b;
					case '/':return a / b;
					default: return 0;
				}
				// handle operators +, -, *, and /
				// flag invalid operators
			}
		}

		cout << "result is " << stk.top() << '\n';
	}

	return 0;
}


ifstream& get_ifs(ifstream &ifs)                               // get input file stream
{
	string filename;                            // input file name

	cout << "name of file to read from? ";
	cin >> filename;
	ifs.open(filename.c_str());

	
	if (!ifs)                                 // cannot open file infilen
	{
		cerr << "cannot open input file '" << filename << "'\n";
	}

	return ifs;                                 // return input file stream

}



ill post the file too
5 6 *
5 6 1 + *
5 6 * 15 -
5 6 * 15 -
40 5 6 * 3 / +
Was This Post Helpful? 0
  • +
  • -

#27 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 02:35 PM

With your code and your input file I get the following output from your program:

name of file to read from? postfix.txt
5  6 


What operating system and IDE are you using?


Jim
Was This Post Helpful? 0
  • +
  • -

#28 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 02:38 PM

I have windows and i am using Visual Studios 2013
Was This Post Helpful? 0
  • +
  • -

#29 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 02:43 PM

The problem appears to be with your loop. I really think you need to rethink your approach. For example what happens if one of the numbers is greater than 9? Another thing I see is that c should be an int, not a char, especially since you're trying to test for EOF().

Jim
Was This Post Helpful? 1
  • +
  • -

#30 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 02:49 PM

I did not code those 2 things. the teacher gave us that part. If it is wrong i will try to take a look at it though

we have not really learned much about this stuff since this was given. most of what this assignment was supposed to be was doing the postfix stuff.
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »