Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 9, 2008, 11:57 AM   #1
hotglass
macrumors newbie
 
Join Date: Aug 2008
How to create and use input/output files with Xcode C++

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
hotglass is offline   0 Reply With Quote
Old Aug 9, 2008, 12:04 PM   #2
Cromulent
macrumors 603
 
Cromulent's Avatar
 
Join Date: Oct 2006
Location: The Land of Hope and Glory
Quote:
Originally Posted by hotglass View Post
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
IOStream Reference.
__________________
Neural Advance - Mac OS X, UNIX and Windows Development
Last.fm Profile | Extreme Metal Reviews
MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3
Cromulent is offline   0 Reply With Quote
Old Aug 9, 2008, 12:15 PM   #3
gnasher729
macrumors G4
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by hotglass View Post
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
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 ().
gnasher729 is offline   0 Reply With Quote
Old Aug 9, 2008, 02:51 PM   #4
hotglass
Thread Starter
macrumors newbie
 
Join Date: Aug 2008
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
hotglass is offline   0 Reply With Quote
Old Aug 9, 2008, 03:44 PM   #5
Cromulent
macrumors 603
 
Cromulent's Avatar
 
Join Date: Oct 2006
Location: The Land of Hope and Glory
Quote:
Originally Posted by hotglass View Post
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
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.
__________________
Neural Advance - Mac OS X, UNIX and Windows Development
Last.fm Profile | Extreme Metal Reviews
MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3
Cromulent is offline   0 Reply With Quote
Old Aug 9, 2008, 05:27 PM   #6
hotglass
Thread Starter
macrumors newbie
 
Join Date: Aug 2008
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]
hotglass is offline   0 Reply With Quote
Old Aug 9, 2008, 05:52 PM   #7
Cromulent
macrumors 603
 
Cromulent's Avatar
 
Join Date: Oct 2006
Location: The Land of Hope and Glory
That is because you haven't told it to output anything.

Something like this should do it:

Code:
 
std::cout << dec << endl;
__________________
Neural Advance - Mac OS X, UNIX and Windows Development
Last.fm Profile | Extreme Metal Reviews
MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3
Cromulent is offline   0 Reply With Quote
Old Oct 17, 2008, 05:05 PM   #8
Stan11
macrumors newbie
 
Join Date: Oct 2008
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?
Stan11 is offline   0 Reply With Quote
Old Oct 17, 2008, 05:16 PM   #9
Cromulent
macrumors 603
 
Cromulent's Avatar
 
Join Date: Oct 2006
Location: The Land of Hope and Glory
Quote:
Originally Posted by Stan11 View Post
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?
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.
__________________
Neural Advance - Mac OS X, UNIX and Windows Development
Last.fm Profile | Extreme Metal Reviews
MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3
Cromulent is offline   0 Reply With Quote
Old Apr 20, 2011, 12:37 AM   #10
Blagiechanga
macrumors newbie
 
Join Date: Apr 2011
Quote:
Originally Posted by Cromulent View Post
That is because you haven't told it to output anything.

Something like this should do it:

Code:
 
std::cout << dec << endl;
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.)
Blagiechanga is offline   0 Reply With Quote
Old Apr 20, 2011, 01:09 AM   #11
ender land
macrumors 6502a
 
Join Date: Oct 2010
Quote:
Originally Posted by Blagiechanga View Post
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.)
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
ender land is offline   0 Reply With Quote
Old Apr 20, 2011, 11:07 AM   #12
Blagiechanga
macrumors newbie
 
Join Date: Apr 2011
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?
Blagiechanga is offline   0 Reply With Quote
Old Apr 20, 2011, 12:27 PM   #13
Mac_Max
macrumors 6502
 
Join Date: Mar 2004
I ran the test code and it worked fine for me:

Quote:

Program loaded.
run
[Switching to process 1565]
Running…
5.5

Debugger stopped.
Program exited with status value:0.
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;
}
Mac_Max is offline   0 Reply With Quote
Old Apr 20, 2011, 12:29 PM   #14
chown33
macrumors 601
 
Join Date: Aug 2009
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:~ 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.
chown33 is offline   0 Reply With Quote
Old Feb 6, 2012, 10:53 PM   #15
element94
macrumors newbie
 
Join Date: Jan 2012
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:

Quote:

GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Thu Nov 3 21:59:02 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000


hello
[Switching to process 2669 thread 0x0]
Program ended with exit code: 0
here is what my project hierarchy looks like:


http://cl.ly/3f40101J0q2s3D0v3A3Q
element94 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
IPHONE 4 how to JAILBREAK AND UNLOCK bs2511feist Jailbreaks and iOS Hacks 6 Apr 14, 2012 05:52 PM
Changing directory for input/output files in Xcode 3.2 Calhounian Mac Programming 7 Feb 12, 2010 11:21 PM
How to create a text list of file names in a directory? PkennethV Mac Basics and Help 12 Mar 21, 2008 09:39 PM
How to Create a New iTunes Library File ?? fab5freddy Mac Applications and Mac App Store 2 Sep 23, 2007 07:49 AM


All times are GMT -5. The time now is 11:54 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC