来自文本文件的矩阵的维度

我想评估表单中方形矩阵的维数

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

所以在这种情况下n = 3,我的代码我能够读取所有整数的数量,但我想停在第一行并得到前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;
}
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读取线,因为如果你的矩阵实际上是一个方阵,那么你得到的线数就是矩阵的维数。干杯!

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;
}
热门标签: JAVA | c | c++ | go | swift | javascript | Nginx | 程序员 | 微服务 | 职场与管理 | Python | C# |