Can someone help me w/ this program... 
Author Message
 Can someone help me w/ this program...

I'm getting 2 errors and one warning with this code.   I don't have
much available to me in learning C, but I'm trying.

My program takes a text file of 5 integers per line, 140-odd lines of
this, and basically puts it into an array.   The lines are separated
by LF-CR, of course.
The integers have to be matched in value with the rows in the array,
and my formatted printout will show 52 rows of integers.   The
integers will be exactly as numerous as the rows in the text file are,
so it will show 52 rows by about 140 columns.

The array rows will be made of integers only increasing in value,
because each of the 5 integers-per-line in the text will be matched
with a given array row according to the integer's actual value, and
these integers repeatedly match and increment the row values as the
rows progress from left-to-right (as read).   Many previous array
values repeat because there is not often a match with the text data.

INPUT:   n1 n2 n3 n4 n5 LFCR
             n6 n7 n8 n9 n10 ....

OUTPUT:
0 0 0 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 (... 140 columns of this ) ...
5 5 5 5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 3 3 3 3 3 3 3 3 3 3 ... 3 3 3
3 4 4 4
(52 rows of this)

My Errors say:

LF undeclared identifier ( I knew this would happen, cuz I don't know
what C calls text-mode LF),
and also  CTRL_Z undeclared identifier.

Warnings were:
Line 18, 'open' undefined
Line 21, 'read undefined,  and
Line 34, 'main' no return value

I DON'T GET THIS STUFF  HELLLLPP!!!

#include <stdio.h>

#include <fcntl.h>                    /* used by open() */

#define SIZE 1024                     /* 140 x 7bytes, incl 1LF */

#define CTRL_Z '\032'                 /* text-mode EOF */

main(argv)

int *argv[];

{

 int buffer[SIZE];

 int fd;

 long int n;

 long count = 0;

 int current[53][141];                /* output array, built-up */

 int row = 1;                         /* from repeat passes of a */

 int col = 1;                         /* current element index */

 fd = open(argv[1], O_RDONLY);

 while (n != CNTL_Z )    /* big loop - begin new dwg date to EOF    */

 { col++;            /* only cols need init 0-buffer: (r[1]c[2] */

       while ( (n = read(fd,buffer,SIZE)) != LF )    /* skip CR-LF  */

           {

                   for ( row = 0; row = 52; row++ ) /* hey! */

                  {

                           current[row][col] = current[row][col-1]; /* copy prev */

                           if ( n == row )                

                  {                            

                   current[row][col] = current[row][col-1] + 1;

                  } /* this IS now a match, so increment element */

                   }

           }

/* hey - break-out read file into buffer, access file 1, buffer many
*/

 }

Quote:
}

--



Thu, 19 Jan 2006 11:51:22 GMT  
 Can someone help me w/ this program...

Quote:

> I'm getting 2 errors and one warning with this code.   I don't have
> much available to me in learning C, but I'm trying.

> My program takes a text file of 5 integers per line, 140-odd lines of
> this, and basically puts it into an array.   The lines are separated
> by LF-CR, of course.
> The integers have to be matched in value with the rows in the array,
> and my formatted printout will show 52 rows of integers.   The
> integers will be exactly as numerous as the rows in the text file are,
> so it will show 52 rows by about 140 columns.

> The array rows will be made of integers only increasing in value,
> because each of the 5 integers-per-line in the text will be matched
> with a given array row according to the integer's actual value, and
> these integers repeatedly match and increment the row values as the
> rows progress from left-to-right (as read).   Many previous array
> values repeat because there is not often a match with the text data.

> INPUT:   n1 n2 n3 n4 n5 LFCR
>              n6 n7 n8 n9 n10 ....

> OUTPUT:
> 0 0 0 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 (... 140 columns of this ) ...
> 5 5 5 5
> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 3 3 3 3 3 3 3 3 3 3 ... 3 3 3
> 3 4 4 4
> (52 rows of this)

> My Errors say:

> LF undeclared identifier ( I knew this would happen, cuz I don't know
> what C calls text-mode LF),
> and also  CTRL_Z undeclared identifier.

> Warnings were:
> Line 18, 'open' undefined
> Line 21, 'read undefined,  and
> Line 34, 'main' no return value

> I DON'T GET THIS STUFF  HELLLLPP!!!

try 'man open'. It tells me (on Debian) which header the function is
prototyped in.

Quote:
> #define SIZE 1024                     /* 140 x 7bytes, incl 1LF */

'SIZE' is such a common name that this is just asking for conflicts. Also,
the comment is mileading. Can't you put this into a local and/or constant
size_t ?

Quote:
> main(argv)
> int *argv[];

FAQ11.4, just fyi.

Quote:
>  int fd;
>  long int n;
>  fd = open(argv[1], O_RDONLY);
>  while (n != CNTL_Z )    /* big loop - begin new dwg date to EOF    */

You're not checking the return-value of open() for failures. Also, since n
is not initialized, it can be anything including CNTL_Z.

