从 txt.file 读取数据并打印出其二维数组内容
我需要一些帮助,我想从文件中打印出信息(以字符为单位)
我决定使用二维数组,因为数据看起来像这样
0 0 . . .
0 0 . . .
. . . . .
. . . . .
. . . . .
的代码打印它们
似乎我无法用我现在准备 如下所示:
/* Read from file, pass on file contents to a 2D array, Print file contents from 2D array*/
#include<stdio.h>
#include<string.h>
#define N 5
//My proposed function to print an array with contents from a file
void printboard(int **a, int n, int n);
int main(int argc, char *argv[])
{
char linestr[100];
int board[N][N];
int k;
int h=0, l=0;
if(argc==2) //File should be called from the terminal hence working with argc & argv
{
FILE *fp;
fp = fopen(argv[1], "r");
if(fp == NULL)
{
printf("Error, can't open '%s' file!!!\n", argv[1]);
return -1;
}
while (fgets(linestr,sizeof linestr, fp) != NULL)
for(k=0; k<strlen(linestr); k++)
{
if (linestr[k]!='\n')
{
board[h][l]=(int)linestr[k];
l++;
}
h++;
l=0;
}
fclose(fp);
}
printboard(board,h,l);
return 0;
}
void printboard(int **a, int n, int n)
{
int i, j;
for (i=0; i< N; i++)
{
for (j=0; j< N; j++)
{
printf("%c", a[i][j]);
}
printf("\n");
}
}
我对 C 语言有非常基础的了解,并且 1.5 个月前才开始编码。社区有什么建议可以解决这个问题或者做得更好吗?目的是以二维数组格式打印文件的内容。我真的很想让数据与 2D 数组一起使用,因为我需要进一步研究它以在名为 Peg Solitaire 的游戏中移动“0”。
I need some help, I would like to print out information from a file (in characters)
I decided to use a 2D array since the data looks something like this
0 0 . . .
0 0 . . .
. . . . .
. . . . .
. . . . .
It seems that I cannot print them out with the code that I have prepared now
please see below:
/* Read from file, pass on file contents to a 2D array, Print file contents from 2D array*/
#include<stdio.h>
#include<string.h>
#define N 5
//My proposed function to print an array with contents from a file
void printboard(int **a, int n, int n);
int main(int argc, char *argv[])
{
char linestr[100];
int board[N][N];
int k;
int h=0, l=0;
if(argc==2) //File should be called from the terminal hence working with argc & argv
{
FILE *fp;
fp = fopen(argv[1], "r");
if(fp == NULL)
{
printf("Error, can't open '%s' file!!!\n", argv[1]);
return -1;
}
while (fgets(linestr,sizeof linestr, fp) != NULL)
for(k=0; k<strlen(linestr); k++)
{
if (linestr[k]!='\n')
{
board[h][l]=(int)linestr[k];
l++;
}
h++;
l=0;
}
fclose(fp);
}
printboard(board,h,l);
return 0;
}
void printboard(int **a, int n, int n)
{
int i, j;
for (i=0; i< N; i++)
{
for (j=0; j< N; j++)
{
printf("%c", a[i][j]);
}
printf("\n");
}
}
I have very basic knowledge in C and only began coding 1.5 months ago. Is there any advice from the community how I can fix this or do it better? The aim is to print contents of a file in a 2D array format. I would really like to have the data work with 2D array because I need to work further on it to move the '0' in a game called Peg Solitaire.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
函数中的参数名称必须是唯一的。因此,您的打印板函数不能有两个“n”参数,您必须更改一个名称(更改为“m”或其他名称)。另外,由于您正在打印 nxn 矩阵并且已通过预处理器定义了“N”,因此您是否还需要这些参数? :)
数组(打印板的第一个参数)的传递有点棘手,需要一些关于指针和内存如何工作的知识。请参阅此处的讨论:
http://cboard.cprogramming .com/c-programming/97898-passing-2-Dimension-array-function.html
这归结为将函数定义更改为:
一件小事:为了打印您可能想要的数字打印它们作为整数而不是字符,因此您应该更改
为
我添加了空格,以便在打印到终端时数字会一起运行。
至于解析输入,那是一个完整的话题。我建议 strtok 在您从文件中读取行时将其分解。然后,您将使用 sscanf 之类的工具将数据存储到板数组中。请参阅这些参考文献:
http://www.cplusplus.com/reference/clibrary/cstdio/ sscanf/
http://www.cplusplus.com/reference/clibrary/cstring/strtok/
Argument names in functions must be unique. So your printboard function cannot have two "n" arguments, you'll have to change one name (to "m" or something). Also, since you're printing n x n matrices and you have defined "N" via the preprocessor, do you even need those arguments? :)
The passing of the array (first argument of printboard) is a bit trickier and requires some knowledge of how pointers and memory work. See discussion here:
http://cboard.cprogramming.com/c-programming/97898-passing-2-dimensional-array-function.html
What this boils down to is changing your function definition to:
One little thing: in order to print your numbers you probably want to print them as integers rather than characters so you should change
to
I added the space so the numbers would run together when print out to the terminal.
As far as parsing the input, that's a whole nuther topic. I'd suggest strtok to break up your lines as you read them from the file. Then you'll use something like sscanf to store the data into your board array. See these refs:
http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/
http://www.cplusplus.com/reference/clibrary/cstring/strtok/
我只是猜测您希望在字段为空时显示点 (.)
所以预填充你的板阵列。
i am just guessing here that you want points (.) displayed when the field is empty
so prefill your board array.