Thread: can't compile program

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

    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:

    Code:
     
    #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++)
               fgets(stringbuffer,STRINGLENGTH,openedfile);
        }
     
        puts(stringbuffer);
     
        fclose(openedfile);
     
        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
    Location
    Singapore
    Posts
    28,413
    It is due to a typographical error. This line:
    Code:
    if ((openedfile = fopen(filename, "r") == NULL)
    should be:
    Code:
    if ((openedfile = fopen(filename, "r")) == NULL)
    The reason is that the compiler is interpreting the expression as:
    Code:
    openedfile = fopen(filename, "r") == NULL
    EDIT:
    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 03:14 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    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
    Location
    Australia
    Posts
    3,459
    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, 02:32 AM