Re: newbie: can't compile program



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.
.



Relevant Pages

  • Re: problem with function
    ... Yes I did miss type the psz example, it is a pointer to char (not pointer to ... companies using Hungarian, and when I looked into one standard, I found ... >> If you intended copying the string, you need to use the strcpy function. ... > Isn't 'psz' a pointer a an array of characters containing a string? ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Separating directory from file name: Code Review!
    ... > return a null pointer in this example, ... > handed a string that does not contain a '/' character. ... > memory for an array of characters and have it all zeroed, ... > calloc() will do both operations in one call. ...
    (comp.lang.c)
  • Re: difference between string literal declarations
    ... an array of characters. ... The definition for 's' allocates a pointer in addition to the ... storage of the character string "hello, ... 't' only allocates the string characters. ...
    (comp.std.c)
  • Re: char* argv[]
    ... in the second program argv is a pointer to the ... *first element* in an array of characters. ... I didn't claim that C has a string type. ...
    (comp.lang.c)
  • Re: ok characters
    ... > newsgroups devoted exclusively to .NET programming. ... >> however I want to define the string of characters that AREN'T ok ... does the _tcscspn function have ...
    (microsoft.public.vb.general.discussion)