Xcode Input File Problem

Helloooo C++ forum!

I am new to these forums and new to programming. I only have a single semesters worth of C++ programming under my belt so I don't know too many tricks beyond the basics.

My problem is I have been having difficulty opening and using input files all semester. I am running Xcode on a macbook pro and have been building command-line tools in C++ stdc++.

I have tried unsuccessfully many times at reading an input file into my program. I have used .txt files, .rtf files, I have put the files in every single possible folder in the program folder Xcode creates for you when you start a new project, I have read through my textbook, I have created multiple different programs, and I have looked through the internet.

At the moment I have copied some simple code from this website and am trying to use it, and it still does not work. The code I am using at the moment is...


#include <iostream>
#include<fstream>

using namespace std;

int main ()
{
string line;
ifstream myfile ("seven.rtf");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}

else cout << "Unable to open file";

system("pause");
return 0;
}


This is what the console shows upon running the program, (it compiled without error)...


[Session started at 2012-04-09 19:20:08 -0400.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1515) (Sat Jan 15 08:33:48 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/ttys002
Loading program into debugger…
Program loaded.
run
[Switching to process 23726]
Running…
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural

\f0\fs24 \cf0 seven}
sh: pause: command not found

Debugger stopped.
Program exited with status value:0.


The input file is called seven.rtf and the only thing in the file is the word seven.

I just want to be able to use input files!

Any help would be greatly appreciated!
I just included your addition and it still does not fix the problem. The problem wasn't with the compiling, it was with running the program somehow. Thanks for the help though, anything is appreciated!
Macintoshes (and most UNIXes) don't have a "pause" command. That's more of a Windows thing. But, if you're programming on a Mac, you shouldn't really need it as the console doesn't close when your program is done, does it?

-Albatross
I have used system ("pause") before and it hasn't given me a problem, though after perusing these forums for a while I know it is bad programming.

The problem with the program does not stem from the system ("pause") command, I think, and I may be completely wrong, but I think the program is not reading the input file or finding it. I don't know which folder to put the input file in.


For example, the program should run using Bloodshed Dev C++, the compiler we are using in my class, but it doesn't work on Xcode, which is the program I have to use since I am working on a mac. All programs that do not deal with an input file I have had zero problems transferring between pc's, macs, and compilers.

And again, thanks for all the help, just the fact that people are willing to give advice to some random person on the forum is really great.
An rtf file is not just plain text. It's text, surrounded by markup. See all that output? It's the markup used in an rtf file. See that seven at the end of the output? There's the word "seven". All the rest of it is the rtf markup.

Open the rtf file using a plain text editor (NOT word or wordpad or anything else that interprets the rtf file for you) and you'll see all the markup. You are opening the file, you are reading it all in.

I suggest you switch to actual plain text files for your input files.
Thanks for the advice on the .rtf files.

I found a website giving a tutorial that helped me figure it out. THe address of the website is...

http://kknapp.no-ip.com/Tutorials/Apple/Xcode_CPP/07_io.html
i had the same problem. i use xcode also and all you need to do it go to the file you created and on the right hand side there is a menu for identity and type. change the location to absolute. then copy the full path and put it into the stream name. for example
copied path : /Users/home/Desktop/test/test/File.txt
how to use

ifstream myfile;
myfile.open("/Users/home/Desktop/test/test/File.txt");
//now ur file is open and ready for use.

remember to create files as empty and when naming do name.txt

also this is a PERFECT example of why system commands are bad. the pause command is windows OS specific. because macs are mainly programmed in objective c there are an extremely minimal number of system commands for c++. system commands are generally a bad idea to use and not really an option for xcode. which is a reason i like it better than most other IDE/compilers.
Last edited on
Topic archived. No new replies allowed.