File input/output help! C++ using xcode

Discussion in 'Mac Basics and Help' started by boyongo, Nov 8, 2010.

  1. boyongo macrumors newbie

    Joined:
    Nov 8, 2010
    #1
    Hi everyone Im using xcode in c++ language and I am having trouble finding the place to save my txt file so the program would recognize it.

    This is the code i have.


    #include <iostream>
    #include <fstream>
    using namespace std;
    int main (int argc, char * const argv[]) {
    int x;
    ifstream in;
    in.open("Input.txt");
    if(!in)
    {
    cout<<"\nProgram cannot find the input"<<endl;
    return 1;
    }
    in>>x;
    while (in) {
    cout<<"Number"<<x<<endl;
    in>>x;
    }
    in.close();
    return 0;
    }
    The program displays the cout inside the if so its clearly not finding the txt file.
    If anyone can help me please do.

    Thanks :)
     
  2. dissolve macrumors 6502a

    Joined:
    Aug 23, 2009
    #2
    You should store the directory to the file in a string or char array and access it from there.

    Code:
    const char file[] = "/Users/Home/Desktop/file.txt";
    ifstream in(file);
    if (! in)
         exit(1);
    else
         do stuff
    
    Hope that helps.
     
  3. boyongo thread starter macrumors newbie

    Joined:
    Nov 8, 2010
    #3
    File input/output problem.

    Hey dissolve i tried it but i dont not worked. If it does not bother you perhaps you can be more clearly with the code. Try modifying the code a placed in the thread so i can have a better idea. Sorry for bothering so much.
     
  4. boyongo thread starter macrumors newbie

    Joined:
    Nov 8, 2010
    #4
    Problem solve!

    I solved the problem.

    This is the code i had.

    #include <iostream>
    #include <fstream>
    using namespace std;
    int main (int argc, char * const argv[]) {
    int x;
    ifstream in;
    in.open("Input.txt");
    if(!in)
    {
    cout<<"\nProgram cannot find the input"<<endl;
    return 1;
    }
    in>>x;
    while (in) {
    cout<<"Number"<<x<<endl;
    in>>x;
    }
    in.close();
    return 0;
    }

    The in.open("Input.txt"); part were you put the direction were your txt file is located is wrong the way it should be is:

    in.open("/Users/the name of your computer/name of the file.txt")

    if you have it on your desktop it should be like this:

    ("/Users/the name of your computer/Desktop/name of the file.txt")

    If you have it on a folder in your desktop. After desktop put the name of the folder between / /:

    ("/Users/the name of your computer/Desktop/name of the folder/name of the file.txt").
    And so on.

    Hope this help.
    If not ask in the thread and i will try to help you. :)
     
  5. dissolve macrumors 6502a

    Joined:
    Aug 23, 2009
    #5
    I'm sorry, I should have been more clear. Yes, "Home" is referring to your Home directory. Glad you got it figured out :)
     
  6. Mactrillionaire macrumors regular

    Joined:
    Oct 16, 2010
    #6
    The place you want to save is in the current user's directory "~/" + subfolder, if any.
     

Share This Page