PDA

View Full Version : Why Xcode did not work correctly??




swanflyer
Aug 27, 2008, 03:25 PM
I wrote a small program to read my data file. But I got ridiculous numbers in xcode, but when I use the terminal, " g++ xxx.cpp". The result turns out to be all right.

The program is quite simple, I attach the one to read the first integer number of my file.

#include <iostream>
#include <fstream>

using namespace std;

int main () {
int nnodes;
fstream datafile;
datafile.open("xx.xx");
datafile >> nnodes;
cout << "nnodes=" << nnodes << "\n";
datafile.close();
}


Can anyone give me some idea about this?

Thanks a lot!:)



Cromulent
Aug 27, 2008, 03:34 PM
What are the errors it gives you? Note that if you are using the command line to compile you are not using Xcode.

swanflyer
Aug 27, 2008, 03:45 PM
There's no error message there.
The program is running, but the result is like:

nnodes=-189879( not exact)

in Xcode.

But when I tried in the terminal, the result is

nnodes=978

which is right the first number appeared in my datafile.

whooleytoo
Aug 27, 2008, 03:46 PM
Where is the data file relative to the Xcode project? Could it be that when running in Xcode, the current working directory is different, thus it cannot find the file, so nnodes is uninitialized when run from Xcode?

JVene
Aug 27, 2008, 04:29 PM
As a standard practice, you should always initialize variables like nnodes to zero when you create them.

If, for example, whooleytoo is correct (and I suspect so), you would probably have seen 0 as the output, which would have been slightly more informative, an indication that nothing was read and you're getting the 'default' value.

In many cases, too, the initialization should NOT be zero, but an arbitrary and recognizable default value, like -1 in this case, especially if zero IS a valid value to be read from your file.

swanflyer
Aug 27, 2008, 04:51 PM
Hey, thanks to you all.

My data file is originally in the same directory as the main.cpp and project file. As I copied it to the /build/Release directory, it finally works. Still feel a little strange... But to add an initial value to it is really a good idea.
:):apple:

lee1210
Aug 27, 2008, 05:32 PM
While it's a little late on this one, and initialization is always the right thing to do with your variables, you may also want to see:
http://www.cplusplus.com/reference/iostream/fstream/is_open.html

You would use this like:


#include <fstream>
#include <iostream>

int main (int argc, char *argv[]) {
int nnodes = 0;
std::fstream datafile;
datafile.open("xx.xx");
if(!datafile.is_open()) {
std::cout << "Error! Could not open file." << std::endl;
return 1;
}
datafile >> nnodes;
if(datafile.fail() || datafile.bad() || datafile.eof()) {
std::cout << "Error reading int from file!" << std::endl;
return 2;
}
std::cout << "nnodes=" << nnodes << std::endl;
datafile.close();
if(datafile.fail()) { //It could happen, but i've never seen it
std::cout << "Error closing file!" << std::endl;
return 3;
}
return 0;
}



Error checking is your friend.

-Lee

swanflyer
Aug 27, 2008, 08:28 PM
Wow, thanks a great deal.
I am too lazy to add them in... But you're right. I will try to use them.
It really makes it a more professional piece of program.

By the way, can anyone recommend me some materials to help me to learn to debug my program.

Thanks again!

lee1210
Aug 27, 2008, 10:32 PM
Wow, thanks a great deal.
I am too lazy to add them in... But you're right. I will try to use them.
It really makes it a more professional piece of program.

By the way, can anyone recommend me some materials to help me to learn to debug my program.

Thanks again!

http://sourceware.org/gdb/current/onlinedocs/gdb_toc.html

Learn it, love it. Add -g to g++ to compile with debugging symbols, then fire up gdb. It is integrated with XCode, but I'm a sucker for the commandline. Either way, know how to use it, how to display things, set break points, etc.

There's always print statements... but once you learn gdb you'll never go back.

-Lee