Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

swanflyer

macrumors newbie
Original poster
Jul 9, 2008
6
0
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!:)
 

swanflyer

macrumors newbie
Original poster
Jul 9, 2008
6
0
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

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
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

macrumors newbie
Jul 22, 2008
29
0
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

macrumors newbie
Original poster
Jul 9, 2008
6
0
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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:
Code:
#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

macrumors newbie
Original poster
Jul 9, 2008
6
0
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.