Thread: running program on unix

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    running program on unix

    This program runs fine when I run it in Visual Studio, but when I upload it to a Unix machine, it only prints the first person's data.
    Here is my code:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    
    class Person
    {
    public:
    	char firstName [11];
    	char lastName[11];
    	char address [16];
    	char city [16];
    	char state [3];
    	char zipCode [10];
    	Person();
    };
    Person::Person()
    {
    	lastName[0] = 0;
    	firstName[0] = 0;
    	address[0] = 0;
    	city[0] = 0;
    	state[0] = 0;
    	zipCode[0] = 0;
    }
    ostream & operator << (ostream & stream, Person & p)
    {		
    	stream << "First Name :" << p.firstName << endl;
    	stream << "Last Name  :" << p.lastName << endl;
    	stream << "Address    :" << p.address << endl;
    	stream << "City       :" << p.city << endl;
    	stream << "State      :" << p.state << endl;
    	stream << "Zipcode    :" << p.zipCode << endl;
    	stream << flush;
    
    	return stream;
    }
    istream & operator >> (istream & stream, Person & p)
    {		
    	stream.get(p.firstName, 11);
    	stream.ignore();
    	stream.get(p.lastName, 11);
    	stream.ignore();
    	stream.get(p.address, 16);
    	stream.ignore();
    	stream.get(p.city, 16);
    	stream.ignore();
    	stream.get(p.state, 3);
    	stream.ignore();
    	stream.get(p.zipCode, 10);
    	stream.ignore();
    
    	return stream;
    }
    
    int main()
    {
    	fstream inFile;
    	inFile.open("fixedlength.txt");
    
    	Person person1;	
    
    	while(1)
    	{	
    		inFile >> person1;
    		inFile.ignore();	
    		if(inFile.fail()) break;
    		cout << person1;
    		cout << endl;
    	}
    	
    	cout << endl;
    	cin.get();
    	cout << endl;
    	return 0;
    }
    Here is the input file:
    Code:
    Will       Smith      25 Place        Tulsa           OK 38135     
    Corey      Black      Elm Street      Dallas          TX 89148     
    Tanner     Hall       Mount View      Denver          CO 52742     
    Matthew    Robins     Parkway         Chicago         IL 91234
    At first, I thought it might have something to do with one of the .ignore()'s, but i tried deleting some of them, but it still didn't work. Any ideas?
    Also, I have two more programs similar (almost exactly the same and work fine in Visual Studio) to this one. One of them has a similar problem (only prints 3 out of the four) and the last one works fine. So, i'm guessing the two broken programs have the same problem.

    Thanks.
    IDE - Visual Studio 2005
    Windows XP Pro

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    You're trying to run a Windows executable program on a UNIX machine?

  3. #3
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    I'm compiling it and running it on Unix.

    g++ file.cpp
    ./a.out
    IDE - Visual Studio 2005
    Windows XP Pro

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Works fine on my comp. I'm using gcc 4.2.3 (on Ubuntu).

    Is the "fixedlength.txt" file been created in Windows ? Sometime, those nasty CR-LF conventions makes "weird" bug. I don't know, it might be this, it might be something else.

    You could try checking which of failbit or badbit is set.
    I hate real numbers.

  5. #5
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    "Is the "fixedlength.txt" file been created in Windows ?"

    Yes, it was created in Windows. I made it in Unix, and it now works.
    Thank you for pointing that out.
    IDE - Visual Studio 2005
    Windows XP Pro

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    always check if a file opened successfully and if not at least give a message so you know immediately whats wrong

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It wasn't a file opening error, though, merely a read error.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding Program Running Time
    By pf732k3 in forum C Programming
    Replies: 6
    Last Post: 03-18-2008, 01:56 PM
  2. Stop program from running twice?
    By Abda92 in forum C Programming
    Replies: 19
    Last Post: 03-17-2008, 01:35 PM
  3. Determaining if a program is running
    By WebmasterMattD in forum Linux Programming
    Replies: 4
    Last Post: 04-09-2002, 04:36 PM
  4. Why is my program running away?
    By badkitty in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2001, 04:27 PM
  5. Running program
    By muffin in forum C Programming
    Replies: 5
    Last Post: 08-30-2001, 10:57 AM