简体   繁体   中英

This C program doesn't work

I copied the following C code from K&R. The code is supposed to print a line if it is currently the longest line typed by the user. This is the code:

 #include <stdio.h>

#define MAXLINE 1000
int max;
char line[MAXLINE];
char longest[MAXLINE];

int getline();
void copy();


int main(){

    int len=0;
    extern int max;
    extern char longest[];

    max = 0;
    while((len = getline()) > 0)
        if (len > max){

            max = len;
            copy();
        }

    if (max > 0){

        printf("%s", longest);
    }
    return 0;
 }


    int getline(){

    int c;
    int i;
    extern char line[];

    for(i = 0; i < MAXLINE-1
        &&(c=getchar())!=EOF&&c!='\n';++i)
            line[i] = c;

    if(c=='\n'){
        line[i]=c;
        ++i;
    }

    line[i] = '\0';
    return i;



}




      void copy(){

      int i;
      extern char line[];
      extern char longest[];

       i=0;

       while((longest[i]=line[i])!='\0')
        ++i;
      }

The problem is that it doesn't appear to work. Running the code, I can type lines into the console but it doesn't print the longest line. I would appreciate any help.

EXTRA INFO: I7m using Win7 with Open Watcom compiler. The console doesn't fold, it lets me input characters. Also, I'm not sure that I am stuck in a loop because if I change my while-loop in main() to

while((len = getline()) > 0)
        if (len > max){
            printf("IT WORKS");
            max = len;
            copy();
        }

with a print command if the length of the line is longer than the current max-length, then "IT WORKS" is printed on screen. So it is definitely counting line lengths.

In fact it does, but it won't stop reading lines until you type an end-of-file character.

This is ^D on Unix/Mac and ^Z on Windows. (But note that Windows doesn't really implement terminal-EOF, so it is up to your library or something like Cygwin to interpret the ^Z.)

If your window is only active when the program is running I suppose a sleep(5) might be a good idea at the very end of main(). It would really be best to run console I/O programs like this from a long-lived window like Terminal on the mac or the DOS box on Windows.

The getline returns 1 higher than expected, so if the user just hits enter, it doesnt exit the loop. Also, add getch() before return 0 in your main function, the console exits/folds after printing the longest line. Many books have the same example so if you want the console to remain open, put getch so the user should strike a key before the program exits. Here, try these codes:

#include <stdio.h>

#define MAXLINE 1000
int max;
char line[4];
char longest[MAXLINE];

int getline();
void copy();


int main()
{
    int len=0;
    extern int max;
    extern char longest[];

    max = 0;
            /* put -1 to getline so it will return the right length... */ 
    while(((len = getline()) - 1) > 0)
    {
        printf("%d \n",len);
        if (len > max)
        {

            max = len;
            copy();
        }
    }

    if (max > 0)
    {

        printf("%s", longest);
    }

            /* Put getch() or getchar() so console will wait for a key to be pressed before exiting */
    getch();
    return 0;
 }


int getline()
{

    int c;
    int i;
    extern char line[];

    for(i = 0; i < MAXLINE-1 &&(c=getchar())!=EOF&&c!='\n';++i)
    {
        line[i] = c;
    }

    if(c=='\n')
    {
        line[i]=c;
        ++i;
    }

    line[i] = '\0';
    return i;
}




      void copy(){

      int i;
      extern char line[];
      extern char longest[];

       i=0;

       while((longest[i]=line[i])!='\0')
        ++i;
      }

Works fine here, but I think what you're missing is that you need to send EOF to getchar() to get out of the loop.

I think you're running this on Linux, right? If so, simply press CTRL-D (which means EOF on Linux/Unix) on an empty line, and your program will exit the loop and print out the longest line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM