[Advice]Simple open text & display code [Plain C]

Soldato
Joined
9 Dec 2006
Posts
9,289
Location
@ManCave
I’m trying to do something very simple but it is not working for me. I’m trying to read a txt file & display the contents on the screen,
I’ve used the example in my book, but don’t work

Error I’m getting: 'fopen' : function does not take 2 arguments

This is my code

Code:
Case 0x3E: //F4
       FILE *fopen(),  *fp;
    int c;
    fp = fopen("log.txt","r");
    c = getc(fp)  ;
    while (c!= EOF)
     {
              putchar(c);
             c =  getc(fp);
 }
     fclose(fp);


I understand it cannot take too arguments, but I can’t see what’s wrong with it the , seem to be in the right places.

Any advice would be appreciated.
 
FILE *fopen(), *fp;

Why is that line like that?

surely "FILE *fp;" is what you want?

otherwise, you're redefining the function which is probably taking precedence over the one in the cstdio header
 
As above your FILE *fopen() line is incorrect.

Try this:

Code:
#include <stdio.h>
#include <stdlib.h> // Needed for exit()

#define FILE_NAME "log.txt"

int main( void )
{
    FILE *file;
    if ( ( file = fopen( FILE_NAME, "r" ) ) == NULL )
    {
        printf( "Error: File not found.\n" );
        exit( EXIT_FAILURE );
    }
    
    char c;
    while( ( c = fgetc( file ) ) != EOF )
    {
        putchar( c );
    }

    fclose( file );
    return ( 0 );
}

Edit: I now notice you have the Case 0x3E: line. Is this in a switch statement? If so, it should be lowercase c i.e "case".
 
Last edited:
Back
Top Bottom