Re: newbie: can't compile program
- From: Barry Schwarz <schwarzb@xxxxxxxx>
- Date: Thu, 1 Jan 2009 13:14:20 -0600 (CST)
On Tue, 30 Dec 2008 17:16:33 -0600 (CST), happyclown
<yeah@xxxxxxxxxxxx> wrote:
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)
First off, you have 3 ( but only 2 )
== binds more tightly than =. Therefore this is parsed as
if ((openedfile = ( fopen(filename, "r") == NULL) )
The relational expression of the right evaluates to either 0 or 1
(both integers) depending on the success of fopen. The expression on
the left is a pointer. You are attempting to assign an integer to a
pointer. You want to write
if ( ( openedfile = fopen(filename, "r") ) == NULL)
puts("Error opening file");
You need to terminate at this point. The subsequent uses of opendfile
result in undefined behavior in its value is NULL.
while (!feof(openedfile))
{
for (counter = 0; counter < STRINGLENGTH; counter++)
fgets(stringbuffer,STRINGLENGTH,openedfile);
This loop will read up to 149 characters 150 times.
}
puts(stringbuffer);
Since all the previous reads went into the same buffer, this will
print only the results of the last read.
fclose(openedfile);
return 0;
}
=========================================
But the compiler is giving the error messages:
1. 18 c:\examples\programs\search~1.c
warning: assignment makes pointer from integer without a cast
2. 19 c:\examples\programs\search~1.c
parse error before `puts'
The missing ) on line 18 completely befuddled the poor compiler.
Can someone please explain why I am getting these messages?
--
Remove del for email
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.
- Prev by Date: Re: Memory alignment
- Next by Date: Re: newbie: can't compile program
- Previous by thread: Re: newbie: can't compile program
- Next by thread: Re: newbie: can't compile program
- Index(es):
Relevant Pages
|