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

从txt文件创建int数组,在C中使用多行和数字之间的空格,c,arrays,file,sudoku,C,Arrays,File,Sudoku,我有一个数独棋盘。我需要将sudoku.txt的数字捕获到C中的整数数组中 我的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 我想像这样加载数组中的所有数字 例如: 我见

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

我的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
我想像这样加载数组中的所有数字

例如:

我见过很多txt到INT数组的帖子,比如12345678或12345678一行。 但多行和分隔还没有

代码:


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

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

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

请注意,您没有检查fopen成功与否的任何具体原因?另外,请详细说明这不起作用。@iharob检查它是确定的,发布一个试图读取文件然后打印它的完整程序。为什么你让人们很难帮助你?整个计划都没有公布。你的主要职能是什么?你的include指令在哪里?您需要发布一个完整的可编译程序,尽可能简短地演示该问题。没有这个,,你在浪费每个人的时间。如果我使用你的代码,它会说我的文件存在并按如下方式打印数组:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1606418432 32767-2102815162-332136427-148998249 14956788624-1188721324 167778272 1606416480 32767 16 0 1 0 104 1 1606578576 32767 1606416240 32767 1606423158 32767 1606416264 32767 1检查文件内容,尝试更新的代码,我刚刚注意到,您没有检查是否从文件中读取了值,例如,在leidos,los valores del arreglo quedan sinicializar y vas a obtener lo que llaman valores basura como los que tu program a estáimimimiendo中。它是固定的,并且是有效的。格雷西亚斯·波拉·阿尤达@iharob
//
//  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;
}