从txt文件创建int数组,在C中数字之间有多行和空格

标签 c arrays file sudoku

我有一个数独检查器。我需要将 sudoku.txt 的数字捕获到 C 中的整数数组中。

我的数独.txt 是多行的,数字之间用“”空格分隔。

6 2 4 5 3 9 1 8 7
5 1 9 7 2 8 6 3 4
8 3 7 6 1 4 2 9 5
1 4 3 8 6 5 7 2 9
9 5 8 2 4 7 3 6 1
7 6 2 3 9 1 4 5 8
3 7 1 9 5 6 8 4 2
4 9 6 1 8 2 5 7 3
2 8 5 4 7 3 9 1 6

我想像这样加载数组中的所有数字。

例如:

int array[]={6, 2, 4, 5, 3, 9, 1, 8, 7, 5, 1, 9, 7, 2, 8, 6, 3, 4, 8, 3, 7, 6, 1, 4, 2, 9, 5, 1, 4, 3, 8, 6, 5, 7, 2, 9, 9, 5, 8, 2, 4, 7, 3, 6, 1, 7, 6, 2, 3, 9, 1, 4, 5, 8, 3, 7, 1, 9, 5, 6, 8, 4, 2, 4, 9, 6, 1, 8, 2, 5, 7, 3, 2, 8, 5, 4, 7, 3, 9, 1, 6};

我看过很多 txt 到整数数组的帖子,例如 12345678 或 1 2 3 4 5 6 7 8 一行。 但还没有多行并用“”分隔。

代码:

//
//  main.c
//  sudoku
//
//  Created by Ramón Serrano López on 23/1/15.
//  Copyright (c) 2015 Ramón Serrano López. All rights reserved.
//

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

void sudoku_checker(int N ,int a[]){
    int i,j;
    int count=0;

    /* This loop calculate the sum of each row */
    /* and check foreach if it equals to (1+2+3+4+5+6+7+8+9) = 45 o not*/
    for (i = 0; i < N*N; i = i+9) {
        int sumRow = 0;
        for (j = i; j < i+9; j++) {
            sumRow = sumRow + a[j];
        }
        if (sumRow != 45) {
            count++;
        }
    }

    /* This loop calculate the sum of each col*/
    /* and check foreach if it equals to (1+2+3+4+5+6+7+8+9) = 45 o not*/
    for (i = 0; i < N; i++) {
        int sumCol = 0;
        for (j = i; j < N*N; j = j+9) {
            sumCol = sumCol + a[j];
        }
        if (sumCol != 45) {
            count++;
        }
    }

    printf("Fails: %d\n", count);
    if(count==0)
        printf("OK\n");
    else
        printf("Successes\n");
}

int main()
{
    //info.txt it's the sudoku file
    FILE *myFile = fopen("/Users/ramonserranolopez/Desktop/SO/sudoku/sudoku/info.txt","r");
    int i, j, k, arrayofNumbers[81];
    if (myFile == NULL) {
        printf("the file could not be opened for reading\n");
    } else {
        for (i=0; i<81; i++) {
            fscanf(myFile, "%1d", &arrayofNumbers[i]);
        }
    }

    //provisional array to check if the function sudoku_cheker works
    //int a[]={6, 2, 4, 5, 3, 9, 1, 8, 7, 5, 1, 9, 7, 2, 8, 6, 3, 4, 8, 3, 7, 6, 1, 4, 2, 9, 5, 1, 4, 3, 8, 6, 5, 7, 2, 9, 9, 5, 8, 2, 4, 7, 3, 6, 1, 7, 6, 2, 3, 9, 1, 4, 5, 8, 3, 7, 1, 9, 5, 6, 8, 4, 2, 4, 9, 6, 1, 8, 2, 5, 7, 3, 2, 8, 5, 4, 7, 3, 9, 1, 6};

    //prints the array
    int N = 9;
    for (j = 0; j < N*N; j = j+4) {
        for (k = j; k < j+4; k++) {
            printf("  %1d  ", arrayofNumbers[k]);
        }
        printf("\n");
     }

     sudoku_checker(N,arrayofNumbers);
     return 0;
 }

最佳答案

即使您确实检查了文件是否已打开,但失败时您仍然继续阅读,这是错误的,此代码不会以这种方式失败,但我认为它也不会工作,因为您的文件显然不存在

另外,在打印循环中,您尝试打印比数组中更多的元素,我也修复了这个问题

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

void sudoku_checker(int N ,int a[])
{
    int i,j;
    int count;

    /* This loop calculate the sum of each row */
    /* and check foreach if it equals to (1+2+3+4+5+6+7+8+9) = 45 o not*/
    count = 0;
    for (i = 0; i < N * N ; i = i + 9) 
    {
        int sumRow = 0;
        for (j = i; j < i + 9; j++) 
            sumRow = sumRow + a[j];
        if (sumRow != 45)
            count++;
    }

    /* This loop calculate the sum of each col*/
    /* and check foreach if it equals to (1+2+3+4+5+6+7+8+9) = 45 o not*/
    for (i = 0; i < N; i++) 
    {
        int sumCol = 0;

        for (j = i ; j < N * N; j = j + 9) 
            sumCol = sumCol + a[j];

        if (sumCol != 45)
            count++;
    }
    printf("Fails: %d\n", count);

    if (count == 0)
        printf("OK\n");
    else
        printf("Successes\n");
}

int main()
{
    //info.txt it's the sudoku file
    int i, j, k, arrayofNumbers[81];
    FILE *myFile;
    int N = 9;
    int readCount = 0;

    myFile = fopen("file.dat", "r");
    if (myFile == NULL)
    {
        printf("the file could not be opened for reading\n");
        /* you should abort the program, if the file was not found */
        return -1; 
    }

    while (fscanf(myFile, "%1d", &arrayofNumbers[i]) == 1)
        readCount++;

    for (j = 0 ; j < readCount ; ++j) 
    {
        printf("  %1d  ", arrayofNumbers[j]);
        if ((1 + j) % 4 == 0)
            printf("\n");
    }
    sudoku_checker(N,arrayofNumbers);

    return 0;
}

关于从txt文件创建int数组,在C中数字之间有多行和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28172484/

相关文章:

java - Camel 分割文件和交换

string - Lua的string.find找不到模式

c - 这个反汇编如何对应给定的 C 代码?

c++ - U后缀的含义

c - 我的指针初始化错误吗?

java - 为什么我的 HashMap 无法注册(工作)

c++ - 如何对整数数组实现归并排序?

c++ - 如何声明一个静态变量但不定义它

php - 如何比较内部数组键和值并在 PHP 中相应地设置变量的值?

c - 使用两个 fprintf 语句写入 C 中的文件