Thread: Having trouble with a simple input/output file program (Ubuntu)

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    Having trouble with a simple input/output file program (Ubuntu)

    Ok so I'm actually in class right now but no matter how many times I raise my hand my teacher decides to just walk past me. I think it's because I am using Ubuntu and she seems to have absolutely no clue how to work with anything in Unix. Anyway here is my code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <cstdio>
    using namespace std;
    int main ()
    {
    	int num;
    	ifstream numbers("numbers_in.ptd");
    	ofstream blank("numbers_out.ptd");
    	if (!numbers_in.is_open())
    	{
    		cout << "Cannot open numbers_in" << endl;
    		exit (1);
    	}
    	else (!numbers_out.is_open());
    	{
    		cout <<"Cannot open " << endl;
    		exit (1);
    	}
    	numbers >> num;	
    	while (numbers >> num)
    	{
    		cout << num;
    	}
    	while(!numbers.eof())
    	{
    		blank << num << endl;
    		numbers >> num;
    	}
    	blank.close ();
    	numbers.close ();
    	
    	return 0;
    }
    When I try to compile I get the error:
    Code:
    HW4_2A.cpp: In function ‘int main()’:
    HW4_2A.cpp:11: error: ‘numbers_in’ was not declared in this scope
    HW4_2A.cpp:16: error: ‘numbers_out’ was not declared in this scope
    The guy beside me is on Mac OS and he said something about having to find where the editor saves the actual program. I'm not sure what he meant or if this even works the same way in Ubuntu. I have created and saved the files numbers_in and numbers_out in the same folder that I saved the code (HW4_2A.cpp) and when I compile any other program they are put into the same folder as the .ccp file. So I'm not sure why it is saying they are not declared when they are in fact in the same folder. Once again I am on Ubuntu and I am using gedit to write the code and terminal to compile it. Thanks in advance for any help as my teacher is avoiding me like the plague.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    ifstream numbers("numbers_in.ptd");
    ofstream blank("numbers_out.ptd");
    These are the names of your streams.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Wow, can't believe I missed that. Most of the problems I've had so far have been simple like this that just need a closer look or a second pair of eyes to catch it. I was set on the possibility that I was encountering a problem similar to the guy beside me on the Mac. Real shame... my teacher likely would have easily noticed this but she seems to not notice any of the guys who are not using Windows. Thanks for solving my simple mistake anyway though. I appreciate it.

    Edit: I am able to compile it now. However it reports that it cannot open numbers_in. Would you happen to know what the file extension is for Ubuntu. When I view the properties of the file it says it is a plain text document which is why I used .ptd. I also tried removing the extension. I'll check again real quick and edit this if I figure it out.

    P.S just changed the permissions of numbers_in to read and write and it seems to be able to open it now as it doesn't report the error but it gives the error for numbers_out now, which I also changed the permissions of.
    Last edited by Murdoc; 04-25-2011 at 10:25 AM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    else (!numbers_out.is_open());
    I think you need to use "else if" instead of just "else"

    Tim S

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Changed that to else if but I am still getting the error. Here is the if/else statements in the code currently:

    Code:
    	int num;
    	ifstream numbers("numbers_in");
    	ofstream blank("numbers_out");
    	if (!numbers.is_open())
    	{
    		cout << "Cannot open numbers_in" << endl;
    		exit (1);
    	}
    	else if (!blank.is_open());
    	{
    		cout <<"Cannot open numbers_out" << endl;
    		exit (1);
    	}

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
    ifstream numbers("numbers_in");
    One of the most obvious reasons that this won't work is because the file is misplaced or doesn't exist. A file called "numbers_in" is different from a file called "numbers_in.txt", like how "numbers.csv" is different from "numbers.doc". Additionally, a "numbers_in" file in your documents is not the same as a "numbers_in" file in your current working directory. Wherever your IDE puts the executable is probably the current working directory at run time, but you need to find out what the directory is.

    Code:
    else if (!blank.is_open());
    {
    	cout <<"Cannot open numbers_out" << endl;
    	exit (1);
    }
    A misplaced semi-colon can make strangeness happen.
    Last edited by whiteflags; 04-25-2011 at 01:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble with a simple program......
    By Redpred in forum C Programming
    Replies: 9
    Last Post: 08-11-2006, 04:39 AM
  2. Trouble with file input
    By w274v in forum C Programming
    Replies: 6
    Last Post: 12-18-2005, 04:40 AM
  3. Having trouble with file input...
    By Xaviar in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2002, 12:55 PM
  4. File Input and Output, simple.
    By Vber in forum C Programming
    Replies: 5
    Last Post: 11-17-2002, 02:57 PM