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

时间:2015-01-27 14:09:37

标签: c arrays file sudoku

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

我的sudoku.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

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

E.G:

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;
 }

1 个答案:

答案 0 :(得分:0)

即使您确实检查了文件是否已打开,但是当它失败时仍然继续读取,这是错误的,这段代码不会失败,但我认为它不起作用,因为你的文件显然没有&# 39; t存在

另外,在打印循环中,您尝试打印的元素多于阵列中的元素,我还修复了

#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;
}