Thread: fprintf and outf

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    15

    fprintf and outf

    I am currently working on a project, and am having trouble understanding part of the code
    A section of this code is:
    Code:
    complex input_complex(FILE *infile)
    {
      complex z = {0,0};
      double re = 0, im = 0;
      char op, ch;
    
      fscanf(infile,"%lf %c%lf%c",&re,&op,&im,&ch);
    
      if (infile != stdin && !good(im,op,ch)) {
        printf("Improperly formed complex number in input file; goodbye\n");
        exit(1);
      }
    	
      while(!good(im,op,ch)) {
        printf("Illegal input for complex number; try again\n");
        fscanf(infile,"%lf %c%lf%c",&re,&op,&im,&ch);
        
      }
      
      z.re = re;
      if (op == '+')
        z.im = im;
      else
        z.im = -im;
      
      return z;
    }
    
    int equal_complex(complex z, complex w)
    {
      return z.re == w.re && z.im == w.im;
    }
    
    double mag_squared(complex z)
    {
      return z.re*z.re + z.im*z.im;
    }
    
    complex scalar_multiple(double c, complex z)
    {
      complex w = ZERO;
      w.re = c*z.re;
      w.im = c*z.im;;
      return w;
    }
    
    complex conjugate(complex z)
    {
      complex w = ZERO;
      w.re = z.re; 
      w.im = -z.im;
      return w;
    }
    
    /*****************************************************************/
    /* FUNCTIONS FOR YOU TO IMPLEMENT - JUST STUBS FOR NOW           */
    /*****************************************************************/
    
    
    void output_complex(FILE *outf,complex z)
    {
      z.re = 2;
      fprintf(outf,"2+2i");
    }
    The top section is what was given and is to not be changed, and the function output_complex is what i have to edit
    and looking at the code that is in output_complex, i am confused with what fprintf(outf,"2+2i"); means, and most importantly what outf specifically means

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ashleyd View Post
    Code:
    void output_complex(FILE *outf,complex z)
    {
      z.re = 2;
      fprintf(outf,"2+2i");
    }
    and looking at the code that is in output_complex, i am confused with what fprintf(outf,"2+2i"); means, and most importantly what outf specifically means
    Look up the syntax for fprintf() in your C library documentation.

    outf is a file handle that's passed into the function.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    is there a way for me to find out exactly what the outf file handle does, because after looking at my documentation it says that the outf writes output to the stream pointed to by outf, which did not help my understanding much

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It's just a variable that C uses to refer to some file. Your output_complex function doesn't care what file it's writing to, only that outf is a valid file handle that can be written to. Here's a tutorial on file handling in C: Cprogramming.com - Tutorials - C File I/O. Google around for more tutorials and try some simple practice problems.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ashleyd View Post
    is there a way for me to find out exactly what the outf file handle does, because after looking at my documentation it says that the outf writes output to the stream pointed to by outf, which did not help my understanding much
    For your puposes ... stream = an open disk file.

Popular pages Recent additions subscribe to a feed

Tags for this Thread