Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Error in writing array to file

$
0
0
Hello and thanks for your time. I came across an unusual problem, while executing the following code. A[0][0], when written onto the text becomes a 0, while it should have been a 1. All other numbers are as they should be.



int main()
{
int i,j,A[9][9],k;
short int a;
char ans;
FILE *Current;
for (i=0;i<9;i++)
    for (j=0;j<9;j++)
        A[i][j]=i+1;
do
{
    disp(A); // This is a function i use to display the array

    scanf("%d",&a);
    switch (a)
    {
        case 1:
            if ((Current=fopen("Current.sud","r"))==NULL)
                printf("Error saving file\n");
            else
                {
                for (i=0;i<9;i++)
                    {
                    for (j=0;j<9;j++)
                        fscanf(Current,"%d",&A[i][j]);
            //        fprintf(Current,"\n");
                    }
                }
            fclose (Current);
            break;

        case 2:
            if ((Current=fopen("Current.sud","w"))==NULL)
                printf("Error opening file\n");
            else
                {
                for (i=0;i<9;i++)
                    {
                    for (j=0;j<9;j++)
                        fprintf(Current,"%d ",A[i][j]);
                    fprintf(Current,"\n");
                    }
                }
            fclose (Current);
            break;

        case 3:
            do
            {
            printf("Do you wish to clear this Sudoku? Y/N\n");
            ans=getch();
            }
            while ((ans!='Y') && (ans!='y') && (ans!='N') && (ans!='n'));
            if (ans=='Y' || ans=='y')
                A[9][9]=Clean(A);
            break;

}

}
while (a!=9);   // I also have a few other case, completely irrelevant, so i didn't post hem up


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>