-1

I am trying to load my file with C++ using ifstream, however it just wouldn't work. I use Atom with Xcode as compiler on my Mac.

This pops up:

dyn3180-214:~ joshua$ /var/folders/y6/jrfdjlsx0nqcxyfvsrfw6bnw0000gn/T/excersice ; exit;
file was not opened correctly
logout
#include <iostream>
#include <fstream>
#include <cstdlib>

int main(){
  std::ifstream infile;
  infile.open("numbers.txt");
  if(!infile.is_open()){
    std::cout<<"file was not opened correctly"<<std::endl;
    exit(EXIT_FAILURE);
  }
  int n;
  while(infile>>n){
    std::cout<<n<<std::endl;
  }
  return 0;
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
joshl9688
  • 1
  • 3

3 Answers3

1

Read how to debug small programs and also the documentation of GDB (or of whatever debugger you have on your machine).

Compile your program (that is the C++ file, let's name it josh.cc, shown in your question) with all warnings and debug info. I recommend to compile it on the command line with g++ -Wall -Wextra -g josh.cc -o joshprog (read how to invoke GCC for details) then to debug it with gdb ./joshprog and to run it in a terminal as ./joshprog.

The point is that your IDE (Atom + XCode) is not helping you. It hides you important details (see this answer, it also works for MacOSX or other Unix systems). That is why I strongly recommend compiling (and running, and debugging) your code with explicit commands that you are typing in a terminal emulator for your shell and understanding (so read the documentation of every command that you are using). Once you are fluent with compilation on Unix systems, you might eventually switch to an IDE (but you need to understand what your IDE is doing on your behalf, and what commands it is running).

You could add into your code something displaying the current working directory by using getcwd (which requires #include <unistd.h> on POSIX system), maybe something like

char mycwdbuf[80];
memset (mycwdbuf, 0, sizeof(mycwdbuf));
if (getcwd(mycwdbuf, sizeof(mycwdbuf)-1) != NULL)
   std::cout << "current directory is " << mycwdbuf << std::endl;

By doing that, you'll probably understand how is your program started and in which working directory. I guess that it was not started in the working directory that you wanted it to.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

In Xcode, select the project navigator, then click on your project. This will open the project editor. Select the Build Phases tab. Open the dropdown for Copy Files. Set Destination to Products Directory. Add a Subpath if you have to. Drag the necessary files into the project editor or add them via +.

Example: During the build phase, project files associated with the target will be copied to the designated subpath in the directory within the Products directory.

Sekin
  • 1
  • 1
-1

I think, Xcode generating the .o (object file ) file in different directory, and your .txt is present in different directory. I do code in Xcode, it always generate the .o file in different directory.