|
|||||||
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
#1 |
|
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 |
|
|
|
0
|
|
|
#2 | |
|
Quote:
__________________
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 |
||
|
|
0
|
|
|
#3 | |
|
Quote:
|
||
|
|
0
|
|
|
#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 |
|
|
|
0
|
|
|
#5 | |
|
Quote:
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 |
||
|
|
0
|
|
|
#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] |
|
|
|
0
|
|
|
#7 |
|
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 |
|
|
|
0
|
|
|
#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? |
|
|
|
0
|
|
|
#9 | |
|
Quote:
__________________
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 |
||
|
|
0
|
|
|
#10 | |
|
Quote:
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.) |
||
|
|
0
|
|
|
#11 | |
|
Quote:
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
|
||
|
|
0
|
|
|
#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;
}
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? |
|
|
|
0
|
|
|
#13 | |
|
I ran the test code and it worked fine for me:
Quote:
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;
}
|
||
|
|
0
|
|
|
#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:~ timothywiesner$ /Users/timothywiesner/Documents/infile/build/Debug/infile ; exit; 0 logout 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. |
|
|
|
0
|
|
|
#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: Quote:
http://cl.ly/3f40101J0q2s3D0v3A3Q |
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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.







Linear Mode

