从文本文件中读取字符,在一个二维数组存储 [英] Reading characters from a text file and storing in a 2D array

查看:127
本文介绍了从文本文件中读取字符,在一个二维数组存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

愚蠢的问题,但我恐慌。
我怎么会去从一个文本文件(包括空格)读取字符并将其存储在一个二维数组。

Stupid question, but I am panicking. How would I go about reading characters from a text file (including spaces) and storing them in a 2D array.

我知道我需要使用一些涉及(说我有一个二维数组文件[50] [50])

I know I need to use something involving (say I have a 2D array file[50][50])

while ((ch = fgetc(fp)) != EOF);

file[50][50] = characterWithinFile

任何帮助将大大AP preciated,因为我(奇怪)知道我需要在这之后的事情。谢谢

Any help would be greatly appreciated, as I (surprisingly) know what I need to do after this point. Thanks

推荐答案

这里是一些code,可以帮助你。

here is some code that can help you

#include <stdio.h>
void main()
{
FILE *fopen(), *fp;
char c;
int i=0,j=0;
char file[50][50];
fp = fopen("file.txt","r");
c = getc(fp) ;
while (c!= EOF)
{
file[i][j]=c;

    j++;
    c = getc(fp);

    if(j==50){
    i++;
    j=0;
    }
}
fclose(fp);
}

和请于50一个较大的数字,如果你的文字超过2500个字符!

and please choose a higher number than 50 if your text is more than 2500 characters !

这篇关于从文本文件中读取字符,在一个二维数组存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