How to create and use input/output files with Xcode C++

Discussion in 'Mac Programming' started by hotglass, Aug 9, 2008.

  1. macrumors newbie

    Joined:
    Aug 9, 2008
    #1
    What application should be used to create an input or output file and how should it be saved for use in Xcode. I am using C++ located in the Command Line Utility.

    Basically, I need to know just about everything related to creating and using an input or output file with C++ in Xcode. Can anyone help me out?

    Thank you
     
  2. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    IOStream Reference.
     
  3. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    If you run your code from the command line (which is in the end the purpose of a command line utility), you will probably redirect input and output, so your program will just read from std::cin and write to std::cout (or use stdin, stdout in C, works in C++ as well). Any other file names would likely be passed in as parameters, so you get the names through argc, argv in main ().
     
  4. thread starter macrumors newbie

    Joined:
    Aug 9, 2008
    #4
    Still unclear on using input/output

    I have an Intro. to programming class, where we use a compiler called Bloodshed with Windows on PCs. Obviously, I have to use a Mac, which doesn't have windows. But, during class, using Bloodshed, all that needs to be done for the compiler to read an input file is to
    1-save a file in Text Editor, then
    2-start a new program and declare the variable inFile as an ifstream data type, then
    3-initialize the file by opening it using the syntax:
    inFile.open(sourcename)


    Is there a way on Xcode use these similar steps for C++? All I know is what I learn on Bloodshed, and then try and use it with Xcode. I
     
  5. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    Well yes. Exactly the same method. That is all standard C++, I linked to a website which describes everything to do with that in my first post on this thread.

    C++, the C++ standard library and the standard template library will be the same across all platforms. So just do it the way you did on Windows.
     
  6. thread starter macrumors newbie

    Joined:
    Aug 9, 2008
    #6
    Here is an example of a program that will not output the data in the inFile

    Thanks for trying to help me out, but I don't think we are on the same page here. This is an example of a sample program that I tried to run.
    The inFile was made in Word for Mac and it contains the number 5.50. When I run this, it is successful, yet there is just a blank output, where I want it to display the number 5.50. Can you make any determinations about the problem based on this source code?
    Thanks


    #include <iostream>
    #include <fstream>
    using namespace std;

    int main (int argc, char * const argv[])
    {
    double dec;
    ifstream inFile;
    inFile.open ("sample.txt");
    inFile >> fixed >> showpoint;

    inFile >> dec;

    return 0;

    }

    This is the output:

    Last login: Sat Aug 9 16:24:36 on ttys003
    /Users/lucasmoderacki/sample/build/Release/sample ; exit;
    macintosh:~ lucasmoderacki$ /Users/lucasmoderacki/sample/build/Release/sample ; exit;
    logout

    [Process completed]
     
  7. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #7
    That is because you haven't told it to output anything.

    Something like this should do it:

    Code:
     
    std::cout << dec << endl;
    
     
  8. macrumors newbie

    Joined:
    Oct 17, 2008
    #8
    I am writing a program that requires me to get info from a file 'Data3.txt'.
    Using assertions I've found out where the problem is, but I'm not sure how to fix it. This is where the error is.

    ifstream inData;
    inData.open ("Data3.txt");
    assert (inData);

    This is the output.

    Assertion failed: (inData), function main, file /Users/stanton/Desktop/Programs/Project-3/Coffey_S_Prj3.cpp, line 36.
    Abort trap
    logout


    I think the problem might be where I'm storing 'Data3.txt'.
    I've tried storing it in various different folders in the Project-3, but I still get the same error. Does anyone know what folder I'm supposed to put Data3.txt in?
     
  9. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #9
    It needs to be in the same directory as the executable based on the code provided. Of course you can put it anywhere you want as long as you provide the correct path to it. At the moment you provide no path so it just assumes it will be in the same place as the executable.
     
  10. macrumors newbie

    Joined:
    Apr 19, 2011
    #10
    So I'm having this same problem now and I can't figure out how to get this to work.

    I tried the solution you gave but I can't get this to work. Can anyone help me?
    (Sorry for posting in such an old thread, I can't seem to figure out how to post a new one.)
     
  11. macrumors 6502a

    Joined:
    Oct 26, 2010
    #11
    You probably are going to have to be more specific, there are a lot of things discussed in this thread. I am not at all sure what your problem is.

    Try posting your code (use the 'code' tags) and you are likely to get far better response as people will know what your question is :)
     
  12. macrumors newbie

    Joined:
    Apr 19, 2011
    #12
    Alright so I did the same thing as the previous guy and I put in what the guy I quoted advised and I got this:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main ()
    {
        double dec;
    	ifstream inFile;
    	inFile.open ("sample.txt");
    	inFile >> fixed >> showpoint;
    	
    	inFile >> dec;
    	cout << dec << endl;
        
        return 0;
    }
    
    My file sample.txt contains the number 5.50 but when I run the program it just says this:

    Last login: Wed Apr 20 12:04:11 on ttys001
    dhcp-155-33-114-131:~ timothywiesner$ /Users/timothywiesner/Documents/infile/build/Debug/infile ; exit;
    0
    logout

    [Process completed]

    I have the infile in the Source folder with the main program. Is the problem simply where I have placed it, or is there some other issue?
     
  13. macrumors 6502

    Joined:
    Mar 8, 2004
    #13
    I ran the test code and it worked fine for me:

    Try making your test file with textedit as a plain text file. If thats not working, try this code which makes the file via the ofstream library.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    void createFile(std::string name, std::string data){
    	
    	std::ofstream outFile;
    	outFile.open(name.c_str());
    	
    	outFile << data;
    	
    	outFile.close();
    	
    }
    
    int main ()
    {
        double dec;
    	std::string fname = "sample.txt";
    	ifstream inFile;
    	
    	createFile(fname, "5.5");
    	inFile.open(fname.c_str());// ("sample.txt");
    	inFile >> fixed >> showpoint;
    	
    	inFile >> dec;
    	cout << dec << endl;
        
        return 0;
    }
    
     
  14. macrumors 603

    Joined:
    Aug 9, 2009
    #14
    Your code works here. With a "sample.txt" file containing 5.50, it prints "5.5", but only when run from the directory where sample.txt is located.


    The example run you showed is not being run from the directory where sample.txt is located:
    Code:
    dhcp-155-33-114-131:[COLOR="Red"]~[/COLOR] timothywiesner$ /Users/timothywiesner/Documents/infile/build/Debug/infile ; exit;
    0
    logout
    
    The red hilited ~ shows the working directory is your home directory. Unless you happen to have a "sample.txt" file there, your program won't read it.

    If you want to know what your program's working directory is when it runs, look up the C function getcwd(). Use it to show what the program actually has, and compare that to what you want it to have.


    As a rule, I advise the use of error checking and error reporting. Right now, you have neither. If you're getting an error you won't know it, and any error that does happen won't have its cause identified. That means one of the few ways to find out what's wrong is to use the debugger to step through the code. But you aren't doing that either.

    You have to do something concrete to debug what's happening (or not happening). Guessing what's wrong is not sufficiently concrete to be a long-term debugging strategy.
     
  15. macrumors newbie

    Joined:
    Jan 12, 2012
    #15
    I have a similar problem.

    I have a file called "decl_ind.txt" in the same folder as the main.cpp file.

    So I was able to get it to read that file and output it's entire contents (the declaration of independence) using the CodeRunner application. However, I'm trying to transition to using xCode, and upon creating a terminal application within Xcode and using the same exact code, I don't get the same readout.

    Here is my source code:

    Code:
    
    //
    //  main.cpp
    //  Assignment4(Charlie)
    //
    //  Created by Anton Notkin on 2/6/12.
    //  Copyright (c) 2012 UC Riverside. All rights reserved.
    //
    
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    using namespace std;
    
    
    int main ()
    {
    
        ifstream inputFile ("decl_ind.txt");
      
        string allText;
        getline(inputFile, allText ,'\0');
        
        
        cout<<allText;
    	
    		cout<<" "<<endl;
    		cout<<" "<<endl;
        
        cout<<"hello"<<endl;
        return 0;
    }
    
    

    and here is the output I get:

    here is what my project hierarchy looks like:
    [​IMG]

    http://cl.ly/3f40101J0q2s3D0v3A3Q
     

Share This Page