Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hotglass

macrumors newbie
Original poster
Aug 9, 2008
4
0
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
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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 ().
 

hotglass

macrumors newbie
Original poster
Aug 9, 2008
4
0
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
 

Cromulent

macrumors 604
Oct 2, 2006
6,802
1,096
The Land of Hope and Glory
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.
 

hotglass

macrumors newbie
Original poster
Aug 9, 2008
4
0
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]
 

Stan11

macrumors newbie
Oct 17, 2008
8
0
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?
 

Cromulent

macrumors 604
Oct 2, 2006
6,802
1,096
The Land of Hope and Glory
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.
 

Blagiechanga

macrumors newbie
Apr 19, 2011
2
0
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.)
 

ender land

macrumors 6502a
Oct 26, 2010
876
0
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 :)
 

Blagiechanga

macrumors newbie
Apr 19, 2011
2
0
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?
 

Mac_Max

macrumors 6502
Mar 8, 2004
404
1
I ran the test code and it worked fine for me:

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;
}
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
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.
 

element94

macrumors newbie
Jan 12, 2012
3
3
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:

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:
3f40101J0q2s3D0v3A3Q


http://cl.ly/3f40101J0q2s3D0v3A3Q
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.