Passing a text file into Xcode instead of using the command line (C language)

Discussion in 'Mac Programming' started by hollabit, Mar 26, 2014.

  1. hollabit macrumors newbie

    Joined:
    May 16, 2013
    Location:
    Texas
    #1
    I've created a new project as a command line tool. I've also stored it in a folder in my dropbox so that I can access it elsewhere if I need. I don't think this is related to my problem, but more information can't hurt.

    My program will eventually read in a string, and count the frequency of letters. I want it to read in a text file just like you would do it in the command line.
    Ex:
    ./a.out < file.txt

    How do I get Xcode to use this file as the input for my program like the above? I'm using the latest version of Xcode (5.1) and Mac OSX (10.9.2).

    Where my source is stored: /Users/mattholladay/Dropbox/CSCE 1030/Homework 4/Homework 4

    I also have file.txt stored in this directory and I have manually made it my working directory. I'm not sure if this is right though. Should I place it somewhere else? Is it necessary to change the working directory?

    I've searched google and macrumors specifically for help with this problem, but nothing has worked so far. I am a beginner programmer and barely know my way around Xcode (though I'm learning).

    Thanks in advance!
     
  2. drsoong macrumors member

    Joined:
    Mar 24, 2008
    Location:
    Munich
    #2
    istream, i.e. cin and stdin

    My initial thought was that you could parse the command line arguments by hand, check for the "<" and load the another as the filename with an fopen.

    On the other hand, bash does open the file you and passes it on stdin to the calling program. That is what "<" means in bash terms.

    So, I would suggest an input stream reading from standard input, i.e. stdin.

    Look for man istream or on the web for the detailed use of reading from cin (which is stdin).

    As long the is input from standard in, do parse the file for characters.

    I am a little bit rusty on the use of xcode. Xcode places the executable in directories called "debug" and "release", depending on the build target.

    It might be also annoying to have xcode close the terminal the program was called in.

    While that doesn't mean you should learn to build the program on the command line - although it's not difficult -, I would suggest changing in a terminal window into the Projectname/debug directory, copying the file once there, and running it from the terminal.

    NB: The "./" in "./a.out" is important to call an executable in the current directory.

    -drsoong
     
  3. hollabit thread starter macrumors newbie

    Joined:
    May 16, 2013
    Location:
    Texas
    #3
    Hmm... I'm having a hard time understanding most of what you said above. I'm a beginner, please forgive me. :p

    I'd rather not mess with the command line at all if possible. When I was searching before I posted this thread, someone suggested this:

    http://grab.by/vuzu

    but it doesn't work for some reason.

    Also not sure what you mean by "parse by hand".
     
  4. drsoong, Mar 26, 2014
    Last edited: Mar 26, 2014

    drsoong macrumors member

    Joined:
    Mar 24, 2008
    Location:
    Munich
    #4
    This is the correct way to tell Xcode to pass the file to the program. So, if you want to do everything in Xcode, which also has a bit of nasty learning curve, then that's the way.

    In dealing with computers it's dangerous to suppose what think might work at once. Usually, you should then break down the problem into bits you can handle.

    Is the file there? Does Xcode complain about it not being found. Check in finder (or terminal), if it's in the folder it should, most likely the "debug" folder of your project.

    Do you know you put in the right folder - this can be trickier in Xcode than otherwise. But I know it means more hassles for the beginner.

    NB: Try to check in your program, if it's there - but that would make the task harder than is being ask for in the assignment.

    What would that do, or at least would we think it should?

    while(cin >> ch)
    {
    cout << ch << endl;
    }

    Should show something, namely, the file character-wise on each line. (I haven't checked that myself, too lazy for the moment, so this comes off my head...)

    If that is a homework assignment, in principle, "all" (depending on the lecturer) what you need to know, should have been covered in the lectures. Did you revise your notes, and understood them?

    Are the bits of code you could use in an example program to test, if you really know what they do? Does that match reality in executing the example?

    Learning how to "program", although I prefer the term "software development", since there is much more to it, in writing good software. As odd, as it seems that even applies IMHO to the smallest program.

    My motto is, "every good program starts on paper", not in opening your IDE/Texteditor etc. Thinking first, on what you're trying to make the program achieve, breaking that down into steps, might help.

    The downside is that sounds like initially even more work. On the upside, it makes you a better software developer, and it might give you marks on an assignment, even if your actual program has some error.

    I sound like an old man, but it helps.

    Post some code, and error messages that you get. You should have at least something that compiles, even if it does initially not the right thing.

    Then we can work on that further. But it's not instructive, and far less rewarding, to get the code on a silver plate.

    This would mean opening and reading the file with C/C++ methods only, circumventing the convenience of bash which does that for you...
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Post a screenshot of what you see in the "Options" view of that pane.


    Also, please explain exactly what you meant by "manually" in this statement:
     
  6. hollabit thread starter macrumors newbie

    Joined:
    May 16, 2013
    Location:
    Texas
    #6
    @Chown33

    Options: http://grab.by/vuLW

    What I meant by manually was that I went into the options pane and change the working directory to that path (not sure what the default working directory is).

    @drsoong

    Indeed, a design document is part of the grade and I have already written out the steps "on paper" (I technically typed the steps but same basic concept).

    I'm willing (and eager) to deal with the learning curve that comes with Xcode. I've watched portions of the Xcode tutorial on lynda.com so I know the basics of the interface but not much more than that.

    As far as I know, the file is indeed there. I don't know what you mean by the debug folder since there is only one folder at the place where I've saved my project.

    http://grab.by/vuNi

    Xcode also does not tell me anything about whether or not it has found the file. I'm assuming it would print this information somewhere but I don't know where to find that information.

    It looks like you wrote some code in C++ (am I right in saying that?). I'm a beginner programming course and haven't yet learned anything about C++. I have very little experience with '<<' except the words Bit Shift Operator come to mind. (Not entirely sure what that does in C).

    Everything was indeed covered in the lectures for this homework assignment. He explained how to pass the file in with the command line (which I know how to do). Like I posted above, I know that you can pass in a file with the '<' character and then the name of the file. However, I am taking it upon myself to learn to use Xcode because I plan to eventually pursue mac development. Plus I'm really liking Xcode for its code completion and the little warnings it gives. :p

    Anyway, I'm positive that my code itself is fine. I'm not looking for any kind of code, simply knowledge of how to use Xcode. As far as my code is concerned, the input is coming from the standard input device. But with a command in Unix, I can use a file as the input (again, with the '<' character).

    I hope I've given enough explanation and that nothing I said was confusing.
     
  7. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #7
    What the '<' character does is to redirect the file to stdin, it's a shell feature. You can supply arguments with edit scheme, but that's it afaik. Since you have said that you do not want to use the command line, you could just open the file for reading and use the file handle instead.
     
  8. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #8
    First off, you probably shouldn't use an IDE for homework. It's like using a real hammer on a toy peg. Just use a text editor and the command line - there's less magic involved so less confusion when you're learning the basics.

    Next, I suggest dragging your input files to Xcode and adding them as resources that get copied when you run. That way they're copied into the working directory when your program runs (and it's a long path that's automatically created when Xcode runs).

    Alternatively, use a macro that determines the absolute path to your source file (can't be determined at run time since the executable won't know where it's source was, thus why it needs to be a CPP macro, aka #define) and then find the relative path from that to your input file.

    Those are the two ways I've handled getting inputs that I already know where they are into C/C++ command line programs in Xcode.
     
  9. hollabit thread starter macrumors newbie

    Joined:
    May 16, 2013
    Location:
    Texas
    #9
    I'm sincerely thankful for all the help I've received so far (even though my original problem isn't yet resolved).

    @ArtOfWarfare I sincerely appreciate your warning not to use an IDE for my purposes. But I think I'm several mistakes away from heeding that warning. I've sort of fallen in love with Xcode's code completion and checking features. I suppose I could use it only as a text editor and compile it with terminal, and in fact I think I'll give that a try. On the other hand, I'm not sure how to add the input file as a resource like you suggest. Perhaps I'll figure this out as I go along.

    @subsonix I suppose I have a bit more to learn to appreciate what you're saying. I don't know anything about what stdin is (wait a second: epiphany. Is that the shortened version of standard in?) but it looks kind of like what I have to include as a header for each program I write (stdio - standard in/out - is that what that stands for?). Also when you say I can supply arguments with edit scheme, are you talking about Xcode or the command line?

    I appreciate all the patience I've been given so far. I really like this community :)
     
  10. kage207 macrumors 6502a

    Joined:
    Jul 23, 2008
    #10
    Hey man. I really suggest what ArtOfWarfare is saying. I use Xcode for my text editor. Then I use terminal to compile and run. This is what I do for my labs, homework and tutorials (that I do for fun). The reason being is that Xcode has some nice features and I also have Xcode projects open for my iOS development.

    So, I use it when writing C for my homework / labs. Learn terminal. Please, please, please. If you know how to use it, you can almost do anything with computers. It is so powerful and when you learn C you can leverage it to build compilers and more. There are jobs out there that are just Linux/Unix Admins out there because not enough people want to work in a bash environment.

    It's helpful so when you learn AWS (or some other bash web service) you know what you are doing, setting up server, learn how OSes are built and more. Terminal is a must know if you want to be a computer scientist. The ones that say they hate bash (terminal) never learned the power of it or took the time to read the documentation (and sometimes tutorials to help reinforce documentation ideas). Once you understand their documentation, really the power of a computer is yours to take.

    It has helped me administer my own server after I built it to serve my media, backup my computers and setup RAID on it. Know what bash can do for you.

    Here's an article I suggest you read if you want to be a computer scientist; http://matt.might.net/articles/what-cs-majors-should-know/

    It's a really good read.
     
  11. mamcx macrumors regular

    Joined:
    Mar 13, 2008
    #11
    A dedicated place for developers is a best place to find answers. StackOverflow is one very popular, if you are looking for specific questions instead of chatting:

    http://stackoverflow.com/questions/19416047/c-character-count-from-a-file

    Also, find a community dedicated to your language/tool friendly to "noobs".
    -----

    Some advice & answers:

    You #1 task as developer is understand well your problem, and your #2 is know your tools. After "burn" some neurons use Google.

    But because you are starting, spend some quality time reading several tutorials about your language/tool of choice, in parallel with what are your doing. Also, learn the best practiques related to them. Take some time and look for "Tutorial C", "Learn C", "C best practiques", "C common mistakes", "Xcode tutorial", "Using Xcode with C" and things like that. Not worry, #1 & #2 will be with you for the rest of your life, and this kind of questions are asked by experts too.

    Another way to improve your search is sometime like "Best way to do [X] with [Y]", "Most efficient way to do [X] with [Y]", "How [X] work" and similar questions.

    After 17+ years doing this, I still do a lot of google-fu before doing any major/new task or after some time of doing things the same way (looking for improvenments). Despite what this look, this kind of routine start to reduce a lot the time you will dedicate to do any task (of course, after learn how do the proper questions ;)).

    This not work if is running under a shell (ie parsing the <). Also is unnecessary because the shell do that for you ;)

    Think in the shell as a specialized program language. Is very powerfull by itself, in fact, can do what you want:

    http://www.linuxjournal.com/magazine/work-shell-more-fun-word-and-letter-counts

    But sometimes is very criptic the result ;)

    This mean: Is important to learn about the shell and the "Unix way". But not yet.

    Good questions, but are this a problem to complete your task? Will be a problem with large projects, but do the simples thing that work. (If you follow the advice about learning the "proper way" to use a language/tool you will understand this). Also, is best to have separated questions that a stream full of them (http://www.catb.org/esr/faqs/smart-questions.html).

    Use ANY tool you want/know how use. HOWEVER, is valuable to know how do things "The Basic(or manual) Way". But if your task at hand is NOT "learn how do this the basic way" then don't divert your attention.

    But you will need to know how use a IDE, and a Programmer Text Editor and the shell, and eventually will use all of them. But if it keep in your way to finish a task, don't learn about that just now.

    ----

    By the way, the kind of thing you are working on is called "I/O":

    http://en.wikipedia.org/wiki/Input/output

    What the shell is doing, is concatenate Input(s) to output(s), where the inputs/outputs can be programs and/or files.

    In C:

    http://en.wikipedia.org/wiki/C_file_input/output

    This is modeled afther this paradigm:

    http://en.wikipedia.org/wiki/Standard_streams

    Any program that use a standar stream can be connected as I/O to others.

    This is all the information that you will need to fully understand this task.

    Good luck!
     
  12. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #12
    stdin is standard in yes, each process has three file handles stdin, stdout and stderr assigned to them. What your teacher did was reading the file from standard in by redirecting the file. That is an awesome feature of the shell, or command line. If you do not want to use Terminal but Xcode you lose this feature because it's a shell feature. In fact a redirect is implemented by closing stdin and replacing it with the file handle of the file. My point was that you can open the file for reading manually, but it sounds like you just got started so perhaps you did not cover that yet.

    I guess you haven't covered arguments either, it's the argc and argv[] bit in the main function which let's you add arguments that will be passed in to the program. My point was that Xcode will let you add arguments, but that's about it as far as 'shell features' goes.

    ----------

    Except that typical "Hello World" questions are often closed, and asking questions related to a specific problem in your code is also often closed as being "too local".
     
  13. hollabit thread starter macrumors newbie

    Joined:
    May 16, 2013
    Location:
    Texas
    #13
    Indeed, I find Unix and the command line very fascinating. I have no qualms with using it and I intend to learn all of its secrets. However I was simply wanting to compile and run my project the same way I would in the command line, but with only pressing a button. The point is moot now because I've already turned in my assignment. ;)

    I took a look at it, and the things he suggests look daunting. But interesting at the same time. I don't know how I would fit that into 4 years, but it sounds like a challenge that I want to take on. :p

    @mamcx Your reply was very helpful. Indeed, stack overflow has been an amazing help for me so far and has cleared up a lot of questions.

    @subsonix Oh! I was wondering what all that argc argv[] stuff was. Now I know!
     
  14. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #14
    Hello Worlds get closed for being repeats of existing questions, as they should be.

    I'd like to see an example of something you've had closed as being "too local". I have a decent amount of rep on SO and could cast a reopen vote on your behalf if you think something was erroneously closed.

    StackOverflow is one of the best websites on the internet, I think, along with SuperUser (although I hate Meta SO - it's been my impression that everyone there is a jerk).
     
  15. subsonix, Mar 29, 2014
    Last edited: Mar 29, 2014

    subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #15
    Well exactly, so it's perhaps not ideal for newb questions as they are likely repeated versions of already existing questions, not only on stack overflow but elsewhere as well.

    I can't really get an example of anything that has gotten closed of the top of my head, no. But a question that is only related to your own problem is by definition "local" and so is of no interest to others. I have seen many interesting questions that has been closed in general for other inane moderation rules as well.


    Well, it's certainly a good website but, it has it's own particular hive mind, and after hearing a talk by Joe Spolsky how the karma point system is modeled after a Napoleon quote, where he noted that a soldier will fight longer and harder in the battle field if you give him a worthless piece of ribbon, it's quite funny.

    Edit: it's also the idea that everything can be formulated as a simple question that has a simple answer, where on top of that, the best answer can be found by public votes. Many problems are more complex and benefit from a discussion, it may be hard to find "the best" answer, many different approaches may be valid etc.
     
  16. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #16
    Right - it's irritating how many of these pop up on MR, but since we don't have official rules about duplicate threads, and in fact the moderators here frown on old topics being posted in, as if it's important that all posts be chronologically close to each other to a future reader, we have them all the time.

    Feel free to share some examples and we can discuss why it's right that they're closed or vote that they be reopened.

    Hm, I don't feel like that's what it's all about. The idea is that those who are more familiar with how the site is supposed to work, because they've contributed more in the form of questions and answers and review tasks, are given more power to make sure that that is what happens. They have an extremely vast and well moderated set of questions and answers on programming despite the company working with a staff of fewer than 100 people (I forget the exact size of the company) because so much of the community has access to some some set of the moderation tools.

    Generally they are. My code doesn't work and I'm getting an error message that I'm not finding any search results for - please help. Or some loop isn't executing as I'd expect, why not?

    These are generally exploratory questions. IE, "I'm starting a new project, here's the requirements, what's the best technologies to use?" I bring those to MacRumors's forums when I have them - they're too opinion based for StackOverflow. It would be nice if there was some way to allow that kind of question on SO. Maybe I should bring that up on meta, if it hasn't already been.

    The biggest problem with the voting system is that votes last forever. So if something was the best answer from 2009 - 2012, but then a new solution became available and someone posted about it in 2013, the older answer would likely appear first because it has more up votes just by virtue of being older. Further, since it'll get more visibility by being more up voted, it'll probably get more up voted if the answer still works even if it's not the best anymore. I wish they'd come up with some algorithm or something that sorts based on a combination of how recent the answer is and how many up votes it has.
     
  17. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #17
    My only point is that there are reservations for when SO is a good fit or not. Which is why I mentioned it to begin with, interestingly you seems to agree.


    I don't have any interest in having any particular question re-opened. My point is that I quite frequently come across questions that seems arbitrarily closed.

    The "too local" reason that I mentioned earlier, while looking into it a bit more, was called "too localized" it's not used anymore apparently, and one of the reasons mentioned is precisely that it was over used, and no one seemed to agree on what exactly it meant. http://meta.stackoverflow.com/questions/185102/responding-to-your-too-localized-concerns



    Well thats exactly the point. People do that for free, or for "virtual badges" similar to a colored ribbon, It's the same kind of motivator, it's by design which is why the reference was mentioned. It's funny (to me) given the value of a virtual badge as a currency, and how seriously some people take that "job".

    Well yeah, and questions that doesn't only have one correct answer, however you are just affirming my points all along here.
     
  18. kage207 macrumors 6502a

    Joined:
    Jul 23, 2008
    #18
    So, do you know about the up and down arrows to re-execute commands. Then there is also double tab to fill in the rest of the command too.

    Yes, the point might be moot but really don't worry about a button press. Use terminal, it's not bad. :p

    I don't know if it is too daunting as I don't look at it in 4 years. I'm finally going to graduate after 6 years. I took some time after transferring and was in the wrong program (BA not BS). I had to switch into the BS program. But hey that's life.

    Listen, you shouldn't be worried about learning this in 4 years. Look at it in a life long learning process. You should understand the concepts and principles in the 4 years and slowly gain expertise in them over the years as you develop. Don't look at it in time but rather a fun adventure that you'll be doing for the rest of your life.

    Just to let you know, compiler classes are the worst. They are just tough. :(
     
  19. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #19
    Yes. No one "knows UNIX". Everyone knows about exactly what they need to know right now (or maybe they remember a bit that they used to need and don't any more). It's a process. You do something a slow way until it truly impedes your work then you learn about X. Make, awk, screen (some people swear by it, depends on your situation), xargs, od, etc. There's a billion little tools that, when you need them, you end up learning. Trying to learn all billion in, say, alphabetical order, would be fool hardy. My best advice: watch someone that you think is an expert. They will do things differently and you'll have no idea how they did it, then you can ask them. They are not an expert, they just seem like it because they know a little more than you. Pretty soon you'll be using their tricks and need a new expert.

    I have to disagree, compilers was the greatest. I want to take that again now. I was such a poor programmer then that, while I learned a lot in lectures and such, my projects were waaay sub-par (I think I swung an A because of high test scores, but my project was not solid). For me I think automata theory and OS kicked my ass much harder. I can make excuses for why, but it just comes down to my brain not being there yet at 21 or whatever. I passed, but just barely. I still loved them and learned a lot, but determining and proving where a language fell in Chomsky's hierarchy nearly ended me. Those were the days

    -Lee
     
  20. kage207 macrumors 6502a

    Joined:
    Jul 23, 2008
    #20
    I wasn't saying that you learn it all and memorize it. It's more understanding the theory behind everything and remember a few key things (tools and such) that help you accomplish the tasks you need to get done. As you can figure out the details while you work on a task or project.

    Haha. I guess we are opposite. I love OS. We don't have to take automata theory.
     

Share This Page