0

I've been given a mission to read parameters from a text file and create a matrix from that parameters. I've been stuck for hours for the part where I read the numbers. The order that they are organized in the test file is this:

Number Number Number A letter (L or S)

And then there's another line with the same struct.

My code until now is this:

#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;
}
CannedMoose
  • 509
  • 1
  • 10
  • 18
  • Did you try [fscanf](http://www.cplusplus.com/reference/cstdio/fscanf/)? – No Em May 04 '18 at 08:28
  • Sure, I did, but how do I separate it from the other parameters, and if there's a 2 digit number, how do I read it? – David Borohov May 04 '18 at 08:34
  • @DavidBorohov say you were reading two numbers, you could do `fscanf(wherever, "%d %d", n1, n2)`. Remember to check the return value of `scanf` though, and be careful with extra `newline` characters in the input buffer – Jose Fernando Lopez Fernandez May 04 '18 at 08:50
  • Yet, @Jose Fernando Lopez Fernandez, I don't quite understand. Can you explain which parameters I need to put into fscanf – David Borohov May 04 '18 at 08:52
  • `#include int main() { FILE * f = fopen("input.txt", "r"); if (!f) printf("file not found"); float a,b,c; char d; fscanf(f, "%f %f %f %c\n", &a, &b, &c, &d); printf("%f %f %f %c\n", a, b, c, d); }` For example the first line: *1.1 2.2 3.3 L* And makesure that between each element should place a space – No Em May 04 '18 at 08:52

1 Answers1

0

It looks like you know that each line has those items separated with spaces.

Then you could just split each line into tokens. Have a look at the answer to this question:

Split string in C every white space

Mattias Backman
  • 927
  • 12
  • 25