Quote:
>  { col++;            /* only cols need init 0-buffer: (r[1]c[2] */

>        while ( (n = read(fd,buffer,SIZE)) != LF )    /* skip CR-LF  */

You don't really have a clue what read() returns, no ? Honestly, Doug, you
have violated one of the biggest rules in programming: making changes
incremental. There is so much in your testprogram that is broken that I
can barely say much about it.
My two cents:
- learn how to use a de{*filter*} and step through the program
- remove everything you don't understand and then start adding features one
by one (did I mention backups ?)

cheers

Uli

--
Questions ?
C++-FAQ Lite: http://www.*-*-*.com/ ++-faq-lite/
C-FAQ http://www.*-*-*.com/ ~scs/C-faq/top.html
--



Fri, 20 Jan 2006 03:49:20 GMT  
 Can someone help me w/ this program...
Doug,

you should use ANSI C functions fopen, fgets and sscanf (or strtol) to
open the file, read it line by line and split each line into five
integers, respectively. You do not need to worry about CR/LF and
EOF-Characters, these functions can handle this. Your program could read
like this:

#include <stdio.h>

int main(int argc, char *argv[])
{

 char line[256];

 FILE *fd;

 long int n[5];

 long count = 0;

 int current[53][141];                /* output array, built-up */

 int row = 1;                         /* from repeat passes of a */

 int col = 1;                         /* current element index */
 int i, rc;

 fd = fopen(argv[1], "r");
 if (fd == NULL)
 {
  fprintf(stderr, "could not open file\n");
  return -1;
 }

 while (fgets(line, sizeof line, fd))    /* big loop - begin new dwg
date to EOF    */
 {
  col++;            /* only cols need init 0-buffer: (r[1]c[2] */

  rc = sscanf(line, "%ld%ld%ld%ld%ld", &n[0], &n[1], &n[2], &n[3],
&n[4]);
  /* test for rc == 5 !*/

  for (i=0; i<5; i++)
  {
   for ( row = 0; row < 52; row++ ) /* hey! */
   {
    current[row][col] = current[row][col-1]; /* copy prev */
    if ( n[i] == row )
    {

     current[row][col] = current[row][col-1] + 1;

    } /* this IS now a match, so increment element */

   }

  }

 /* hey - break-out read file into buffer, access file 1, buffer many
 */

 }

 return 0;

Quote:
}

--



Fri, 20 Jan 2006 03:49:22 GMT  
 Can someone help me w/ this program...
On 03 Aug 2003 03:51:22 GMT

Quote:

> I'm getting 2 errors and one warning with this code.   I don't have
> much available to me in learning C, but I'm trying.

> My program takes a text file of 5 integers per line, 140-odd lines of
> this, and basically puts it into an array.   The lines are separated
> by LF-CR, of course.

C libraries can worry about this for you.

Quote:
> The integers have to be matched in value with the rows in the array,
> and my formatted printout will show 52 rows of integers.   The
> integers will be exactly as numerous as the rows in the text file are,
> so it will show 52 rows by about 140 columns.

> The array rows will be made of integers only increasing in value,
> because each of the 5 integers-per-line in the text will be matched
> with a given array row according to the integer's actual value, and
> these integers repeatedly match and increment the row values as the
> rows progress from left-to-right (as read).   Many previous array
> values repeat because there is not often a match with the text data.

> INPUT:   n1 n2 n3 n4 n5 LFCR
>              n6 n7 n8 n9 n10 ....

> OUTPUT:
> 0 0 0 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 (... 140 columns of this ) ...
> 5 5 5 5
> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 3 3 3 3 3 3 3 3 3 3 ... 3 3 3
> 3 4 4 4
> (52 rows of this)

> My Errors say:

> LF undeclared identifier ( I knew this would happen, cuz I don't know
> what C calls text-mode LF),
> and also  CTRL_Z undeclared identifier.

> Warnings were:
> Line 18, 'open' undefined
> Line 21, 'read undefined,  and
> Line 34, 'main' no return value

> I DON'T GET THIS STUFF  HELLLLPP!!!

> #include <stdio.h>

> #include <fcntl.h>                    /* used by open() */

Why use non-standard stuff when you can use the standard fopen & fgets
functions?

Quote:
> #define SIZE 1024                     /* 140 x 7bytes, incl 1LF */

> #define CTRL_Z '\032'                 /* text-mode EOF */

> main(argv)

> int *argv[];

Old style declaration and incorrect anyway. Use
int main(int argc,char **argv)

- Show quoted text -

Quote:
> {

>  int buffer[SIZE];

>  int fd;

>  long int n;

>  long count = 0;

>  int current[53][141];                /* output array, built-up */

>  int row = 1;                         /* from repeat passes of a */

>  int col = 1;                         /* current element index */

>  fd = open(argv[1], O_RDONLY);

Check that you have a parameter then use fopen.

Quote:
>  while (n != CNTL_Z )    /* big loop - begin new dwg date to EOF    */

check the FAQ. A google search for comp.lang.c FAQ should find it.

Quote:
>  { col++;            /* only cols need init 0-buffer: (r[1]c[2] */

>        while ( (n = read(fd,buffer,SIZE)) != LF )    /* skip CR-LF  */

Use fgets and remember to check if you got the expected line feed.
Anyway, this line does not do what you think it does.

You might also want to look at the data you have read in to buffer.

- Show quoted text -

Quote:
>       {

>               for ( row = 0; row = 52; row++ ) /* hey! */

>              {

>                       current[row][col] = current[row][col-1]; /* copy prev
>                       */

>                       if ( n == row )                

>                   {                            

>                    current[row][col] = current[row][col-1] + 1;

>                   } /* this IS now a match, so increment element */

>               }

>       }

> /* hey - break-out read file into buffer, access file 1, buffer many
> */

>  }

> }

You really need to get a decent C book. I suggest The C Programming
Language 2nd edition by Kernighan & Ritchie
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
--



Fri, 20 Jan 2006 03:49:25 GMT  
 Can someone help me w/ this program...

Quote:
>I'm getting 2 errors and one warning with this code.   I don't have
>much available to me in learning C, but I'm trying.

>My program takes a text file of 5 integers per line, 140-odd lines of
>this, and basically puts it into an array.   The lines are separated
>by LF-CR, of course.
>The integers have to be matched in value with the rows in the array,
>and my formatted printout will show 52 rows of integers.   The
>integers will be exactly as numerous as the rows in the text file are,
>so it will show 52 rows by about 140 columns.

>The array rows will be made of integers only increasing in value,
>because each of the 5 integers-per-line in the text will be matched
>with a given array row according to the integer's actual value, and
>these integers repeatedly match and increment the row values as the
>rows progress from left-to-right (as read).   Many previous array
>values repeat because there is not often a match with the text data.

>INPUT:   n1 n2 n3 n4 n5 LFCR
>             n6 n7 n8 n9 n10 ....

>OUTPUT:
>0 0 0 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 (... 140 columns of this ) ...
>5 5 5 5
>0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 3 3 3 3 3 3 3 3 3 3 ... 3 3 3
>3 4 4 4
>(52 rows of this)

>My Errors say:

>LF undeclared identifier ( I knew this would happen, cuz I don't know
>what C calls text-mode LF),
>and also  CTRL_Z undeclared identifier.

Go back and look at the error message again.  Does it say CTRL_Z or
CNTL_Z?  The difference is significant.

- Show quoted text -

Quote:

>Warnings were:
>Line 18, 'open' undefined
>Line 21, 'read undefined,  and
>Line 34, 'main' no return value

>I DON'T GET THIS STUFF  HELLLLPP!!!

>#include <stdio.h>

>#include <fcntl.h>                    /* used by open() */

>#define SIZE 1024                     /* 140 x 7bytes, incl 1LF */

>#define CTRL_Z '\032'                 /* text-mode EOF */

This defines a macro named CTRL_Z.  Can you find any place this name
is used?

Quote:

>main(argv)

>int *argv[];

>{

> int buffer[SIZE];

Since this is the input buffer for a text stream, you probably want
char instead of int.

- Show quoted text -

Quote:

> int fd;

> long int n;

> long count = 0;

> int current[53][141];                /* output array, built-up */

> int row = 1;                         /* from repeat passes of a */

> int col = 1;                         /* current element index */

> fd = open(argv[1], O_RDONLY);

> while (n != CNTL_Z )    /* big loop - begin new dwg date to EOF    */

This references something named CNTL_Z.  Can you find any place the
name is defined?

n currently is uninitialized.  It's value is indeterminant.  You
cannot reference the value of an uninitialized variable; it invokes
undefined behavior.

Quote:

> { col++;            /* only cols need init 0-buffer: (r[1]c[2] */

>       while ( (n = read(fd,buffer,SIZE)) != LF )    /* skip CR-LF  */

What did you intend LF to represent?  From your description, it
appears you want the line feed that separates records in a stream.
Most special characters like that are represented in C using escape
sequences (e.g., '\0', '\a', etc.).  Check you manual.

What does the value read() returns represent?  It is not a standard C
function (I think it's POSIX but I'm not sure).  I am reasonably
certain it does not return the integer value of the text in the input
stream.  It is probably the count of the number of characters read in.

Quote:

>       {

>               for ( row = 0; row = 52; row++ ) /* hey! */

The first time through this loop, row is 0 which is not equal to 52 so
the loop exits immediately.  You need to look up the syntax of the for
statement in your reference.

Quote:

>              {

>                       current[row][col] = current[row][col-1]; /* copy prev */

The value of current[row][col-1] is also uninitialized.

Quote:

>                       if ( n == row )                

You need to determine how to extract the values you are interested in
from the current record you have read.  Look up sscanf or strtol.
Once you do that, you will need to synchronize your array indices with
the data you are processing.

- Show quoted text -

Quote:

>                  {                            

>                   current[row][col] = current[row][col-1] + 1;

>                  } /* this IS now a match, so increment element */

>               }

>       }

>/* hey - break-out read file into buffer, access file 1, buffer many
>*/

> }

>}

<<Remove the del for email>>
--



Sat, 21 Jan 2006 00:11:10 GMT  
 Can someone help me w/ this program...

[snip]

Quote:
>>                   for ( row = 0; row = 52; row++ ) /* hey! */

                                      ^
     "I'm lonely!"

Quote:
>The first time through this loop, row is 0 which is not equal to 52 so
>the loop exits immediately.  You need to look up the syntax of the for
>statement in your reference.

     GOTCHA!  The first time through this loop, row is 0.  It is then
assigned 52.  This will happen each iteration.  Since 52 is true, the
loop never terminates (barring a break or a return or some undefined
behaviour (quite likely in this program)).

     Why C is not my favourite programming language.

[snip]

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
     I have preferences.
     You have biases.
     He/She has prejudices.
--



Sun, 22 Jan 2006 05:26:20 GMT  
 Can someone help me w/ this program...
I'm having fun with your suggestions, board.   Of course I have
downloaded the C FAQ, but not the C++ link provided by  Ulrich
Eckhardt, because I am having trouble with SAM's IBM C Primer ++
(copyright 1986!!!)   GET IT!??  I'm dumb!

I ***REALLY*** like help in the form of Message 3 : Ulrich Ottersbach.
  The more toys I have to play with, the quicker useful things can
soak in.   I still can't get any outpout from Ottersbach's program,
tho.
Remember ol' QBASIC?   That's the speed I should be learning at,
building-block-by-building-block, not whole sheets of simulated wood
siding!
I'll just lean those sheets up against each other and cinch it with
duct tape and I''l have what lloks like a house from a distance - a
tumble-down shanty from inside!
Sorry I can't do anything myself to add pizzazz to this board, folks,
I gotta use bricks.

I'm gonna start by responding to Schwarz's posting, since that is the
last one I have on my screen.   It's 8pm ET, and my connection says
his posting (9 this AM) is THE LATEST on this whole board!  (can that
be right???)

Quote:
>> while (n != CNTL_Z )    /* big loop - begin new dwg date to EOF  
*/

> This references something named CNTL_Z.  Can you find any place the
> name is defined?

> n currently is uninitialized.  It's value is indeterminant.  You
> cannot reference the value of an uninitialized variable; it invokes
> undefined behavior.

Nopers, I wrote it wrong.   I will use CTRL_Z, as declared.

Do I hafta initialize n?   It won't equal CTRL_Z until EOF, and C
isn't going to sabotage me with a /032' right off-the-bat, right?

Quote:
>> int buffer[SIZE];

> Since this is the input buffer for a text stream,
> you probably want char instead of int.

I didn't think this would work, but all data is integer, so I guess I
need integer storage for it.
Yopu think THIS is bad?   I have taken college courses in C, BASIC,
PL/I and fortran, and read several books on beginner programming cover
to cover (per the instruction) and I ain't never seen an explanation
for pointers that works for me!  Memory is a black box, unless you use
low-level statements to access it.   Using pointers is like using a
black finger to explore a black background.   I got a lot of other
work to do before I move on to casting data types like I'm the friggin
boss.

Quote:

>> { col++;            /* only cols need init 0-buffer: (r[1]c[2] */

>>       while ( (n = read(fd,buffer,SIZE)) != LF )    /* skip CR-LF
*/

> What did you intend LF to represent?  From your description, it
> appears you want the line feed that separates records in a stream.
> Most special characters like that are represented in C using escape
> sequences (e.g., '\0', '\a', etc.).  Check you manual.

LF is a linefeed.   I'm guessing C understands my meaning only in
terms of /r' - Carriage Return.

Quote:
> What does the value read() returns represent?  It is not a standard C
> function (I think it's POSIX but I'm not sure).  I am reasonably
> certain it does not return the integer value of the text in the input
> stream.  It is probably the count of the number of characters read in.

fd is the file handle for the datafile argument I enter into the
command-line at execution.  "buffer" tells read where to put the data,
in chunks of bytes described in SIZE.   It returns my text data file
bytes (integers) until it returns 0 at EOF.

Quote:
>>     for ( row = 0; row = 52; row++ ) /* hey! */

> The first time through this loop, row is 0 which is not equal to 52 so
> the loop exits immediately.  You need to look up the syntax of the for
> statement in your reference.

???? how else to do a standard ANSI for-loop (this IS C', right?):  
    for (row=initial_value; ("TO") row=final_value; increment-by-ones.

Quote:
>>      current[row][col] = current[row][col-1]; /* copy prev */

>The value of current[row][col-1] is also uninitialized.

At the top of the file, I have:    int current[53][141];
C is supposed to not fill it with garbage.   Maybe I should say:
  int current[53][141]=0;        thus setting everything to 0???

Quote:
> You need to determine how to extract the values you are interested in
> from the current record you have read.  Look up sscanf or strtol.
> Once you do that, you will need to synchronize your array indices with
> the data you are processing.

You are one not-kiddin' dude.

I like Ulrich Ottersbach s work.   It runs without a glitch and it
looks so CLEAN.

BUT,....

Quote:
>> fd = fopen(argv[1], "r");
>> if (fd == NULL)
>> {
>>  fprintf(stderr, "could not open file\n");
>>  return -1;
>> }

>> while (fgets(line, sizeof line, fd))  /* bigloop - read records to

EOF */
     How the heck does while execute, when there is no test value to
tell it when to quit???!!!    Very weird.

Also, can I tack a little nested printing loop onto the end of his
code to check output like I did below?   His code terminates fine, but
my printout is an infinite loop, so I dunno what he is generating:

... (previous code snipped)
    } /* this IS now a match, so element is incremented */
   }
  }

 }

   for (row=1; row=52; row++)                      /* my fruity-loops
begin here  */
      {                        
        printf ("\n");      
        for (col=1; col=140; col++)
           {            
            printf ("%ld ", current[row][col]);
        }      
    }    

 return 0;

