📄 creatematrix.c
字号:
/*create a file which store a metrix with specific Row &Column in the file the first two int are the Row&column of the matrix*/#include "stdio.h"#include "stdlib.h"#define MAX 20#define INTSIZE sizeof(int)#define CHARSIZE sizeof(char)/*assume the max value of every element of that matrix */int main() { char *filename; FILE *fd; int row,column; int i,j;/*loop variable*/ int value; printf("Open a new file for write only!\n");printf("WARING:If the file already exist,it will be truncated to zero length!\n"); printf("Please enter file name:"); filename=(char *)malloc(CHARSIZE*128); scanf("%s",filename);/* printf("the filename is %s\n",filename);*/ fd=fopen(filename,"w"); if (fd==NULL)/*CANNOT create the file and exit the system*/ { printf("error in create the file %s\n",filename); exit(0);}/*if*/else printf("Successfully create a new file %s\n",filename);/*create a file for write only,or open a file and truncate it to zero length*/ printf("Please input the ROW and COLUMN of the file please?\n"); printf("Row:"); scanf("%d",&row); printf("Column:"); scanf("%d",&column); printf("Matrix Row=%d,column=%d\n",row,column); printf("the first two int of the file write down number of the Row&column of the matrix!\n"); fwrite(&row,INTSIZE,1,fd); fwrite(&column,INTSIZE,1,fd); srand(getpid());/*reset the seed,and initialize the pseudo-random creation process*/for (i=0;i<row;i++) { for (j=0;j<column;j++) { value=MAX*rand()/(RAND_MAX+1); printf("%d ",value); fwrite(&value,INTSIZE,1,fd); } /*for j*/ printf("\n");} /*for i*/fclose(fd);free(filename);/*close the file fd*/return(0);}/*main*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -