using ifstream and ofstream in xcode

Discussion in 'Mac Programming' started by oicu4202, Sep 5, 2009.

  1. oicu4202 macrumors newbie

    Joined:
    Sep 5, 2009
    #1
    I am writing this C++ program for my class. My problem is that I don't know where to put the files program1.dat and program1.out or know what kind of files they are supposed to be. There are no build errors, and it works in Visual Basic at school, because I know where to put those files and what kind of files they are. I can get it to work in xcode if I declare the variable cm in the program and use cin and cout, but I need to be able to put my data in the file program1.dat and have the program read the data out of that file and output it into the file program1.out I tried putting blank text files with those names everywhere in the subfolders on the side, but none worked. I could put them somewhere else on my computer and access them by using the full file name, but I want them to just be a part of the project. Please help!!!! I don't want to have to use Visual Basic. I;m pretty sure I'm missing something really simple here. The code is below, the last build succeeded.


    #include <iostream>
    #include <fstream>
    #include <iomanip>

    using namespace std;

    int main ()
    {
    int cm, tin, yds, ft, inch, i;

    ifstream inf;
    inf.open ("program1.dat");
    ofstream outf;
    outf.open ("program1.out");

    for (i=1; i<= 10; i++)
    {
    inf >> cm;

    tin = cm/2.54;
    yds = tin/36;
    ft = tin%36/12;
    inch = tin%36%12;

    outf

    << cm << "Cm = "
    << yds << " Yards, "
    << ft << " Feet, "
    << inch << " Inches, "
    << "for the sum of approximately "
    << tin << " Total Inches"
    <<endl;
    }
    }
     
  2. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #2
    If you do it like this, the files have to be in the "current working directory". If you're launching your program from a Terminal, that's the directory you're currently in. For Xcode, it's not that simple.

    You could replace the file names by full paths to the files.

    By the way, there are a few issues with your program - are you sure all these values are integer?
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    Sander is right about the current working directory, etc. You could likely find out what it is by throwing:
    system("pwd");
    in there.

    However, the program has one source file and a couple of dozen lines. Why do you need a full-blown IDE for this? Compiling and running from the terminal would make pathing problems like this go away because you know where to put the data files, since you know where you are running from.

    Also,
    x%36%12
    is always equal to
    x%12

    -Lee
     
  4. oicu4202 thread starter macrumors newbie

    Joined:
    Sep 5, 2009
    #4
    I just did it like my teacher wants, thats why all the values are integers. I've already turned it in, but I want to know for my next project cause I know thats how I'm gonna have to do the input and output like this for this teacher. Like I said, it works on Visual Basic. Where would I put those files in xCode to access them?

    HaHa, can't believe I missed x%36%12 = x%12
     
  5. oicu4202 thread starter macrumors newbie

    Joined:
    Sep 5, 2009
    #5
    I mean, what exactly is the "current working directory"? Is it the same directory the main.cpp file is?
     
  6. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    The following needs to be done for each the Debug and Release settings:

    • Project->Edit Active Executable <Your Current Project Name>
    • Select the View Tab entitled "General"
    • Near the bottom of the view pane note the radio-button group titled "Set the working directory to:".
    • To simplify things for yourself I'd suggest selecting the "Custom directory" radio-button followed by the "Choose..." button (to the right) and select a directory that will contain your input file and where your output file will be created.
     
  7. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #7
    I must say I'm surprised about this requirement. In your program, you're also chaining the rounding errors of two divisions, so your "yards" value may be wrong.

    On the other hand, the "%" operator doesn't like floating point operands, so perhaps he wanted to save function calls for the next lecture.
     
  8. oicu4202 thread starter macrumors newbie

    Joined:
    Sep 5, 2009
    #8
    Ya, I think the whole purpose of this was to show us a for loop, the difference between int and floating point, and how to I/O data using other files. All the math works out on paper, except for 10 cm, which coms out to 3 in, but is closer to 4 because of the int notation, which I think was the teacher's point. I've actually got it working now, thx lloydean. I didn't do it exactly like you said, but your info was very useful.
     
  9. dirtnapper macrumors newbie

    Joined:
    Sep 18, 2009
    #9
    This fixed my problem

    Thanks for this response. It was exactly the fix I needed
     

Share This Page