|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
void f(char* file)
{
FILE *f;
f=fopen(file,"r");
char buff[255];
char arry[3][3]={' '};
while(fgets(buff,255,f)!=NULL){
printf("%s",buff);}
fclose(f);
}
int main(){
f("a.txt");
return 0;
}
a.txt 里面是个矩阵
a b c
d e f
a b c
像请问一下大佬们 我这里能读取txt的内容并打印出来 但是我要怎么样才能将内容 输入到arry[3][3]里面 提供给控制台操作呢?
另外是不是需要把函数f 变为 char 最后还要return arry呢?
本帖最后由 理想小青年 于 2019-9-21 07:08 编辑
使用 fprintf 和 fscan这两个函数对文件操作,直接可以格式化的读取
把读取的所有数据保存在Buff中,对buff做过滤重定向到arry数组中,如下:
- int index = 0;
- for (int i = 0; i < sizeof arry;)
- {
- // 判断Buff中是不是字母
- if ('a' <= buff[index] && 'z' >= buff[index])
- {
- ((byte*)arry)[i] = buff[index];
- ++i;
- }
- ++index;
- }
复制代码
|
|