views:

215

answers:

2

Im new in programming c with arrays and files. Im just trying to run the following code but i get warnings like that:

23 44 warning: assignment makes pointer from integer without a cast

53 error: expected expression before ‘char’

Any help? It might be silly... but I cant find what's wrong.

#include <stdio.h>

FILE *fp;
FILE *cw;
char filename_game[40],filename_words[40];

int main()
{
    while(1)
    {
         /* Input filenames. */
            printf("\n Enter the name of the file  \n");
            gets(filename_game);
            printf("\n Give the name of the file2 \n");
            gets(filename_words);

         /* Try to open the file with the game */
            fp=fopen(/* args omitted */);                             //line23**
            if   (fp!= NULL)     
            {  
                printf("\n Successful opening %s \n",filename_game); 
                fclose(fp);
                puts("\n Enter x to exit,any other to continue! \n ");
                if ( (getc(stdin))=='x')
                   break;
                else
                    continue;
            }
            else
            {
                fprintf(stderr,"ERROR!%s \n",filename_game);
                puts("\n Enter x to exit,any other to continue! \n");
                if (getc(stdin)=='x')
                   break;
                else
                    continue;
            }

              /* Try to open the file with the names. */            //line 44**
              cw=fopen(/* args omitted */);
             if   ( cw!=NULL )   
            {  
                printf("\n Successful opening %s \n",filename_words); 
                fclose(cw);
                puts("\n Enter x to exit,any other to continue \n ");
                if ( (getc(stdin))=='x')                         
                   break;                                          //line 53**
                else
                continue;
            }
            else
            {
                fprintf(stderr,"ERROR!%s \n",filename_words);
                puts("\n Enter x to exit,any other to continue! \n");
                if (getc(stdin)=='x')
                   break;
                else
                    continue;
            }
    }   
    return 0;
}
+6  A: 

You are missing parentheses here:

if (fp=fopen("crypt.txt","r")!=NULL)

The != operator has higher precedence than = so the compiler sees the expression like this:

if ( fp = ( fopen("crypt.txt","r") != NULL ) )

fp gets either 1 or 0 depending on whether fopen returned NULL. fp is a pointer and 0/1 is an integer, hence the warning.

You want

if ( ( fp=fopen("crypt.txt","r") ) != NULL )
Potatoswatter
(For the benefit of the OP) It's generally considered bad programming style to mix assignments or other "side effects" into conditional statements like this, and this question is a prime example of why it's bad. If you move the fp=fopen() onto the previous line (outside the if statement) so you are "only doing one thing at a time" it will be much more readable for any level of programmer.
Jason Williams
yes u are correct. But it still has the same warning!
FILIaS
Jason nice tip! Until now i thought that way was the 'good programming style'... thanx for the tip.For the issue, even with the changes, I get the same warnings
FILIaS
@fil: please update with your new code and your new error messages. Also, is there any change in the program's behavior? Besides the compiler warning, those changes should have fixed it to work properly.
Potatoswatter
i get the exactly same warnings, potatoswatter! There's none change in the program's behavior.
FILIaS
@fil: did you save changes?
Potatoswatter
ok im not so beginner! yeap i saved it.
FILIaS
@fil: I'm sorry, but please add some trivial behavior to your program (eg `printf("hello\n");` at the beginning) to verify that changes are taking effect.
Potatoswatter
but it isn't compiled potatosw! I get the same warnings as before!
FILIaS
@fil: Warnings shouldn't prevent it from compiling, unless you have the compiler set to strict mode. Anyway, the line numbers should have changed at least, if you followed Jason's style suggestion.
Potatoswatter
well ok now it;s edited
FILIaS
@fil: Now you have the error message pointing at a comment. I'm sorry, but I give up.
Potatoswatter
that's what the compiler says! ok
FILIaS
A: 

First, it will be much easier for you to debug this problem, and for others to help you, if you try to strip your file down to the minimum example. That aside, if this

fp = fopen( /* args omitted */ );

is giving you a "assignment makes pointer from integer without a cast" warning, then I suspect you don't have a declaration of fopen in scope. Do you by any chance get a warning about an "implict declaration" near the first use of fopen? If so, then you somehow don't have a proper declaration of fopen available, and C automatically decides that it must return an integer.

I don't know why this would be happening, except that the very first line of your example code looks slightly odd:

#include<stdio.h>

Is this really how it appears in your source code? Normally, I'd expect

#include <stdio.h>

I don't recall right now if C should insist on a space between the #include and <stdio.h>.

Dale Hagglund
No i dont get an "implict declaration" warning. That was just an error by copy-paste.Edited
FILIaS
both `include` statements work.
N 1.1