-
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.
-
You're trying to run a Windows executable program on a UNIX machine?
-
I'm compiling it and running it on Unix.
g++ file.cpp
./a.out
-
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.
-
"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.
-
always check if a file opened successfully and if not at least give a message so you know immediately whats wrong
-
It wasn't a file opening error, though, merely a read error.