PDA

View Full Version : Using Stdin/Stdout in terminal




therocket
Nov 16, 2010, 06:26 PM
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");
exit(1);
}
FILE *fp = fopen(argv[1], "r");
if(fp==NULL) { // check if file opened properly
printf("file open failed\n");
exit(2);
}
while(1){ // copy file one character at a time
ch = fgetc(fp);
if(ch == EOF) break;
cnt++;
printf("%c", ch);
}
printf("%d characters copied\n", cnt);
fclose(fp);
return 0;
}


just a note, we use gcc compiler

Thanks



Hansr
Nov 16, 2010, 06:38 PM
Use ./ instead of open and disregard that <. E.g. ./open file2.txt

For output just supply it as a second element in argv

cubist
Nov 16, 2010, 09:16 PM
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.

lloyddean
Nov 16, 2010, 09:24 PM
You'd be better off quoting your assignment word-for-word than paraphrasing it.

therocket
Nov 16, 2010, 09:29 PM
Use ./ instead of open and disregard that <. E.g. ./open file2.txt

For output just supply it as a second element in argv

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,

therocket
Nov 16, 2010, 09:46 PM
You'd be better off quoting your assignment word-for-word than paraphrasing it.

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...:o

lloyddean
Nov 17, 2010, 12:22 AM
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'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!

therocket
Nov 17, 2010, 12:42 AM
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!

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.

lloyddean
Nov 17, 2010, 01:02 AM
(my guess is they dont go to the lectures, haha)

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.

therocket
Nov 17, 2010, 04:09 AM
I'm sure that my be be the case in some instances but more often than not the instructor is the point of failure not any action, or inaction, of the student.

Edit: I'm referring to students whom I've interacted with, not students and instructors in general.

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)

lee1210
Nov 17, 2010, 08:50 AM
For kicks:

cat testdata.in | 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.

-Lee

Hansr
Nov 17, 2010, 10:05 AM
it would be ./file file2.txt

Yeb, I shouldn't post while watching TV :P