Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中将字符串设置为空字符串_C - Fatal编程技术网

在C语言中将字符串设置为空字符串

在C语言中将字符串设置为空字符串,c,C,我想用C编写一个程序,它将接受一个输入.txt文件,从中读取并应用函数,或在标准输出上显示文本,或将其写入输出文件。为了简单起见,我写它只是为了显示文字 完整代码: #include <stdio.h> #include <stdlib.h> #include <getopt.h> int main(int argc, char **argv){ int c; int output = 0; FILE *infile; while((c = getop

我想用C编写一个程序,它将接受一个输入.txt文件,从中读取并应用函数,或在标准输出上显示文本,或将其写入输出文件。为了简单起见,我写它只是为了显示文字

完整代码:

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

int main(int argc, char **argv){

int c;

int output = 0;
FILE *infile;

while((c = getopt(argc, argv, "o:")) != -1){
    switch(c){

        case 'o':
            if(output){

                exit(EXIT_FAILURE);
            }
            output = 1;

            break;
        case '?':
            exit(EXIT_FAILURE);
        default:
            exit(EXIT_FAILURE);
    }
}

    for(; optind< argc; optind++) {
    infile = fopen(argv[optind], "r");


    size_t cpos = 0;
    char str[100];

    int ch;
    while((ch = fgetc(infile)) != EOF ) {
        if(ch == '\n' || ch == ' '){
            fprintf(stderr, "%s\n", str);
            str[0] = '\0';
            continue;
        } else{
            str[cpos++] = ch;
        }

    } 

    str[cpos] = 0;
    fprintf(stderr, "%s\n", str);

    fclose(infile);

    }
    exit(EXIT_SUCCESS);
    }
我得到的只是

word1

我试图将
str[0]='\0'
设置为空字符串,但此后
str
上没有存储任何内容。如何输出所有3个单词

您尚未将构建的字符串归零,并且未重置
cpos=0

结果是,您继续向
str[]
写入内容,超出了终止它的
'\0'

循环应如下所示:

while((ch = fgetc(infile)) != EOF ) {
    if(ch == '\n' || ch == ' '){
        str[cpos] = '\0';               // terminate this string
        fprintf(stderr, "%s\n", str);
        cpos = 0;                       // for next word
    } else{
        str[cpos++] = ch;
    }
} 

我看到了几个潜在的问题:

  • 遇到空白时,必须将
    cpos
    重置为零
  • 在循环中的fprintf之前,应该确保nul终止当前str
  • 如果EOF前的最后一个字符是
    \n
    ,则将在末尾输出一个空行
  • 我不知道您的数据集,但我仍然要注意输入超过99个字符,因为当您第一次遇到一个包含100个或更多字符的单词时,可能会导致缓冲区溢出

  • 当您知道str数组具有该单词的所有字符时,您必须在数组中附加一个终止符(零字节),并在为新单词收集字符之前将CPO重置为0

    假设str是{'w','o','r','d'} cpos为4 您不需要str[cpos]=0 然后str是{'w','o','r','d',0} 这是一个正确的以null结尾的字符串

    空字符串是{0},这是正确的


    无论如何,不要忘记将CPO重置为0,否则您将在结束符之后写入。

    请在发布之前删除一些空行。问题不是字符串重置。您需要进行一些基本的调试。将变量的打印输出放入循环中,以调查发生了什么。如果文件中的第一个字符是
    '\n'
    '
    ,则可能存在未初始化的UB打印输出
    str
    。而且,
    继续语句是无用的。
    
    word1
    
    while((ch = fgetc(infile)) != EOF ) {
        if(ch == '\n' || ch == ' '){
            str[cpos] = '\0';               // terminate this string
            fprintf(stderr, "%s\n", str);
            cpos = 0;                       // for next word
        } else{
            str[cpos++] = ch;
        }
    }