文本文件中矩阵的尺寸

时间:2019-04-14 08:21:15

标签: c

我想以以下形式评估方阵的尺寸

-2  2 -3
-1  1  3 
 2  0 -1

所以在这种情况下,n = 3,使用我的代码,我可以读取所有整数的数量,但是我想在第一行停下来并获得前三个数字。

#include <stdio.h>

int main(){
  int temp;
  int n = 0;

  FILE *file = fopen("matrix","r");

  if(file == NULL){
    printf("Could not open specified file");
    return 1;
  }
  while(fscanf(file,"%d",&temp) == 1){
    n++;
  }  

  fclose(file);

  printf("%d",n);
  return 0;
}

2 个答案:

答案 0 :(得分:0)

我可能有些复杂的事情,但是如果必须这样做,我会这样做。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE* file_in = fopen("mat1", "r");

    char c;
    int temp;
    int n = 0;
    if (file_in)
    {
        // you get the characters one by one and check if
        // if it is an end of line character or EOF
        while ((c = (char)fgetc(file_in)) != '\n' && c != EOF)
        {
            // if it wasn't, you put it back to the stream
            ungetc(c, file_in);
            // scan your number
            fscanf(file_in, "%d", &temp);
            n++;
        }

        fclose(file_in);
    }
    else
    {
        fprintf(stderr, "Could not open specified file.\n");
        return 1;
    }

    printf("Number of numbers: %d\n", n);

    return 0;
}

也许这可以回答您的问题... 但是,我认为查看fgets会更简单(如上所述)。之后,您甚至不需要sscanf读取行,因为如果您的矩阵实际上是一个正方形矩阵,那么仅得到的行数就是矩阵的维数。 干杯!

答案 1 :(得分:0)

我决定逐行执行,而不是逐个字符地进行,尽管我认为它不必要地使这个特定示例复杂化,但是您应该能够看到如何从该示例中扩展。

例如,token变量只是无用地坐在那里,只保存了一次它用作while循环的检查对象。实际上,您可以在进入这些值时开始读取这些值,或者再次使用rowscols参数的确切知识再次遍历该文件,因此它将是一个简单地scanf设置值并验证返回值以确保没有奇怪的事情就可以解决了。

基本上,这要归结为时间和内存使用之间的折衷,因此该决定可能会将其他参数纳入最终决定中,例如期限约束。

无论哪种方式,我认为我们都可以同意,要求用户包括行数和列数而不是让我们追捕它们会更容易。

希望这会有所帮助,祝您好运!

#define DEFAULT_BUFFER_LENGTH (512)

int main(int argc, char *argv[])
{
    const char* inputFilename = (argc == 1) ? "matrix" : argv[argc - 1];

    FILE *inputFile = fopen(inputFilename, "r");

    if (!inputFile){
        printf("Could not open specified file");

        return EXIT_FAILURE;
    }

    char inputBuffer[DEFAULT_BUFFER_LENGTH];
    memset(inputBuffer, 0, DEFAULT_BUFFER_LENGTH);

    char* separator   = " ";
    char* token       = NULL;

    size_t rows       = 0;
    size_t cols       = 0;
    size_t prev       = 0;

    while (fgets(inputBuffer, DEFAULT_BUFFER_LENGTH - 1, inputFile)) {
        /** Save the current value of columns. I'm checking the number of tokens
         *  in every column because I think it would be a good idea to validate
         *  that the matrix is indeed a rectangle, but I didn't implement that
         *  check here.
         *
         *  In the event that the file has something weird, like a trailing new 
         *  line, this preserved value allows us to backtrack and preserve the 
         *  correct value.
         *
         */
        prev = cols;
        cols = 0;

        ++rows;

        if ((token = strtok(inputBuffer, separator))) {
            ++cols;

            while (token) {
                token = strtok(NULL, separator);

                if (token) {
                    ++cols;
                }
            }
        }

        /** If we reach the end of the line without having found any tokens 
         *  we have probably reached the last new line character before the end 
         *  of the file. Set the 'cols' value to the value of the last actual 
         *  line.
         *
         *  Also remember to correct the 'rows' variable by reducing it by one.
         *
         */
        if (!cols) {
            cols = prev;
            --rows;
        }
    }

    printf("%lu x %lu\n", rows, cols);

    return EXIT_SUCCESS;
}