C 文本文件中矩阵的维数

C 文本文件中矩阵的维数,c,C,我想计算一个正方形矩阵的维数 -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 fil

我想计算一个正方形矩阵的维数

-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;
}
#包括
int main(){
内部温度;
int n=0;
FILE*FILE=fopen(“矩阵”、“r”);
if(file==NULL){
printf(“无法打开指定的文件”);
返回1;
}
而(fscanf(文件“%d”,&temp)==1){
n++;
}  
fclose(文件);
printf(“%d”,n);
返回0;
}

我可能有点过于复杂,但如果我必须这样做,我会这样做

#包括
#包括
int main()
{
文件*FILE_in=fopen(“mat1”、“r”);
字符c;
内部温度;
int n=0;
如果(在中存档)
{
//一个接一个地获取字符并检查
//如果是行尾字符或EOF
while((c=(char)fgetc(file_in))!='\n'和&c!=EOF)
{
//如果不是,你就把它放回小溪
ungetc(c,文件中);
//扫描你的号码
fscanf(文件在“%d”中,&temp);
n++;
}
fclose(文件输入);
}
其他的
{
fprintf(stderr,“无法打开指定的文件。\n”);
返回1;
}
printf(“数字数量:%d\n”,n);
返回0;
}
也许这回答了你的问题。。。 不过,我认为(如上所述)查看
fgets
要简单得多。你甚至不需要
sscanf
read行,因为如果你的矩阵实际上是一个正方形矩阵,那么你得到的行数就是矩阵的维数。
干杯

我决定一行一行地做,而不是一个字符一个字符地做,虽然我认为这不必要地使这个特定示例复杂化,但您应该能够看到如何从这个示例中构建

例如,
token
变量只是在那里无用地保存了一次,它被用作
while
循环的检查。实际上,您可以在找到这些值时开始在中读取这些值,或者第二次遍历该文件,完全了解
参数,因此,只需对值进行
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;
}