Quote:
}

full code follows:

#include <stdio.h>

int main(int argc, char *argv[])
{
 char line[5];                      /* mboard gave this 256 */
 FILE *fd;
 long int n[5];
 long count = 0;
 int current[53][141];                /* output array, built-up */
 int row = 1;                         /* from repeat passes of a */
 int col = 1;                         /* current element index */
 int i, rc;

 fd = fopen(argv[1], "r");
 if (fd == NULL)
 {
  fprintf(stderr, "could not open file\n");
  return -1;
 }

 while (fgets(line, sizeof line, fd))    /* big loop - begin new
records, to EOF  */
 {
  col++;                                       /* only cols need init
0-buffer (r[1]c[2] */

  rc = sscanf(line, "%ld%ld%ld%ld%ld", &n[0], &n[1], &n[2], &n[3],
&n[4]);
                                         /* test for rc == 5 !*/

  for (i=0; i<5; i++)                    /* I guess C 'for' loops are
bottom-test */
  {
   for ( row = 0; row < 52; row++ )    
   {
    current[row][col] = current[row][col-1];   /* just copy prev value
*/
    if ( n[i] == row )
    {

     current[row][col] = current[row][col-1] + 1;

    } /* this IS now a match, so element is incremented */
   }
  }

 }

   for (row=1; row=52; row++)
      {                        
        printf ("\n");      
        for (col=1; col=140; col++)
           {            
            printf ("%ld ", current[row][col]);
        }      
    }    

 return 0;

Quote:
}

--



Sun, 22 Jan 2006 05:26:28 GMT  
 Can someone help me w/ this program...
On 03 Aug 2003 19:49:22 GMT, "Ulrich Ottersbach"

Quote:

>Doug,

>you should use ANSI C functions fopen, fgets and sscanf (or strtol) to
>open the file, read it line by line and split each line into five
>integers, respectively. You do not need to worry about CR/LF and
>EOF-Characters, these functions can handle this. Your program could read
>like this:

>#include <stdio.h>

>int main(int argc, char *argv[])
>{

> char line[256];

> FILE *fd;

> long int n[5];

> long count = 0;

> int current[53][141];                /* output array, built-up */

> int row = 1;                         /* from repeat passes of a */

> int col = 1;                         /* current element index */
> int i, rc;

> fd = fopen(argv[1], "r");
> if (fd == NULL)
> {
>  fprintf(stderr, "could not open file\n");
>  return -1;
> }

> while (fgets(line, sizeof line, fd))    /* big loop - begin new dwg
>date to EOF    */
> {
>  col++;            /* only cols need init 0-buffer: (r[1]c[2] */

>  rc = sscanf(line, "%ld%ld%ld%ld%ld", &n[0], &n[1], &n[2], &n[3],
>&n[4]);
>  /* test for rc == 5 !*/

>  for (i=0; i<5; i++)
>  {
>   for ( row = 0; row < 52; row++ ) /* hey! */
>   {
>    current[row][col] = current[row][col-1]; /* copy prev */

This still invokes undefined behavior since current[0][1] (the
variable to be copied on the first iteration of the loop) was never
initialized.

- Show quoted text -

Quote:
>    if ( n[i] == row )
>    {

>     current[row][col] = current[row][col-1] + 1;

>    } /* this IS now a match, so increment element */

>   }

>  }

> /* hey - break-out read file into buffer, access file 1, buffer many
> */

> }

> return 0;
>}

<<Remove the del for email>>
--



Sun, 22 Jan 2006 05:26:30 GMT  
 Can someone help me w/ this program...

snip

Quote:

>>> while (n != CNTL_Z )    /* big loop - begin new dwg date to EOF  
>*/

>> This references something named CNTL_Z.  Can you find any place the
>> name is defined?

>> n currently is uninitialized.  It's value is indeterminant.  You
>> cannot reference the value of an uninitialized variable; it invokes
>> undefined behavior.

>Nopers, I wrote it wrong.   I will use CTRL_Z, as declared.

>Do I hafta initialize n?   It won't equal CTRL_Z until EOF, and C
>isn't going to sabotage me with a /032' right off-the-bat, right?

If you don't initialize n, how do you know it won't equal CTRL_Z
immediately.  C does not initialize automatic variables.  It could
have any value including CTRL_Z.  If you don't initialize it, you
invoke undefined behavior.  Whether you have to initialize it depends
on your tolerance for this behavior, which is not required to be
consistent.

Quote:

>>> int buffer[SIZE];

>> Since this is the input buffer for a text stream,
>> you probably want char instead of int.

>I didn't think this would work, but all data is integer, so I guess I
>need integer storage for it.

Your initial program description strongly implies that your file
contains text.  It is extremely unusual for a binary file containing
integers to have lines and for those lines to be separated by LF/CR
sentinels.

snip

Quote:

