how to print a file on the screen

This is a discussion on how to print a file on the screen within the C Programming forums, part of the General Programming Boards category; I am having trouble bring information from a file into a structure and printing it out on the screen. How ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    40

    Question how to print a file on the screen

    I am having trouble bring information from a file into a structure and printing it out on the screen. How is that done? Here is the code I have that has the structure and the open statement. But not sure how to pull the data from the file and print it out.. Is that a plain printf statement?
    Code:
    #include <stdio.h>
       #include <stdlib.h>
         int  num_comp;           /*place holder for number of companies */
         float  num_months;       /*place holder for number of months */
    
    
       struct company{    /*structure to hold the file of company.txt*/
          char city[30];
          float months[10];
          char name[20];
          char manager[20];
        };
    
        struct company *comp;  /*create structure pointer */
    
       int main(int argc, char **argv){
    
           FILE *fp;
    
           char filename[20];
           int choice, sum, counter;
           double average;
           float month;
         comp = (struct company *)malloc(sizeof(struct company));
    
           printf("Please enter the name of the relevant file:  ");
           scanf("%s", &filename);
           fp = fopen("filename", "r");   /*to open a current file*/
           fscanf(fp, "%s", &filename);
    
           printf("1. Enter 1 to get the statistics for a specific month.\n");
           printf("2. Enter 2 to get the overall statistics.\n");
           printf("3. Enter 3 to terminate the program.\n");
            printf("Please enter your choice:\n" );
           scanf("%d", &choice);
    
           switch(choice){
              case 1:
              printf("Which month are you interested in?");
              scanf("%f", &month);
              if (month > 4)
               printf("Sorry, this data is not available, please enter a valid integer from 1 to 4\n");
    
              else
    
               for (counter =1; counter <= 19; counter=counter++)
              {
               average = sum/counter;
              }
               printf("Average sale for month %d: %f \n", month, average);
    
              break;
              case 2:
              case 2:
              printf("The statistics are below: %s\n ", filename);
    
              break;
              case 3:
              printf("Good Bye!\n");
              break;
              default:
              break;
    
             fclose(fp);
             }
          return 0;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Don't cast malloc.

    Code:
    fp = fopen("filename", "r");
    You should check to see if open failed (if it failed, fp will be NULL).

    Code:
    counter=counter++
    All you need is counter++ here. Your code is the same as
    Code:
    counter = counter + 1;  /* or counter++ or ++counter */
    counter = counter;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Your fclose(fp) is unreachable. It will never execute.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    Why won't it execute?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you'd indent properly, maybe you'd see . . .
    Code:
    switch(choice){
          case 2:
              case 2:  // duplicate case: should be an error
              printf("The statistics are below: %s\n ", filename);
    
              break;
          case 3:
              printf("Good Bye!\n");
              break;
          default:
              break;
    
             fclose(fp);
         }
    There's a break before the fclose(). It will never execute.

    Also note the duplicate case.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  3. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21