tags:

views:

233

answers:

6
#include <stdio.h>

main(void) {
file *file = fopen("words.txt","r") 
if(file != null) {
    char line[128];
    while(fgets( line, sizeof line, file) != null) 
    {
    fputs ( line, stdout );
    }
    fclose ( file );
    }
}

This is my code. im trying to read a file, and output the content. but this gives me error codes

main.c: In function 'main':
main.c:4: error: 'file' undeclared (first use in this function)
main.c:4: error: (Each undeclared identifier is reported only once
main.c:4: error: for each function it appears in.)
main.c:5: error: parse error before "if"
main.c:7: error: 'line' undeclared (first use in this function)
main.c:7: error: 'null' undeclared (first use in this function)
main.c: At top level:
main.c:13: error: parse error before '}' token
main.c:13:2 warning: no newline at end of file

how do i fixs this errors.

+2  A: 

Try capitalizing FILE.

Ben Alpert
+1  A: 
  • File should be capitalized at the begininning of line 4
  • the end of line 4 is missing a semicolon, leading to the parse problem on line 5

EDITED twice, because I was wrong on the edit. oops.

Rob Lachlan
sizeof is an operator, not a function.
Chris Lutz
Yeah, I just double checked that; my bad.
Rob Lachlan
sizeof doesn't need parentheses here because `line' is a variable. Parentheses aren't needed for variables or expressions, only type names, AFAIK.
Nick Presta
Yeah, again, a total memory failure on my part.
Rob Lachlan
+8  A: 

FILE and NULL are wrongly written (C is case sensitive). fopen line was missing a semicolon.

Code below compiles and runs.

#include <stdio.h>

main(void) {
FILE *file = fopen("words.txt","r"); 
if(file != NULL) {
    char line[128];
    while(fgets( line, sizeof line, file) != NULL) 
    {
    fputs ( line, stdout );
    }
    fclose ( file );
    }
}
diciu
sizeof should have parens: sizeof(line). Much more readable that way.
abelenky
And in C99, main() should be defined returning int.
Jonathan Leffler
abelenky: sizeof line is preferred since sizeof is an operator, not a function. Parentheses are only required around a type, as in 'sizeof (int *)'. Note the space after sizeof, this is also to emphasize that sizeof is an operator. It's really up to personal coding standards, but this is widely used
Chris Young
+1  A: 

The file type is defined in stdio.h and must be all-caps (FILE). Simple mistake to make.

chris.nullptr
+2  A: 

In addition to the other things already pointed out, your main() function should be specified as returning an int and should explicitly do so at the end. Also, I don't quite understand your indentation and bracketing style.

#include <stdio.h>

int main(void)
{
  FILE *file = fopen("words.txt","r"); 
  if(file != NULL)
  {
    char line[128];
    while(fgets( line, sizeof line, file) != NULL) 
    {
      fputs ( line, stdout );
    }
    fclose ( file );
  }
  return 0;
}

Alternatively, you could make it return 1; (error codes are usually non-zero on UNIX) if the file doesn't open:

#include <stdio.h>

int main(void)
{
  FILE *file = fopen("words.txt","r"); 
  if(file != NULL)
  {
    char line[128];
    while(fgets( line, sizeof line, file) != NULL) 
    {
      fputs ( line, stdout );
    }
    fclose ( file );
    return 0;
  }
  else
  {
    return 1;
  }
}
Chris Lutz
+2  A: 

The following works fine and has been cleaned up with consistent braces and spaces in function calls and definitions and loops/ifs. It also works in C, if that was your intent from the tags, and prints an error and returns 1 if the file cannot be opened.

#include <stdio.h>
#include <errno.h>

int main (void) {
    char line[128];
    FILE *file = fopen ("words.txt", "r");
    if (file != NULL) {
        while (fgets (line, sizeof line, file) != NULL) {
            fputs (line, stdout);
        }
        fclose (file);
    } else {
        fprintf (stderr, "Cannot open 'words.txt', error = %d\n", errno);
        return 1;
    }
    return 0;
}
paxdiablo
"Cuddled elses" (i.e. "} else {" on one line) are bad style to some people, but I love them too.
Chris Lutz
I like to see as much of my code as possible on-screen at the same time, so opening braces on the same line as if/while and "cuddled elses" (which is a new term for me) are preferable.
paxdiablo