C程序:写字符至磁盘文件出现内存越界,实在检查不出有关问题所在,望有心人提点一下

C程序:写字符至磁盘文件出现内存越界,实在检查不出问题所在,望有心人提点一下
异常:
C程序:写字符至磁盘文件出现内存越界,实在检查不出有关问题所在,望有心人提点一下

代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
FILE *fp1,*fp2;
char current;
char buffer[50][5];
int i = 0,j = 0,count = 0,k = 0;
    
fp1 = fopen("C:\\Users\\zpp\\Desktop\\temp\\Indata.txt","r");
if(NULL==fp1)
{
printf("can not open input file\n");
exit(0);
}
fp2 = fopen("C:\\Users\\zpp\\Desktop\\temp\\Outdata.txt","w");
if(NULL==fp2)
{
printf("can not open output file\n");
exit(0);
}

while(EOF!=(current=getc(fp1)))
{
if(current=='\n')
{
buffer[i][j] = '\0';
i++;
j = 0;
}
else
{
buffer[i][j] = current;
j++;
}
}
fclose(fp1);
count = i;
    j = 1;
for(k=0;k<count;k++)
{
//temp = (char)j;
//putc('\n',fp2);
fputc(j,fp2);
fputc('\n',fp2);
if(0!=strcmp(buffer[k],buffer[k+1])&&(k!=count-1))
{
j++;
putc(j,fp2);
putc('\n',fp2);
}
}
fclose(fp2);
    printf("sucess\n");
}
------解决思路----------------------

while(EOF!=(current=getc(fp1)))
{
if(current=='\n')
{
buffer[i][j] = '\0';
i++;
j = 0;
}
else
{
buffer[i][j] = current;
j++;
}
}

此循环可能导致数组越界