#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Hi Have a question regarding formatting with getline and fprintf


    I have written a code that reads from a data file then onto the screen in the format of a check and takes the following lines; however, I am unable to get the proper formatting for the check that the assignment asks for. It should look like this.
    ****************************************************************
    * Check NO *
    * Sabre Corporation *
    * 15790 West Henness Lane *
    * New Corio, New Mexico 65790 *
    * *
    but I'm getting this
    [IMG]
    ************************************************************
    * *
    Sabre Corporation
    15790 West Henness Lane
    New Corio, New Mexico 65790
    [/IMG]
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    void printReport(FILE **reportFILE, FILE **inputFILE);
    
    
    #define CHECK          "************************************************************\n" 
    #define CHECKBORDER    "*                                    Check No. %3d         *\n"
    #define CHECKFORMAT    "*                                                          *\n"
    #define COMPANYNAME    "*      %-6s%5s                                   *\n"
    #define COMPANYADDRESS "*      %-5d%5s %7s%5s                     *\n"
    #define CITYSTATE      "* %-6s%-2s*\n"
    #define CHECKFORMAT1   "*      Pay to the Order of                                 *\n"
    #define CHECKNAME      "*      %s                                        *\n"
    
    
    int main(void)
    {
       char *line=NULL;
      char *line2=NULL;
      char *line3=NULL;
      size_t len=61;
      ssize_t bytes_read;
      FILE * inputFILE;
      printReport(&reportFILE, &inputFILE);
    
      fprintf(stdout, CHECK);//prints the top stars on screen for the check
      fprintf(stdout, CHECKFORMAT, number); //prints check no. 100
      bytes_read=getline(&line, &len, inputFILE);
       printf("%s", line);
      bytes_read=getline(&line2, &len, inputFILE);
       printf("%s", line2);
      bytes_read=getline(&line3, &len, inputFILE);
       printf("%s", line3);
    
      fclose(reportFILE);
     
    
      while(getchar()!= '\n');
      getchar;
      return 0;
    }
    
    void printReport(FILE **reportFILE, FILE **inputFILE)
    {
      *reportFILE=fopen("./report.txt","wt");
      if (*reportFILE==NULL)
      {
        printf("Report File open failed...\n");
        exit(-80); //terminate app-reqs <stdlib.h>
      } 
      *inputFILE=fopen("./check.data","r");
      if(*inputFILE==NULL)
      { 
        printf("File cannot be accessed");
        exit(-80);
      }
    }
    What can I do to reorganize the format so it prints the way it should?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481

    Unsatisfied


    Searching for "number" shows one occurrence, and not in a declaration. The program doesn't compile under the syntax and lexical rules of c.
    Code:
    $ gcc -Wall -g -c -o c.o c.c
    c.c: In function ‘main’:
    c.c:26:16: error: ‘reportFILE’ undeclared (first use in this function)
    c.c:26:16: note: each undeclared identifier is reported only once for each function it appears in
    c.c:29:32: error: ‘number’ undeclared (first use in this function)
    c.c:41:3: warning: statement with no effect [-Wunused-value]
    c.c:24:11: warning: variable ‘bytes_read’ set but not used [-Wunused-but-set-variable]
    Combining CHECKFORMAT with a variable first caught my attention. The CHECKFORMAT string doesn't have any specific % format specifiers.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo