Board index » cppbuilder » Wrong abt finished program - why no output?

Wrong abt finished program - why no output?


2003-08-11 02:46:40 AM
cppbuilder30
All messages from thread
Message 1
From Doug_S. ( XXXX@XXXXX.COM )
Subject Can someone help me w/ this program...
Newsgroups comp.lang.c.moderated
Date 2003-08-02 205122 PST
I'm getting NO errors and 2 warnings with this code. I don't have much available to me in learning C, but I'm trying.
The warnings say that "OPEN() and READ() are undeclared - assuming an external is returning integer values."
Can someone here tell me why my console is idle but for the blinking cursor? I've come a long way to give up now.
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)
#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(argc,argv)
int *argv[];
{
char buffer[SIZE]; /* hafta make do w/o int decl */
int fd;
long int n=1;
long count = 0;
int current[52][141]; /* r52c141 array, built-up */
int row; /* from repeat passes of a */
int col; /* current element index */
/* 52r140c printout omits init col - used for its 0's */
for (row = 0; row < 52; row++) /* initialize output array */
{
for(col = 0; col < 141; col++)
current[row][col] = 0;
}
row = 0;
col = 1;
fd = open(argv[1], O_RDONLY | O_TEXT);
/* while (n != CTRL_Z ) /* big loop - begin new dwg date to EOF */
while (n>0 ) /* 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)) != '\n' ) /* skip CR-LF */
{
for ( row = 0; row = 52; row++ )
{
current[row][col] = current[row][col-1]; /* copy prv */
if ( n == row )
{
current[row][col] = current[row][col-1] + 1;
} /* this IS now a match, so increment the copy */
}
}
/* hey - break-out read file into buffer, access data 1, access buffer many */
}
for(row = 0; row < 52; row++)
{
printf("\n");
for (col = 0; col < 140; col++)
printf ("%d ", current[row][col]);
}
/* for(row = 1; row = 52; row++) */
/* { */
/* printf ("\n"); */
/* for (col = 1; col = 140; col++) */
/* printf ("%i ", current[row][col]); */
/* } */
return(0);
}
 
 

Re:Wrong abt finished program - why no output?

"DAS" < XXXX@XXXXX.COM >writes:
Quote
fd = open(argv[1], O_RDONLY | O_TEXT);
open() returns -1 if the file can't be opened. As with every function call,
the return value should be checked before assuming the call succeeded.
 

Re:Wrong abt finished program - why no output?

On 10 Aug 2003 11:46:40 -0700, "DAS" < XXXX@XXXXX.COM >wrote:
[snip]
Quote
for ( row = 0; row = 52; row++ )
[snip]
Your conditional test above is actually an assignment.
 

{smallsort}

Re:Wrong abt finished program - why no output?

On 10 Aug 2003 11:46:40 -0700, "DAS" < XXXX@XXXXX.COM >wrote:
Quote
The warnings say that "OPEN() and READ() are undeclared -
Include the correct header for open() and read() which is <io.h>.
Quote
main(argc,argv)
int *argv[];
{
You're using a very old style for defining main(). Replace the two
lines above with this one:
int main(int argc, char *argv[])
{
 

Re:Wrong abt finished program - why no output?

My God it's a long time since I did C--. Firstly:
add: #include <io.h>// You need that for open &
read
change: main( int, char**argv) // or char*argv[] if you
prefer. You must specify the argument types if you use them
remove line: int *argv[]; // it is not true, and in
the wrong place too.
change: for ( row = 0; row = 52; row++ ) to: for ( row = 0; row <
52; row++ ) // or something equally sensible
That gets rid of compiler errors.
Now on execution, the first read statement swallows the whole file into the
buffer, which you never look at again, and as it returns the number of
characters read, it will never return '\n' unless there just happen to be
ten (or 1034 or 2058 etc.) characters in the file.
You need to read the manual on 'read'. It isn't what you should be using
here anyway. you want formatted stream i/o (fopen & scanf, which is what is
in stdio.h). For example your buffer is now filled with ASCII characters
from the text file, not integer values, ie '0' will be 48, a two digit
integer will occupy two locations, etc. Scanf will convert text to integers
for you. The only trouble with it is that it doesn't differentiate between
spaces and linefeeds, they are all 'whitespace'. If you want to enforce
line synchronisation you have to be a bit more imaginative and industrious.
That's one of the things that was fixed in C++.
That's as far as I'm going for free. If you've got Turbo De{*word*81} or some
such, go step through the program and see what it's doing. Otherwise just
stick temporary cprintf or cputs statements in at regular intervals to
monitor what is going on.
You've still got a way to go, but 'real programmers' never let bugs defeat
them. That's why I've got no hair left.
Tim Jackson
"DAS" < XXXX@XXXXX.COM >wrote in message
Quote


All messages from thread
Message 1
From Doug_S. ( XXXX@XXXXX.COM )
Subject Can someone help me w/ this program...
Newsgroups comp.lang.c.moderated
Date 2003-08-02 205122 PST

I'm getting NO errors and 2 warnings with this code. I don't have much
available to me in learning C, but I'm trying.
The warnings say that "OPEN() and READ() are undeclared - assuming an
external is returning integer values."
Can someone here tell me why my console is idle but for the blinking
cursor? I've come a long way to give up now.

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

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

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)




#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(argc,argv)
int *argv[];
{
char buffer[SIZE]; /* hafta make do w/o int decl */
int fd;
long int n=1;
long count = 0;
int current[52][141]; /* r52c141 array, built-up */
int row; /* from repeat passes of a */
int col; /* current element index */
/* 52r140c printout omits init col - used for its 0's */


for (row = 0; row < 52; row++) /* initialize output array */
{
for(col = 0; col < 141; col++)
current[row][col] = 0;
}
row = 0;
col = 1;



fd = open(argv[1], O_RDONLY | O_TEXT);
/* while (n != CTRL_Z ) /* big loop - begin new dwg date to EOF */
while (n>0 ) /* 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)) != '\n' ) /* skip CR-LF */
{
for ( row = 0; row = 52; row++ )
{
current[row][col] = current[row][col-1]; /* copy prv */
if ( n == row )
{
current[row][col] = current[row][col-1] + 1;
} /* this IS now a match, so increment the copy */
}
}
/* hey - break-out read file into buffer, access data 1, access buffer
many */
}

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

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

return(0);
}



 

Re:Wrong abt finished program - why no output?

On Mon, 11 Aug 2003 01:36:05 +0100, "Tim Jackson" < XXXX@XXXXX.COM >
wrote:
Quote
My God it's a long time since I did C--.
change: main( int, char**argv) // or char*argv[] if you
prefer. You must specify the argument types if you use them
I don't know what the C-- language is or expects <smile>
but for C you can't omit the argument name as you have
done above with argc. You can only do that with C++
(unless the latest C Standard adds it to that language).
In any event, it is unlikely that any C compiler relevant
to this newsgroup supports the omission.
P.S. - Please trim quoted material when replying. Thanks.