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?

rproffitt commented: I'll be able to read the include and defines for decades now. +15

Recommended Answers

All 2 Replies

Your code is not formatted so I won't get into it but an obvious omission is the missing fclose(). Take your pick from https://www.google.com/search?q=is+fclose+required%3F

I will not debate bad practices like this. Nor will I give a passing grade to such if submitted as homework.

Part of the formatting problem was that the OP did not use the code tool to post the code. As such all of the lines beginning with # (because they were not indented) were interpreted as MarkDown formatting commands (header lines). I corrected that but did not bother applying proper structure indentation (my time is more valuable than that).

commented: I take it you fixed it. It was rather interesting with screams about include and defines. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.