>>> { col++;            /* only cols need init 0-buffer: (r[1]c[2] */

>>>       while ( (n = read(fd,buffer,SIZE)) != LF )    /* skip CR-LF
>*/

>> What did you intend LF to represent?  From your description, it
>> appears you want the line feed that separates records in a stream.
>> Most special characters like that are represented in C using escape
>> sequences (e.g., '\0', '\a', etc.).  Check you manual.

>LF is a linefeed.   I'm guessing C understands my meaning only in
>terms of /r' - Carriage Return.

You need to use '\r'.

Quote:

>> What does the value read() returns represent?  It is not a standard C
>> function (I think it's POSIX but I'm not sure).  I am reasonably
>> certain it does not return the integer value of the text in the input
>> stream.  It is probably the count of the number of characters read in.

>fd is the file handle for the datafile argument I enter into the
>command-line at execution.  "buffer" tells read where to put the data,
>in chunks of bytes described in SIZE.   It returns my text data file
>bytes (integers) until it returns 0 at EOF.

See, your file is text.

Quote:

>>>     for ( row = 0; row = 52; row++ ) /* hey! */

>> The first time through this loop, row is 0 which is not equal to 52 so
>> the loop exits immediately.  You need to look up the syntax of the for
>> statement in your reference.

>???? how else to do a standard ANSI for-loop (this IS C', right?):  
>    for (row=initial_value; ("TO") row=final_value; increment-by-ones.

As Gene pointed out, I misread your code.  The middle term is an
assignment, not a test for equality.  What I think you really want is
    for (row = 0; row < 52; row++)

Given that current contains 53 elements, you may want < 53 or <= 52
but that is not the major point.  You need to look up what the
expressions in a for statement mean.  Any book should do since it
hasn't changed in over 25 years.

Quote:

>>>      current[row][col] = current[row][col-1]; /* copy prev */

>>The value of current[row][col-1] is also uninitialized.

>At the top of the file, I have:    int current[53][141];
>C is supposed to not fill it with garbage.   Maybe I should say:
>  int current[53][141]=0;        thus setting everything to 0???

C is not supposed to fill it at all, including not filling it with
zeros.  The values of uninitialized variables are indeterminant and
you cannot reference them.

If you use {0}, it will set everything to 0 and eliminate this
problem.

- Show quoted text -

Quote:

>> You need to determine how to extract the values you are interested in
>> from the current record you have read.  Look up sscanf or strtol.
>> Once you do that, you will need to synchronize your array indices with
>> the data you are processing.

>You are one not-kiddin' dude.

>I like Ulrich Ottersbach s work.   It runs without a glitch and it
>looks so CLEAN.

>BUT,....

>>> fd = fopen(argv[1], "r");
>>> if (fd == NULL)
>>> {
>>>  fprintf(stderr, "could not open file\n");
>>>  return -1;
>>> }

>>> while (fgets(line, sizeof line, fd))  /* bigloop - read records to
>EOF */
>     How the heck does while execute, when there is no test value to
>tell it when to quit???!!!    Very weird.

Of course there is a test value.  (If there weren't, your compiler
would generate a diagnostic.)  fgets returns a value.  The value
happens to be a pointer.  If the value is NULL, then that is
considered false and the while loop terminates.  NULL happens to be
the value fgets will return when it detects EOF.  Any value other than
NULL is considered true and the loop continues.

You really need to get a book.

Quote:

>Also, can I tack a little nested printing loop onto the end of his
>code to check output like I did below?   His code terminates fine, but
>my printout is an infinite loop, so I dunno what he is generating:

>... (previous code snipped)
>    } /* this IS now a match, so element is incremented */
>   }
>  }

> }

>   for (row=1; row=52; row++)                      /* my fruity-loops
>begin here  */

The middle term insures that your loop never terminates.  In addition
to assigning 52 to row, the expression always evaluates to 52 which is
considered true by the for statement and therefore insures that the
loop always continues for another iteration.

Quote:
>      {                        
>    printf ("\n");      
>        for (col=1; col=140; col++)

Same for loop problem.

Quote:
>           {            
>            printf ("%ld ", current[row][col]);

current[i][j] is an int.  %ld is the format for a long.  If you lie to
printf, you cannot expect it to do what you want.  Either use %d or
cast current to long.

Quote:
>        }      
>    }    

> return 0;
>}

snip

<<Remove the del for email>>
--



Mon, 23 Jan 2006 00:03:08 GMT  
 Can someone help me w/ this program...
On 05 Aug 2003 21:26:28 GMT

<snip>

Quote:
> I'm gonna start by responding to Schwarz's posting, since that is the
> last one I have on my screen.   It's 8pm ET, and my connection says
> his posting (9 this AM) is THE LATEST on this whole board!  (can that
> be right???)

If someone has already shown lots of errors in the program why would
other bother posting more?

Quote:
> >> while (n != CNTL_Z )    /* big loop - begin new dwg date to EOF  
> */

> > This references something named CNTL_Z.  Can you find any place the
> > name is defined?

> > n currently is uninitialized.  It's value is indeterminant.  You
> > cannot reference the value of an uninitialized variable; it invokes
> > undefined behavior.

> Nopers, I wrote it wrong.   I will use CTRL_Z, as declared.

> Do I hafta initialize n?   It won't equal CTRL_Z until EOF, and C
> isn't going to sabotage me with a _/032' right off-the-bat, right?

It it is unititialised then it could equal CTRL_Z. Also, evaluating it
could cause your computer to catch fire or a small army to invade your
house. Evaluating an unititialised automatic variable can cause anything
to happen.

Quote:
> >> int buffer[SIZE];

> > Since this is the input buffer for a text stream,
> > you probably want char instead of int.

> I didn't think this would work, but all data is integer, so I guess I
> need integer storage for it.

You will probably want to store the data in integers. However, it will
be read as characters and you have to do the conversion.

Quote:
> Yopu think THIS is bad?   I have taken college courses in C, BASIC,

I suggest asking for your money back.

Quote:
> PL/I and FORTRAN, and read several books on beginner programming cover
> to cover (per the instruction) and I ain't never seen an explanation
> for pointers that works for me!  Memory is a black box, unless you use
> low-level statements to access it.   Using pointers is like using a
> black finger to explore a black background.

A pointer is used to point somewhere, just like you use your finger to
point somewhere. However, it it points off the edge of a cliff and you
tell your computer to follow it then your computer will be a pile of
bits at the bottom of the cliff, so you have to make sure that pointer
always point somewhere sensible when you use them.

Quote:
> I got a lot of other
> work to do before I move on to casting data types like I'm the friggin
> boss.

It is very rare that you need to use casts.

