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:
just a note, we use gcc compiler
Thanks
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:
Code:
#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