Newb:作业:C Prog:将 getchar() 输入保存到二维数组中

Newb: Assignment: C Prog: saving getchar() input into 2D array

Newb:学习 C - 使用 C 编程语言 - 第 2 版。 我搜索了 Whosebug - 找不到解决方案。

我正在使用 getchar() 打开一个流(这是手册让我学习的内容。流以 EOF <ctrl> z 结束。脚本寻找一个新行并且应该将每一行放入一个二维数组(行,宽度)每一行都应该包含一整行输入。

代码运行,流工作。虽然似乎没有填充数组。代码远未完善,只是想在我完善之前让它工作。我只是将 exit() 用作试图让它工作的捷径。有任何想法吗?我添加了一个在末尾打印的计数器 r ,它应该指示在数组中创建的行数......它是零......让我觉得数组没有被构建。宇宙的命运就靠你了!!!

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define ROW 1000
#define WIDTH 5000

char arr[ROW][WIDTH] = {0};               // array to store char strings of sentences - each sentence stored in an individual row
int r = 0;                              // initiallizing rows
int i = 0;
char endprog(char arr[ROW][WIDTH]);

int main () {
    
    int c;                               // int variable to accept input - will be copied into arraw
    bool end = true;                           // count the character for each line to iterate through
    
        
    while (end){
    
    for ( i = 0; i < (c = getchar()) != EOF && c != '\n'; ++i){
    
        if (c == EOF)
            endprog(arr);
            
        arr[r][i] = c;
        
        if (c == '\n'){
                    
            arr[r][i] = c;
            i++;
            r++;                              // new line deterted row incremented to next row - ready for new line
        }
        
        arr[r][i] = '[=10=]';                        // copying input into array at position (row, i)
        
    }
}
}

char endprog(char arr[ROW][WIDTH]){
        
        int j;
    
        for (j = 0; j <= r; j++){                 // iterating through array to print rows of input strings
            printf("R: %d", r);
            printf("%s\n", arr[j]);
        
    }
    exit(0);
}

输出

Stream is working.
New lines are accepted

Help me Obi-Wan Kenobi
You're my only hope....
^Z
R: 0

--------------------------------
Process exited after 120.8 seconds with return value 0
Press any key to continue . . .

我稍微修改了您的代码,希望这就是您要找的。 代码中的注释几乎解释了我对您的代码所做的所有更改。

#include <stdio.h>
/* you don't need stdbool.h and stdlib.h for this code*/
#define ROW 1000
#define WIDTH 5000

char arr[ROW][WIDTH] = {0};               // array to store char strings of sentences - each sentence stored in an individual row
int r = 0;                              // initiallizing rows
int i = 0;
void endprog(char arr[ROW][WIDTH]); //the function is void since it's not returning anything

int main () {
int c; // int variable to accept input - will be copied into array
while (1){ //you don't need the for-loop and also the for-loop you've written has wrong logic
    c=getchar();
    if (c == EOF){
        endprog(arr);
        break;
    }
    else{
    arr[r][i] = c;
    i++; //increasing the column by 1
    if (c == '\n'){
        arr[r][i] = '[=10=]'; //since c is '\n', it means the line is completed, so write the NUL character at the end
        i=0;
        r++; //increasing the row by 1 and changing the column to 0 since the next character has to be written in the first (0) index of the next row
    }
    }
}
}

void endprog(char arr[ROW][WIDTH]){
        
        int j;
        for (j = 0; j<r; j++){
            printf("R: %d ", j); //you're supposed to print j, not r
            printf("%s\n", arr[j]);
        }
}