我的任务是从文本文件中读取参数并根据参数创建矩阵。在我阅读数字的那一部分,我已经被困了几个小时。它们在测试文件中的组织顺序如下:
号码 号码 号码 一封信(L或S)
然后还有另一条具有相同结构的行。
我的代码到现在为止:
#include <stdio.h>
#include <stdlib.h>
typedef struct matrix {//create a struct of matrix
int** mat;//the name
int rows, cols, start;//How many rows and cols + starting number
char shape;// The shape of the matrix
}matrix;
int** createMat(int r, int c);//Signature of a function #1
int strToInt(char* str);//Signature of a function #2
int CheckName(char A[], char B[]);//Signature of a function #3
int main(int argc, char** argv)//start of the program
{
matrix mat;//create a matrix
int lines = 0;//get the number of lines
FILE* input;//Create a pointer
char temp[5];//Create a temporary array
if (argc > 3 || argc < 2)//Check if the number of the parameters is correct
{
printf("There's a wrong number of arguments. Please try again.");
}
else
{
if (CheckName(argv[1], "input.txt") == 0)//check if the parameter is input.txt
{
if (argc == 3)//Check if the third parameter is given.
{
lines = strToInt(argv[2]);//fill the number of lines with the number of the lines
}
input = fopen(argv[1], "R");//open and read the input file
}
}
return 0;
}
int** createMat(int r, int c)//creat a matrix function
{
int *arr = (int *)malloc(r * c * sizeof(int));
return arr;
}
int strToInt(char* str)//insted of atoi function
{
return atoi(str);
}
int GetNumber(FILE* file)//Get the number function
{
char ch;//Create a temporary file
int len;//Create a temporary file
char temp[5];//Create a temporary array
while ((ch = fgetc(file)) != ' ')
{
len = strlen(temp);
temp[len] = ch;
temp[len + 1] = 0;
}
int CheckName(char A[], char B[])
{
int result = 1;
for (int i = 0; A[i] || B[i]; i++)
{
if (A[i] != B[i])
result = 0;
}
return result;
}
答案 0 :(得分:0)