简体   繁体   English

我的程序无法正确输出行

[英]My program can't output the lines correctly

I'm relatively new in C and I currently reading Kernighan's book.我在 C 方面相对较新,目前正在阅读 Kernighan 的书。 One of the problems in the book is to create an algorithm that from a input line output the line if it is more than 10 characters long.本书中的一个问题是创建一个算法,如果输入行超过 10 个字符,则从该行输出该行。

The point is I'm frustrated because I cant find what is wrong with my code.关键是我很沮丧,因为我找不到我的代码有什么问题。 I debugged and recreate it many times but still cant find out what's going on!我调试并重新创建了很多次,但仍然无法找出发生了什么!

The escape character from function getl() is '.'函数getl()的转义字符是 '.' (dot), and sometimes works and other times don't. (点),有时有效,有时则无效。 If you compile it and test you will see:如果你编译它并测试你会看到:

gcc -Wall -o out 'script.c'

The question header from the book is:书中的问题标题是:

“Exercise 1-17. “练习 1-17。 Write a program to print all input lines that are longer than 10 characters.”编写一个程序来打印所有超过 10 个字符的输入行。”

I'm sure that's relatively easy, but I really wanted to know why this algorithm is not working as expected, i think it has something to do with '\\n'.我确信这相对容易,但我真的很想知道为什么这个算法没有按预期工作,我认为它与 '\\n' 有关。

If someone could help me find out what's the problem with the code, I would appreciate it.如果有人能帮我找出代码的问题,我将不胜感激。

Code代码

#include <stdio.h>

#define MAX 10000

int getl(char line[], int lim) {
    char c;
    int count;

    for (count = 0 ; count < lim-1 && (c = getchar()) != '.' ; count++) {
        if (c == '\n') {
            line[count] = '\n';
            count++;
            break;
        }
        line[count] = c;
    }
    line[count] = '\0';
    return count;
}
    
int main() {
    char line[MAX];
    int len = 1;

    for (; len > 0 ;) {
        getl(line, MAX);
        len = getl(line, MAX);
        if (len >= 10)
            printf("%s", line);
    }
    return 0;
}

Your code almost works.你的代码几乎可以工作。 You just seem to have some repeated lines here and there that confuse things.您似乎只是在这里和那里有一些重复的行,使事情变得混乱。

Specifically, you are calling getl(line, MAX);具体来说,您正在调用getl(line, MAX); twice in a row.连续两次。 The first gets the input, but don't save the count, the second has only an empty stdin buffer to work with so no sensible count is saved from that.第一个获取输入,但不保存计数,第二个只有一个空的 stdin 缓冲区可以使用,因此不会从中保存合理的计数。 Removing the first call that don't save the count fixes your issue.删除不保存计数的第一个调用可以解决您的问题。

#include <stdio.h>

#define MAX 10000

int getl(char line[], int lim) {
    char c = getchar();
    int count;
    
    for (count = 0 ; c != '.' ; count++) {
        line[count] = c;
        c = getchar();
    }
    line[count++] = '\n';
    return count;
}
    
int main() {
    char line[MAX];
    int len = 1;
    for (; len > 0 ;) {
        len = getl(line, MAX);
        if (len >= 10)
            printf("%s", line);
    }
    return 0;
}

First, you're calling your getl function twice instead of once (you only want to read lines one by one).首先,您调用 getl 函数两次而不是一次(您只想一行一行地读取行)。 Fixing that should work.修复它应该有效。 Then I think you shouldn't add the trailing '\\n' to your lines, just print it when your line is longer than 10 characters, in your code, the '\\n' will be counted as a character.然后我认为您不应该将尾随的 '\\n' 添加到您的行中,只需在您的行长于 10 个字符时打印它,在您的代码中,'\\n' 将被视为一个字符。

Here's the modified code:这是修改后的代码:

#include <stdlib.h>
#include <stdio.h>
#define MAX 10000

int getl(char line[])
{
    char    c;
    int     count;

    for (count = 0; count < MAX - 1 && (c = getchar()) != '.' ; count++)
    {
        if (c == '\n')
            break;
        line[count] = c;
    }
    line[count] = '\0';
    return (count);
}

int main()
{
    char    line[MAX];
    int     len = 1;

    while (len > 0)
    {
        len = getl(line);
        if (len >= 10)
            printf("%s, c = %i\n", line, len);
    }
    return (0);
}

This should work.这应该有效。 https://ideone.com/cXXRUH https://ideone.com/cXXRUH

#include <stdio.h>

#define MAX 10000

int getline_length(char line[]) {
  char ch;
  int count = 0;

  printf("\nWaiting for INPUT...");
  // Using clear while loop to get input, removing redundent complexity 
  // Either `.` or `\n` consider End Of Line 
  while(count < MAX-1 && ((ch = getchar()) != '.' || (ch = getchar()) != '\n')) {
      line[count++]=ch;
  }
  line[count] = '\0';
  return count;
}

int main() {
   char line[MAX];

   while(1) {
       // reset array before each input 
       memset(line, 0, sizeof(line));
       int len = getline_length(line); //No need to pass limit 
       if (len >= 10) {
           printf("%s", line);
        } else {
            printf("len < 10");
        }
   }
   return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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