Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.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
如何从fgetc将重复的空字符存储在C中的字符串中?_C_String_Null_Stdin_Fgetc - Fatal编程技术网

如何从fgetc将重复的空字符存储在C中的字符串中?

如何从fgetc将重复的空字符存储在C中的字符串中?,c,string,null,stdin,fgetc,C,String,Null,Stdin,Fgetc,编辑:这根本无法可靠地处理字符串。我已经将整个系统更改为使用int数组。也消除了许多其他的头痛问题。我的MVC的工作版本是: #include <stdio.h> #include <string.h> int main (){ int nextChar; int augmented[256]; int index = 0; while ((nextChar = fgetc(stdin)) != EOF){ augment

编辑:这根本无法可靠地处理字符串。我已经将整个系统更改为使用int数组。也消除了许多其他的头痛问题。我的MVC的工作版本是:

#include <stdio.h>
#include <string.h>

int main (){
    int nextChar;
    int augmented[256];
    int index = 0;
    while ((nextChar = fgetc(stdin)) != EOF){
        augmented[index] = nextChar;
        index++;
    }
    for (int i = 0; i <= index;++i){
        printf("%c", augmented[i]);
    }
}
#包括
#包括
int main(){
int nextChar;
整数增广[256];
int指数=0;
而((nextChar=fgetc(stdin))!=EOF){
增广[索引]=下一个索引;
索引++;
}

对于(int i=0;i以下是对您的程序的一些更新。0被转换为“0”。不完全确定您在寻找什么,但希望这能为您指明正确的方向:

#include <stdio.h>
#include <string.h>

int main (){
    int nextChar;
    char augmented[256] = {0}; // zero entire array
    int i = 0;
    while ((nextChar = fgetc(stdin)) != EOF){
        // convert 0 to some other character
        if( nextChar == 0 ) nextChar = '0';
        augmented[i++] = (char)nextChar;
        //check for buffer overflow
        if( i==255) break;
    }
    printf("%s",augmented);
}
#包括
#包括
int main(){
int nextChar;
字符扩充[256]={0};//将整个数组归零
int i=0;
而((nextChar=fgetc(stdin))!=EOF){
//将0转换为其他字符
如果(nextChar==0)nextChar='0';
增广的[i++]=(char)nextChar;
//检查缓冲区溢出
如果(i==255)中断;
}
printf(“%s”,增大);
}

问题是括号。更改为:

while ((nextChar = fgetc(stdin)) != EOF){
您的代码将比较值
fgetc(stdin))!=EOF
分配给
nextChar


您还应该将
charBuffer
初始化为零。

如果数据包含零,那么使用字符串函数将不会很好。您最好保留
增广[]的索引
直接写入,而不是四处走动。这让我走上了正确的道路。我放弃了所有字符串,在int数组上完成了全部工作。关于:
while((nextChar=fgetc(stdin))!=EOF){augmented[index]=nextChar;index++;}
此代码允许在
augmented[256]中存储无限数量的字符
但是,该数组只有256字节长。建议:
while(index
谢谢您的回复。我以前试过这个(现在再次)似乎没有效果。这是一个好主意,但它会将其转换为ascii零。我需要实际的null 0x00。然后删除if(nextChar==0)nextChar='0'。还要注意,printf将在第一个0x00处停止。是的,这肯定是个问题。但这只是在我发布的代码片段中。在实际代码中,它是您定义的。很好的捕获!谢谢!但仍然不能解决空值问题。
while ((nextChar = fgetc(stdin)) != EOF){