Wrong abt semi-finished program - why no output?

Discussion in 'C Programming' started by Blankdraw, Aug 10, 2003.

  1. Blankdraw

    Blankdraw Guest

    I'm getting NO errors and 2 warnings with this code. I thought I was
    ready to write the output-formatting segment, but may be way off the
    mark now. The warnings say that "OPEN() and READ() are undeclared -
    assuming an external is returning integer values." This is not the
    source of the problem.
    Step-Thru causes the system to demand the location of system-sounding
    files like: CHKSTK.ASM, CRT0.C, READ.C. With no real debugger
    feedback, I can't do nuthin. Step-Thru seems to get caught in an
    infinite loop, but I can't figger what its doing.
    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 transforms it into a 2D 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 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;


    /* problem has to be between here and the output loops */

    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 */
    }
    }
    /* shld break-out read file into buffer, access data 1, access buffer
    many */
    }

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


    return(0);
    }
    Blankdraw, Aug 10, 2003
    #1
    1. Advertising

  2. Blankdraw

    John L Guest

    "Blankdraw" <> wrote in message news:...
    > /* 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 */
    > {



    n is a character count, not something you can sensibly test against '\n'
    or ^Z (and in any case, you'd do better using stdio functions).

    John.
    John L, Aug 10, 2003
    #2
    1. Advertising

  3. Blankdraw wrote:
    > I'm getting NO errors and 2 warnings with this code. I thought I was
    > ready to write the output-formatting segment, but may be way off the
    > mark now. The warnings say that "OPEN() and READ() are undeclared -
    > assuming an external is returning integer values." This is not the
    > source of the problem.
    > Step-Thru causes the system to demand the location of system-sounding
    > files like: CHKSTK.ASM, CRT0.C, READ.C. With no real debugger
    > feedback, I can't do nuthin. Step-Thru seems to get caught in an
    > infinite loop, but I can't figger what its doing.
    > 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 transforms it into a 2D 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 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)


    The above specification makes *no* sense. Please try writing it in
    English. Your code, retained at the EOM, is not written in standard C. In
    fact, it is written in K&R C using non-standard functionality.

    Here is a start for what it *appears* you are trying to do:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 1024

    int main(int argc, char *argv[])
    {
    char buffer[SIZE];
    FILE *fd;
    int current[52][141] = { {0} };
    int row;
    int col;

    if (argc != 2) { /* process error here */
    exit(EXIT_FAILURE);
    }
    if (!(fd = fopen(argv[1], "r"))) { /* process error here */
    exit(EXIT_FAILURE);
    }

    for (col = 1; fgets(buffer, sizeof buffer, fd); col++) {
    char *token;
    int n;
    for (token = strtok(buffer, " "); token; token = strtok(0, " ")) {
    if (!sscanf(token, "n%d", &n)) /* process error */
    ;
    /* use this read integer 'n' however it is that you do so */
    }
    }

    return 0;
    }


    [EOM: original code]

    > #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;
    >
    >
    > /* problem has to be between here and the output loops */
    >
    > 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 */
    > }
    > }
    > /* shld break-out read file into buffer, access data 1, access buffer
    > many */
    > }
    >
    > for(row = 0; row < 52; row++) /* output loops */
    > {
    > printf("\n");
    > for (col = 0; col < 140; col++)
    > printf ("%d ", current[row][col]);
    > }
    >
    >
    > return(0);
    > }



    --
    Martin Ambuhl
    Martin Ambuhl, Aug 10, 2003
    #3
  4. On 10 Aug 2003 13:17:36 -0700
    (Blankdraw) wrote:

    <snip useless explanation of what it should be doing which I won't bother to
    read because it's too long and complicated, since the author didn't take the
    time to summarize it in a few lines>
    >
    > #include <stdio.h>
    > #include <fcntl.h> /* used by open() */
    > #define SIZE 1024 /* 140 x 7bytes, incl 1LF */
    > #define CTRL_Z '\032' /* text-mode EOF */


    platform specific

    > main(argc,argv)
    > int *argv[];


    K&R style function definition. Implicit int. the above line is pointless if you
    really want what you did here, but I doubt it. It was char *argv[], last time I
    checked. Do they do things this way where you're from?

    > {
    > char buffer[SIZE]; /* hafta make do w/o int decl */


    hafta? w/o? nice english in the comment, pal.

    > int fd;
    > long int n=1;
    > long count = 0;
    > int current[52][141]; /* r52c141 array, built-up */


    magic numbers!

    > 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;
    >
    >
    > /* problem has to be between here and the output loops */
    >
    > fd = open(argv[1], O_RDONLY | O_TEXT);


    open() takes a pointer to a character. argv[1] is a pointer to an integer in
    this code. And you never checked if argv[1] was even given. Try using argc for
    that.

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


    your compiler must've warned you about this one.

    > 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 */


    read() returns the number of bytes read. So it's pointless to check it against
    '\n' (and for the record, a CRLF is "\r\n"), or to check n to CTRL_Z in the
    outer loop(which I think you commented out). Although it's hard to see what the
    outer loop IS, or what you commented out or not. I'm not reading any more of
    this, go fix your code. The way I see it if this does what you want it to you're
    lucky, because it's undefined and obfuscated.

    And make it readable next time? The indentation sucks badly. Spaces here, tabs
    there... When posting code to a newsgroup, try indenting with spaces(4 of them).
    No tabs. Get rid of all code commented out unless it makes the code more
    understandable.
    Also, don't let lines be longer than 75-80 characters.

    --
    char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
    c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
    c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
    Pieter Droogendijk, Aug 10, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek LaZard

    Re: Abt Datareader Count

    Derek LaZard, Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,731
    Derek LaZard
    Jul 8, 2003
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    853
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,736
    Smokey Grindel
    Dec 2, 2006
  4. James Stroud

    py2app semi-standalone semi-works

    James Stroud, Oct 4, 2006, in forum: Python
    Replies:
    2
    Views:
    683
    James Stroud
    Oct 4, 2006
  5. Replies:
    5
    Views:
    303
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Jan 23, 2007
Loading...

Share This Page