Hi all,
I am working on a project that should take all the data from file redirect and write them to a new file. Here is what I have:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "node.h"
#include "tree.h"
#define TRUE 0
#define FALSE 1
#define stdin stdin
#define stdout stdout
#define stderr stderr
int main(int argc,char *argv[])
{
int i;
FILE *fp;
fp = fopen("result.out", "w"); //create file to write stdin data to.
if(fp == NULL) // file validation
{
printf("Could not open file");
return 0;
}
if(argc == 1) // executes if command line has file redirect.
{
printf("Getting data from file redirect. \n");
fprintf (fp, "%s\n", stdin); //Print arguments from redirect to fp.
}
printf("Completed\n");
return 0;
}
This code will execute fine with no errors, but result.out is empty
The file being redirected only contains words separated by spaces.
I also never know the size of the file being redirected.
I just want to copy the data from the file being redirected to result.out.
Any suggestions on how I can improve the code?