help printing text file

This is a discussion on help printing text file within the C Programming forums, part of the General Programming Boards category; Im not really sure why this is not working. My book doesnt really explain how to print a file very ...

  1. #1
    Unregistered
    Guest

    help printing text file

    Im not really sure why this is not working. My book doesnt really explain how to print a file very well. Can anybody help me on why this isnt working. Here is my source code.


    #include <stdio.h>
    #include <stdlib.h>

    int main (void)
    {
    FILE *fpIn;


    fpIn = fopen("P7-03.h", "r");
    if (!fpIn)
    {
    printf("could not open file\a\n");
    exit(101);
    }

    fprintf(fpIn,"%d");
    return 0;
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    The part of opening your file is correct, but the rest not. After you opened the file you can read lines from the file with the fgets function. The fgets function returns a NULL pointer when it reaches end of file. Here's an example:
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    int main (void) 
    { 
      FILE *fpIn = NULL;
      char buffer[1024];
    
      if((fpIn = fopen("P7-03.h", "r")) == NULL)
      { 
        printf("could not open file\a\n"); 
        return -1; 
      } 
    
      while(fgets(buffer, 1024, fpIn) != NULL)
      {
        printf(buffer);
      }
    
      fclose(fpIn);
      return 0; 
    }
    Take a look at the manual page of the fgets function.

  3. #3
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Lightbulb

    Here's another alternative, I just modified your code a little:

    /*Start of code*/

    #include <stdio.h>
    #include <stdlib.h>

    int main (void)
    {
    FILE *fpIn;


    fpIn = fopen("P7-03.h", "r");
    if (!fpIn)
    {
    printf("could not open file\a\n");
    exit(101);
    }
    else { /*Promotes logic safety*/
    while(!feof(fpIn)) { /*To detect the End-of-file flag*/
    fprintf(stdout,"%c", getc(fpIn));
    } /*you have to print it out to the stdout (monitor) not to fpIn*/
    }

    return 0;
    }

    /*End of code*/

    If you try this out, report back if this stinks.
    I have no compiler here in the Internet cafe.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    billholm, this loop of yours:
    Code:
    while(!feof(fpIn)) 
    { 
    	fprintf(stdout,"%c", getc(fpIn)); 
    }
    is wrong. It'll end up writing an extra character to the output. Do it like this:
    Code:
    int c;
    
    while ((c = getc(stdin)) != EOF)
    {
    	putc(c, stdout);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Lightbulb

    Yah that has been a perpetual problem of mine!

    Thanx a lot! Your new loop cured that bug in many of my programs.

    Oh by the way Hammer, why does it come up with an extra character? Isn't it supposed to be just like your alternative loop?
    Tried to figure that out but it still bothers me.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  6. #6
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Bill, the thread below illustrates this well, (as I was recently educated on this topic).

    http://www.cprogramming.com/cboard/s...threadid=18036
    Demonographic rhinology is not the only possible outcome, but why take the chance

  7. #7
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Lightbulb

    Azuth thanks for the link!
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 10:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Problem when printing to text file
    By sissoko in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2002, 05:26 AM

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