c - 乱码输出

标签 c arrays string stdio

我目前正在做一项作业,要求我审查 argv 和输入重定向的单词。

我的问题在我的输出中很明显,可以在下面找到。我有很多跟踪打印语句,它们可能会或可能不会帮助您指出我的问题。

我的意图是:

  1. 获取file.txt
  2. file.txt的内容复制到buffer[x​​][y]中,其中[x]是一个字符串,[y][x] 的一个字符。
  3. argv[] 参数与 buffer[][] 进行比较。
  4. 创建 newstr[size2]。对于在 buffer[][] 中找到的每个 argv[] 参数,将其替换为 replace[9] = "CENSORED"
  5. 打印newstr[0 to (size2-1)]的字符串。

这是我的代码:

// censored.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 128

int main ( int argc, char* argv[])
{
    int i = 0;
    int j = 0;
    int k = 0;
    char buffer[SIZE][SIZE];
    char x;
    int count = 0;
    int size1 = sizeof(buffer);
    int size2 = 0;
    char replace[9] = "CENSORED";
    int buffersize=0;
    printf("tracing: size1: %d.\n", size1);
    printf("tracing: argc: %d.\n", argc);
    while((fscanf(stdin, "%c", &x)!=EOF))
    {
        if(isalpha(x))
        {
            buffer[i][j]=x;
            printf("tracing: buffer[%d][%d]: %c\n", i,j, buffer[i][j]);
            j++;
        }
        else if(isspace(x)) // if whitespace
        {
            j = 0;
            i++;
            buffer[i][j]=x;//this should be buffer[i][j]
            printf("tracing: buffer[%d][%d]: %c\n", i,j, buffer[i][j]);
            j = 0;
            i++;
        }
        else if(ispunct(x)) // if x is a punctuation
        {
            j = 0;
            i++;
            buffer[i][j]=x;//this should be buffer[i][j]
            printf("tracing: buffer[%d][%d]: %c\n", i,j, buffer[i][j]);
        }
        else if(iscntrl(x)) // if control key (\n, \r etc...)
        {
            j = 0;
            i++;
            buffer[i][j]=x;//this should be buffer[i][j]
            printf("tracing: buffer[%d][%d]: %c", i,j, buffer[i][j]);
            j = 0;
            i++;
        }
        else if(isdigit(x))
        {
            buffer[i][j]=x;//this should be buffer[i][j]
            printf("tracing: buffer[%d][%d]: %c\n", i,j, buffer[i][j]);
            j++;
        }
        else
        {
            break;
        }
    }
    size2 = i;
    printf("tracing: buffer[8][0]:%s\n",buffer[8]);
    char newstr[size2][SIZE];
    i = 0;
    j = 0;
//  tracing:
    printf("tracing: line 72\n");
    printf("tracing: size2: %d.\n", size2);
    while(i < size2) //print buffer[]
    {
        printf("%s", buffer[i]);
        i++;
    }

    printf("tracing: line 80\n");
    for(k=1; k < argc; k++)
    {
        printf("%s\n", argv[k]);
    }
//  end tracing
    i = 0; //reinitialize i
    j = 0; //reinitialize j
//  creating newstr[SIZE] and censoring words
    printf("tracing: line 89\n");
    for(i = 0; i < size2; i++)
    {
        for(j=1; j < argc; j++)
        {
            if(strcmp(buffer[i], argv[j])==0)
            {
                strcpy(newstr[i], &replace[0]);
                printf("tracing: replaced at [%d]\n", i);
                break;
            }
            else
            {
                strcpy(newstr[i], buffer[i]);
                printf("tracing: copied at [%d]\n", i);
            }
        }
    }
    i = 0; //reinitialize i
    while(i < size2)
    {
        printf("%s", newstr[i]);
        i++;
    }
    return 0;
}

假设我有一个名为 file.txt 的输入重定向文件及其内容:

Said Hamlet to Ophelia,
I'll draw a sketch of thee,
What kind of pencil shall I use?
2B or not 2B?

我的输入是:

./censored Ophelia thee 2B < file.txt

这是我得到的奇怪输出:

Said Hamlet to CENSORED, Ill draw a sketch ???)ofoQ? ?"h?CENSORED,2oQ?
What? /oQ?kind? of 6oQ?pencil +oQ?shall ?"h?I-oQ? ???)use? ???2BoQ?
7oQoroQ Qnot 1oQ?CENSORED?4oQ?

感谢任何帮助,我知道我的代码很乱,这是我学习 C 的第一学期。

最佳答案

我有好消息和坏消息,坏消息是:我发现了一些错误和失误。 好处是:所有这些对于初学者来说都很常见!

第一个:buffer[] 中的字符串没有以 '\0' 结束,这是字符串结束指示符。这是你的问题的一个原因。 这是其他的:

  • 字符缓冲区[SIZE][SIZE]; 在这里,您正在考虑单词的数量和单词的长度不能超过 128,这将在这种情况错误时导致 Segmentation Fault。我建议您了解动态分配 (malloc())。
  • for(j=1; j < argc; j++) 如果 argc == 1,您将不会进入此循环,这将导致 newstr 为空。通常我们以 NULL 指针结束数组,然后在读取数组时检查数组中的每个字符串是否为非空,这样我们就不会尝试使用未定义的内容。
  • 正如您指出的那样,您的代码非常困惑,下次尝试将代码分离到函数中,而不是在 main() 中编码。
  • 尝试减少它,你写的行越少,你的代码就越容易阅读和调试:

j = 0; 我++; buffer[i][j]=x;//这应该是buffer[i][j] printf("追踪: 缓冲区[%d][%d]: %c\n", i,j, 缓冲区[i][j]); j = 0; i++;

  • 这条建议与那种旨在读取大量数据的程序无关,但请记住:尽可能避免存储数据。

我想这就足够了,我祝你好运,并为我糟糕的英语道歉。

关于c - 乱码输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33118194/

相关文章:

java - 将字符串数组从容器 Activity 传递到 fragment 时,getArguments() 返回 null

java - 需要在字符串中选择一个随机字母并更改它

c - C- 中的字符串是 char *str 真的等同于 char str [] 吗?

c - 为什么我应该使用特定于线程的数据?

c++ - ASCII "graphics"库?

c++ - 如何使用最少的代码 C++ 设置数组的特定元素

arrays - 修改字典数组内部的字典属性。错误: Cannot assign to immutable expression of type [String:AnyObject]

c - 多边形的三角剖分

java - 字符串操作编码建议

javascript - 将字符串转换为 javascript 关联数组