如何通过c++实现char型字符数组的输入?(具体看详情)

如何通过C++语言的for循环(或其他循环)实现将一串未知长短字符(比如说一段英文句子,长度<=1000000,带空格)输入到char型数组(将其中的空格转为‘-’输入)到char 型数组?

int pos = 0;
char arr[1000000];
char buff[1000];
while (1)
{
scanf("%s", buff);
int n = strlen(buff);
strcpy(arr[pos], buff);
pos += n + 1;
if (n == 0) break;
arr[pos] = ' ';
}

不过建议你从文件读取,因为1000000的文章,万一输错一个字,就要重新运行输入,这个体验不太好。

#include<iostream>
using namespace std;
int main() {
    int len = 0;
    char ch;
    char str[1000000];
    while ((ch = getchar()) != '\n')    //遇到换行符结束
        str[len++] = ch == ' ' ? '-' : ch;
    str[len] = '\0';                    //填充终止字符
    cout << str;
    return 0;
}
#include<iostream>
using namespace std;
char sum[10000001];
int main()
{
    char t;
    int temp = 0;
    freopen("a.txt","r",stdin);
    while(scanf("%c",&t)==1)
    {
        if(t==' ')
            sum[temp] = '-';
        else
            sum[temp] = t;
        ++temp;
    }
    return 0;
}

这个要用文件读写,把要输入的文件放到与程序同一个路径下