Quote:
> >> { col++;            /* only cols need init 0-buffer: (r[1]c[2] */

> >>       while ( (n = read(fd,buffer,SIZE)) != LF )    /* skip CR-LF
> */

> > What did you intend LF to represent?  From your description, it
> > appears you want the line feed that separates records in a stream.
> > Most special characters like that are represented in C using escape
> > sequences (e.g., '\0', '\a', etc.).  Check you manual.

> LF is a linefeed.   I'm guessing C understands my meaning only in
> terms of _/r' - Carriage Return.

If you open the file in text mode then C will know how an end of line is
represented.

Quote:
> > What does the value read() returns represent?  It is not a standard
> > C function (I think it's POSIX but I'm not sure).  I am reasonably
> > certain it does not return the integer value of the text in the
> > input stream.  It is probably the count of the number of characters
> > read in.

> fd is the file handle for the datafile argument I enter into the
> command-line at execution.  "buffer" tells read where to put the data,
> in chunks of bytes described in SIZE.   It returns my text data file
> bytes (integers) until it returns 0 at EOF.

You obviously have not read _and_understood_ the documentation. Also you
should stick to the routines provided by the C standard (fopen, fread et
al) unless you need platform specific routines and _understand_ why you
need them. From memory your original program does not need them.

Quote:
> >>     for ( row = 0; row = 52; row++ ) /* hey! */

> > The first time through this loop, row is 0 which is not equal to 52
> > so the loop exits immediately.  You need to look up the syntax of
> > the for statement in your reference.

> ???? how else to do a standard ANSI for-loop (this IS _C', right?):  
>     for (row=initial_value; ("TO") row=final_value; increment-by-ones.

row=52 assigns 52 to row and "returns" 52. To compare row with 52 use
row==52. You need a good C book (I think I said this before) since you
obviously tdo not yet know the basics of the language.

<snip>

Quote:
> I like Ulrich Ottersbach _s work.   It runs without a glitch and it
> looks so CLEAN.

> BUT,....

> >> fd = fopen(argv[1], "r");
> >> if (fd == NULL)
> >> {
> >>  fprintf(stderr, "could not open file\n");
> >>  return -1;
> >> }

> >> while (fgets(line, sizeof line, fd))  /* bigloop - read records to
> EOF */
>      How the heck does while execute, when there is no test value to
> tell it when to quit???!!!    Very weird.

fgets returns a value. Read a book.

<snip>

Quote:
> full code follows:

> #include <stdio.h>

> int main(int argc, char *argv[])
> {
>  char line[5];                      /* mboard gave this 256 */
>  FILE *fd;
>  long int n[5];
>  long count = 0;
>  int current[53][141];                /* output array, built-up */

current is not initialise so it *may* contain anything including the
secret recipe for the coating on KFCs chicken.

- Show quoted text -

Quote:
>  int row = 1;                         /* from repeat passes of a */
>  int col = 1;                         /* current element index */
>  int i, rc;

>  fd = fopen(argv[1], "r");
>  if (fd == NULL)
>  {
>   fprintf(stderr, "could not open file\n");
>   return -1;
>  }

>  while (fgets(line, sizeof line, fd))    /* big loop - begin new
> records, to EOF  */
>  {
>   col++;                                       /* only cols need init
> 0-buffer (r[1]c[2] */

>   rc = sscanf(line, "%ld%ld%ld%ld%ld", &n[0], &n[1], &n[2], &n[3],
> &n[4]);
>                                          /* test for rc == 5 !*/

You have not put in error trapping. What if there are not 5 numbers on
the line?

Quote:
>   for (i=0; i<5; i++)                    /* I guess C 'for' loops are
> bottom-test */
>   {
>    for ( row = 0; row < 52; row++ )    
>    {
>     current[row][col] = current[row][col-1];   /* just copy prev value
> */

*BANG*

current is uninitialised.

Quote:
>     if ( n[i] == row )
>     {

>      current[row][col] = current[row][col-1] + 1;

>     } /* this IS now a match, so element is incremented */
>    }
>   }

>  }

>    for (row=1; row=52; row++)

Learn the difference between = and ==.

Quote:
>       {                        
>    printf ("\n");      
>         for (col=1; col=140; col++)
>            {            
>             printf ("%ld ", current[row][col]);
>         }      
>     }    

>  return 0;
> }

--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
--



Mon, 23 Jan 2006 00:03:39 GMT  
 Can someone help me w/ this program...

Quote:

> I'm having fun with your suggestions, board.   Of course I have
> downloaded the C FAQ, but not the C++ link provided by  Ulrich
> Eckhardt, because I am having trouble with SAM's IBM C Primer ++
> (copyright 1986!!!)   GET IT!??  I'm dumb!

I have that C++ link in my sig by default and forgot to remove it....
However, good books: http://www.accu.org . If you don't find that primer
there, it is probably not worth the paper without the particular system it
was written for.

Quote:
>>>     for ( row = 0; row = 52; row++ ) /* hey! */

>> The first time through this loop, row is 0 which is not equal to 52 so
>> the loop exits immediately.  You need to look up the syntax of the for
>> statement in your reference.

> ???? how else to do a standard ANSI for-loop (this IS C', right?):  
>     for (row=initial_value; ("TO") row=final_value; increment-by-ones.

In a C for loop, you have three statements. The first statement is executed
only once, before entering the loop. The second statement is also executed
and its outcome decides whether the loop's body will be executed another
time. The third is executed after the loop's body was executed.

Back to the above, your problem is that the statement 'row = 52' assigns 52
to the variable row (remember, == is for comparison) and then evaluates
the result of the statement. Since the result is the value of row after
the assignment and that converts to a logical true(because !=0), you will
loop infinitely.

What you want is rather:

unsigned const num_rows = 53;
unsigned const num_cols = 141;
int current[num_rows][num_cols] = {0};

for( row=0; row!=num_rows; ++row)
   ...

Quote:
> At the top of the file, I have:    int current[53][141];
> C is supposed to not fill it with garbage.   Maybe I should say:
>   int current[53][141]=0;        thus setting everything to 0???

see above.

Quote:
>>> while (fgets(line, sizeof line, fd))  /* bigloop - read records to
> EOF */
>      How the heck does while execute, when there is no test value to
> tell it when to quit???!!!    Very weird.

It converts the integral return-value to a boolean true or false, i.e.
everything but zero is true.

One last suggestion: I said that you had taken too many steps without
really understanding them and there is a nice form of remedy for that:
IRC. Many IRC-networks have a #C channel where small misunderstandings can
be solved before they add up to a big one.

happy hacking

Uli
--



Mon, 23 Jan 2006 00:03:42 GMT  
 Can someone help me w/ this program...

Many posters have given you really good advice.  It is not fair to
make them write your code for you.

Doug> Do I hafta initialize n?   It won't equal CTRL_Z until EOF, and C
Doug> isn't going to sabotage me with a /032' right off-the-bat,
Doug> right?

In order to reliably evaluate n, you must first initialize it.  If you
do not first initialize n, how would you know that it is not already
CTRL_Z?  So yes you "hafta" initialize n.

Quote:
>>> int buffer[SIZE];

>> Since this is the input buffer for a text stream,
>> you probably want char instead of int.

Doug> I didn't think this would work, but all data is integer, so I guess I
Doug> need integer storage for it.

You are really reading text.  You will need to determine how to
make that text into separate integers.  Check out functions like fgets
and strtok to get an idea of how to deal with text.

Quote:

>>> { col++;            /* only cols need init 0-buffer: (r[1]c[2] */

>>> while ( (n = read(fd,buffer,SIZE)) != LF )    /* skip CR-LF
Doug> */

>> What did you intend LF to represent?  From your description, it
>> appears you want the line feed that separates records in a stream.
>> Most special characters like that are represented in C using escape
>> sequences (e.g., '\0', '\a', etc.).  Check you manual.

Doug> LF is a linefeed.   I'm guessing C understands my meaning only in
Doug> terms of /r' - Carriage Return.

Help your self and use the C escape sequences where they are
available.

Quote:
>> What does the value read() returns represent?  It is not a standard C
>> function (I think it's POSIX but I'm not sure).  I am reasonably
>> certain it does not return the integer value of the text in the input
>> stream.  It is probably the count of the number of characters read in.

Doug> fd is the file handle for the datafile argument I enter into the
Doug> command-line at execution.  "buffer" tells read where to put the data,
Doug> in chunks of bytes described in SIZE.   It returns my text data file
Doug> bytes (integers) until it returns 0 at EOF.

Consider simplifying your life and using a higher level function like
fgets or fread...  You would need to alter your code.  (I will not
write it for you.) Do not use read unless you have a compelling
reason.  You do not have a compelling reason.  

Quote:
>>> for ( row = 0; row = 52; row++ ) /* hey! */

>> The first time through this loop, row is 0 which is not equal to 52 so
>> the loop exits immediately.  You need to look up the syntax of the for
>> statement in your reference.

Doug> ???? how else to do a standard ANSI for-loop (this IS C', right?):  
Doug>     for (row=initial_value; ("TO") row=final_value; increment-by-ones.

Examine the difference between '=' and '=='.

Quote:
>>> current[row][col] = current[row][col-1]; /* copy prev */

>> The value of current[row][col-1] is also uninitialized.

Doug> At the top of the file, I have:    int current[53][141];
Doug> C is supposed to not fill it with garbage.  

"C" does not "fill it with garbage" the code simply allocates storage
for that symbol.  It is up to you to clean that storage if that is
what you need.  Prior to that storage being allocated to that symbol
that storage likely had some other data stored there.  Until you alter
that storage, the data would still be there.

Doug> Maybe I should say:
Doug>   int current[53][141]=0;        thus setting everything to 0???

Don't guess; know what you want to do.

Quote:
>>> while (fgets(line, sizeof line, fd))  /* bigloop - read records to

Doug> EOF */
Doug>      How the heck does while execute, when there is no test value to
Doug> tell it when to quit???!!!    Very weird.

Sure there is.  Look up fgets.  See what it returns.  See how that
might or might not work as a test value.

Doug> Also, can I tack a little nested printing loop onto the end of his
Doug> code to check output like I did below?  

No, I am not convinced that you can.  Good luck.  

--
-MM
I rarely read email from this address          /"\
because of spam.                               \ /     ASCII Ribbon Campaign
I MAY see it if you put #NOTSPAM#               X      Against HTML Mail
in the subject line.                           / \
--



Mon, 23 Jan 2006 00:05:03 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Help with compiling a "canned" program

2. Someone Please Help Me - I Help You in Any Software Queries/Programming/Coding

3. SOMEONE HELP ME, HELP ME, HELP ME PLEASE...

4. HELP: Could someone help me out with this?

5. Determining what canned preprocessor symbols are available

6. Beginner [Q] Using Canned File Open/Save dialog

7. Canned Dialogs

8. can someone compile this c program for me!

9. Can someone send me a source code on any graphic programming in C

10. I NEED SOMEONE WHO CAN PROGRAM GRAPHICS - MOTIF

11. Can someone please compile this program in VC6 ?

12. I NEED SOMEONE WHO CAN PROGRAM IN C

 

 
Powered by phpBB® Forum Software