Using Stdin/Stdout in terminal

Discussion in 'Mac Programming' started by therocket, Nov 16, 2010.

  1. therocket macrumors member

    Apr 11, 2010
    Hey, Sorry if this is a noob question, but I cant figure it out and I havent found anything that has helped me yet...

    I am taking a beginners C programming course and I need to know how to utilize stdin and stdout in terminal(to use with fopen(), fgetc() ect) to open an executable with a file as the input. In a windows machine, you would simply write "file<file2.txt" in the command prompt to open and executable C program (file) and the input for that program would be file2.txt. However, in terminal on a mac, i found out that I need to type "open file" to open the executable file but I havent found out how to open the executable with an input file. writing "open file<file2.txt" doesnt seem to work
    Also, could you explain what I would need to do to have an output file? again, the cmd prompt in windows uses > but dont know how to do this in terminal

    here is the code if it helps you understand my question better:

    #include <stdio.h>
    #include <stdlib.h>
     int main(int argc, char *argv[]){
      int ch, cnt=0;;
      if(argc<2){ // check number of command line parameters
        printf("error: too few command line parameters\n");
      FILE *fp = fopen(argv[1], "r");
      if(fp==NULL) { // check if file opened properly
        printf("file open failed\n");
      while(1){ // copy file one character at a time
        ch = fgetc(fp);
        if(ch == EOF) break;
        printf("%c", ch);
      printf("%d characters copied\n", cnt);
      return 0;
    just a note, we use gcc compiler

  2. Hansr macrumors 6502a

    Apr 1, 2007
    Use ./ instead of open and disregard that <. E.g. ./open file2.txt

    For output just supply it as a second element in argv
  3. cubist macrumors 68020

    Jul 4, 2002
    Muncie, Indiana
    stdin and stdout are opened by runtime, so you don't need to fopen() it.

    I am guessing that your program is "file.c".

    for input redirection, remove the fopen() section and just use "stdin" in place of your variable "fp".
    program as shown should redirect to output file fine.

    The commands you show are incorrect both for Windows and for Mac. They should be "file <file1.txt" and "file <file1.txt >file2.txt" on both platforms. No "open" anywhere.
  4. lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
    You'd be better off quoting your assignment word-for-word than paraphrasing it.
  5. therocket thread starter macrumors member

    Apr 11, 2010
    Worked like a charm. That is exactly what I was looking for. except to open the executable with the file2.txt it would be ./file file2.txt

    And yes, cubist, the program file is file.c, but then compiled with gcc...

    Thank you so much for your response,
  6. therocket thread starter macrumors member

    Apr 11, 2010
    well, this isnt my assignment. All the info that I asked for is stuff that PC users already know how to do. this is just a sample program from our lecture that takes text from a file, copies it into your command prompt(terminal) and counts the characters that it copies. Our teacher did this in class on his PC laptop with command prompt and I couldnt figure out what the commands were to do it in terminal.

    However, if you want my assignment, I will gladly message it to you, as it would save me a ton of time and I could study for all my finals coming up... hahaha.

    I find it funny how many CompSci assignments that you can find in forums and strangers will gladly do the assignment for the OP. I am not like that though, I know that to learn programming, you need to practice, and practice I will...:eek:
  7. lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
    I've only posted complete code for those that seem totally confused. And only AFTER I know the homework was past due. The reason being is that "I" think a complete solution following a failure can itself be educational, and I find instructors assigning the same homework year after year a very silly thing to do given the internet.

    So sorry, I won't do the homework for you but I will assist as I otherwise see fit according to my perception of the askers personality and abilities.

    Good luck!
  8. therocket thread starter macrumors member

    Apr 11, 2010
    Hahahaha, Great point.

    I didnt think that you would take my offer either. but that is ok, as I am pretty much done my assignment. FYI, I had to modify the program so that it would accept another command line argument to specify an output file.

    Then I had to write a program from scratch that would take an input file full of integers and put the positive integers into one file, and the negative integers into a different file. easy enough, but you cant imagine all the people that cant figure this out(my guess is they dont go to the lectures, haha)

    it was pretty simple. The assignment was just for us to get used to <stdlib.h> and fscanf, fprintf and feof.
  9. lloyddean, Nov 16, 2010
    Last edited: Nov 16, 2010

    lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
    I'm sure that may be be the case in some instances but more often than not the instructor is the point of failure not any action, or inaction, on the part of the student.

    Edit: I'm referring to students whom I've interacted with, not students and instructors in general.
  10. therocket thread starter macrumors member

    Apr 11, 2010
    Yes, I have been told that before. People say that I often take for granted my ability to learn/pick things up quickly. I often forget that not everybody has the same learning styles, or abilities to learn. I can do well in a class with a bad instructor, and very well in a class with a good instructor. But it is true that a bad instructor can make the majority, if not all, of the class bad.
    (Bad, as in not understanding the course material)
  11. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    For kicks:
    cat | awk '{for (i=1; i<=NF;i++) if( $i < 0) system("echo "$i" >&2"); else print $i}' > pos.out 2> neg.out
    This groups 0 with positives.

  12. Hansr macrumors 6502a

    Apr 1, 2007
    Yeb, I shouldn't post while watching TV :p

Share This Page