can't compile program

This is a discussion on can't compile program within the C Programming forums, part of the General Programming Boards category; Hi all. I've been self-learning C for about 3 weeks now. I am trying to open a file and read ...

  1. #1
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    In my head

    can't compile program

    Hi all. I've been self-learning C for about 3 weeks now.

    I am trying to open a file and read the first 150 characters into a character array, and then
    print the string to the screen.

    Here's what I've done so far:

    #include <stdio.h>
    #include <stdlib.h>
    #define MAXLENGTH 40
    #define STRINGLENGTH 150
    int main(void)
        char stringbuffer[STRINGLENGTH];
        int counter;
        FILE *openedfile;
        char filename[MAXLENGTH];
        printf("Enter a filename to open: ");
        fgets(filename, MAXLENGTH, stdin);
        if ((openedfile = fopen(filename, "r") == NULL)
           puts("Error opening file");
        while (!feof(openedfile))
           for (counter = 0; counter < STRINGLENGTH; counter++)
        return 0;
    But the compiler is giving the error messages:

    18: warning: assignment makes pointer from integer without a cast

    19: parse error before `puts'

    Can someone please explain why I am getting these messages?

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    It is due to a typographical error. This line:
    if ((openedfile = fopen(filename, "r") == NULL)
    should be:
    if ((openedfile = fopen(filename, "r")) == NULL)
    The reason is that the compiler is interpreting the expression as:
    openedfile = fopen(filename, "r") == NULL
    Now, the fopen(filename, "r") == NULL binds tighter than the assignment, but the result of this subexpression is a boolean value, an integer. This temporary integer is then assigned to openedfile, but that mean you are assigning an integer to a pointer without a cast - which is precisely what the "extra" pair of parentheses was supposed to avoid. Then, the compiler goes on to find that there is a missing closing parenthesis, hence the parse error.
    Last edited by laserlight; 12-30-2008 at 02:14 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2001
    I am trying to open a file and read the first 150 characters into a character array,
    Your program does something different. Check the while / for / fgets part.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    And see the FAQ about using feof() to control loops.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Compile Public Domain Pocket PC C Program
    By m1l in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 07-20-2007, 04:02 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. new to C--can't get program to compile
    By samerune in forum C Programming
    Replies: 12
    Last Post: 04-02-2007, 09:44 AM
  5. how do i compile a program that deals w/classes?
    By Shy_girl_311 in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2001, 01:32 AM

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