Printing out contents of a file to the screen

This is a discussion on Printing out contents of a file to the screen within the C Programming forums, part of the General Programming Boards category; Gday all, I'm trying to get all this file processing under control and i am having a bit of trouble. ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    34

    Printing out contents of a file to the screen

    Gday all,

    I'm trying to get all this file processing under control and i am having a bit of trouble.

    I think i can create a file ok and store information into it, but i'm not sure if there is a simple way to print out the contents of a file.
    This is whats happening

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    	FILE *fp;
    	int i;
    
    	if((fp = fopen("clients.dat", "r+w")) == NULL)
    
    		printf("Error in opening the file\n");
    	else
    		printf("FILE IS OPEN\n");
    
    	for (i = 1;i<=10;i++)
    			fprintf(fp, "i = %d\n", i);
    
    
    
    	fclose(fp);
    
    }
    I understand that this creates a file called clients.dat for writing to. Firstly, i havent been able to get the file to open at all.
    Code:
    if((fp = fopen("clients.dat", "r+w")) == NULL)
    This constantly seems to return a NULL value.

    In the event that it would open and the information would be entered through the FOR loop, im not sure what to include in the code which would print out the contents of the file

    e.g.

    FILE IS OPEN
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10


    in pretty sure there is a simple way to approach this, possibly with the use of fscanf, or fread or something of the like, but i'm just not sure.

    Sorry, but on the topic, when would a function like fseek be used?
    What is its purpose?
    Is it only when dynamic memory is allocated using malloc() and it goes to a certain point of memory in the file?

    Thanks if anyone can help.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    34
    Code:
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
            fputs( buff, stdout );
    could you explain what while ( fgets( buff, BUFSIZ, fp ) != NULL ) is doing, with buff and BIFSIZ, are they standard variable names or user defined?

    Also if you could outline what happens in the fputs( buff, stdout ); line?

    Thanks again.

  3. #3
    xlord
    Guest
    Originally posted by Simon
    Code:
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
            fputs( buff, stdout );
    could you explain what while ( fgets( buff, BUFSIZ, fp ) != NULL ) is doing, with buff and BIFSIZ, are they standard variable names or user defined?

    Also if you could outline what happens in the fputs( buff, stdout ); line?

    Thanks again.
    hehe Well that is easy... it is saing that ...
    whilte fgets/get char/(buff/holds in to array/wich is [BUFSIZ, and fp is your file pointer..


    correct me if im wrong!

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    34
    are you referring to -

    http://www.eskimo.com/~scs/C-faq/top.html?

    or something else?

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    34
    OK, this should be the end.

    So what happens in this:

    Code:
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
            fputs( buff, stdout );
    is that it checks each line and as long as it isn't NULL is prints it to the screen or stdout?? Then it checks the next line and so on?
    Thus the while loop?

    I hope i'm right.

    Thanks for the Link Salem, its a good one.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    stdout.. is your screen.. its your output screen..
    stdin is your input one
    Only the strong survives.

  7. #7
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31
    Code:
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
            fputs( buff, stdout );
    The first line will go through all lines in the file, one at a time, till fgets( ) returns NULL.

    When does fgets( ) return ?
    1) newline character ( \n ) reached.
    2) BUFSIZ - 1 characters have been read.
    3) EOF ( End Of File ).
    4) Error occured while reading.

    The last 2 make fgets( ) return NULL.

    Otherwise it will rturn the pointer to the buffer ( buff ).

    fputs( ) just puts a string on the screen. It will not add any newline character to the end of the string.

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    34
    Originally posted by Simon

    as long as it isn't NULL it prints it to the screen or stdout??
    The screen, (otherwise known as stdout)

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    34
    In this code

    Code:
    #include <stdio.h>
    int main ( ) {
        char buff[BUFSIZ];
        FILE *fp = fopen( "file.txt", "r" );
        if ( fp == NULL ) { /* error */ }
        while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
            fputs( buff, stdout );
        }
        fclose( fp );
        return 0;
    }
    can you let me know why
    Code:
    char buff[BUFSIZ];
    is needed?

    is it just to set a maximum size for fgets to check or something of the like?

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char buff[BUFSIZ];
    ... is an array of type char, size BUFSIZ.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31
    Every array must have a size.

    Code:
    #define BUFSIZ 4096 /* for instance */
    
    ...
    
    char buff[BUFFSIZ];
    fgets( ) needs as its second argument the size of the array where the line read is to be saved. It could also be written as:

    Code:
    while ( fgets( buff, sizeof buff, fp ) != NULL )

  12. #12
    Registered User
    Join Date
    Aug 2002
    Posts
    34
    I posted it before but are buff and bufsiz standard variable names, do they have to be used and are they only used to show the size of the file?

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    BUFSIZ is standard. buf isn't.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Aug 2002
    Posts
    34
    so let me just summarise:

    >>char buf[BUFSIZ];
    needs to be declared because when the compiler sees fgets() it needs to know how big the file is

    >>#define BUFSIZ 4028
    i'm guessing this isn't necessary

    Code:
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
            fputs( buff, stdout );
        }
    in terms of fputs( buff, stdout); , it prints the contents of buff at that point in time to the stdout/screen, and then checks the next line. If the next line doesn't contain a NUll value then it will call fpts() again.

    Thanks again for everyones help and patience.

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>If the next line doesn't contain a NUll value then it will call fpts() again.
    NULL is returned if end-of-file is encountered, or a read error occurs
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  4. Problem With search a file and printing to screen
    By sell682 in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2004, 06:55 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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