9 Replies - 5737 Views - Last Post: 18 January 2013 - 07:41 PM Rate Topic: -----

#1 Alkoclick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-January 13

Error in writing array to file

Posted 17 January 2013 - 04:35 AM

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



Is This A Good Question/Topic? 0
  • +

Replies To: Error in writing array to file

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Error in writing array to file

Posted 17 January 2013 - 04:44 AM

Can you please post the disp() function? I have a feeling the problem lies in that function.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 Alkoclick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-January 13

Re: Error in writing array to file

Posted 17 January 2013 - 04:49 AM

View Postraghav.naganathan, on 17 January 2013 - 04:44 AM, said:

Can you please post the disp() function? I have a feeling the problem lies in that function.

regards,
Raghav


It's all about a sudoku actually. For now, my temporary fix is to use this
fprintf(Current,"%d ",A[i][j]);
right before my code. If i use A[0][0] it still fails to correctly display it. As for the Disp, here it is


void disp(int A[9][9])
{
int i,j;
printf("\t");
for (i=0;i<9;i++)
    {

    if (i%3==0  && i!=0)  
    {
        printf("  ");
        for (j=0;j<9;j++)
            {
            if (j==3 || j==6)
                printf("|___");
            printf("|___");
            }
        printf("|\n\t");
    }


    for (j=0;j<10;j++)
        {
        if (j==3 || j==6)
            printf("  | ");
        printf("  | ");
        }
    printf("\n\t  ");


    for (j=0;j<9;j++)  
        {
        if (j==3 || j==6)
            printf("|   ");
        printf("| %d",A[i][j]);
        printf(" ");
        }
    printf("|\n\t");

    printf("  ");
    for (j=0;j<9;j++)  
        {
        if (j==3 || j==6)   
            printf("|___");
        printf("|___");
        }
    printf("|\n\t");
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 undefined behaviour  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 36
  • Joined: 17-January 13

Re: Error in writing array to file

Posted 17 January 2013 - 09:24 AM

Do you believe you were born with an understanding of scanf? Please read this page carefully and answer the following questions:
1. Which type of data does a "%d" format specifier tell scanf to write to?
2. What is the type of &a in your code?
3. What will scanf return when it encounters a match error? For example, consider when scanf expects to see a series of decimal digit characters, but is instead given 'a'.
4. Will the 'a' from the above example remain in stdin?
5. If the 'a' from the example does remain in stdin, what happens next time you use scanf("%d", ...)?
6. What will scanf return when it encounters EOF (eg. when you press CTRL+Z on Windows), or some other input error?
7. Let us assume you tell scanf to put two values into two variables. What should scanf return, assuming that the values were successfully put into their variables?

I would suggest using getchar from stdio.h instead of getch from conio.h, for portability reasons. C can be dangerous for people to learn by "trial and error", or by "example". Our time is better put to use when clarifying concepts for people who have already read books and manuals. I prefer not to quote from books for people who are too lazy to "learn by reading". In addition, you'll get a much more comprehensive "learn by reading" from a book than you will by asking this forum to debug your code for you.

ps. In case you're too lazy to read the manual for getchar, it returns an int. If you're sensible, that's the kind of data type you'll use for your "ans" variable...

pps. Go out and find a book! "The C Programming Language" by Kernighan & Ritchie will be okay. Do the exercises...
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

Re: Error in writing array to file

Posted 17 January 2013 - 09:52 AM

Also in the following snippet:
int i,j,A[9][9],k;
...
            if (ans=='Y' || ans=='y')
                A[9][9]=Clean(A);

You declared A to have a size of 9 so trying to acces element 9 is accessng your array out of bounds. Remember arrays in C start at zero and end at size - 1.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 Alkoclick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-January 13

Re: Error in writing array to file

Posted 17 January 2013 - 01:23 PM

View Postjimblumberg, on 17 January 2013 - 09:52 AM, said:

Also in the following snippet:
int i,j,A[9][9],k;
...
            if (ans=='Y' || ans=='y')
                A[9][9]=Clean(A);

You declared A to have a size of 9 so trying to acces element 9 is accessng your array out of bounds. Remember arrays in C start at zero and end at size - 1.

Jim


I am not accessing element [9][9], i am using a function which wipes out the entire array. It does not work in any other way i tried and i am not sure if it should.



View Postundefined behaviour, on 17 January 2013 - 09:24 AM, said:

-Lots of stuff here, read above-


I have carefully read and understood 50-80% of the page you have posted. However at this point, i am pretty certain that you are way out of point. You see, the problem does not arise when scanning, but rather when printing. There is no "scanf" in case 2, however, this is the case that produces the problem. It is obvious that i am not asking for debugging, as i have already found a way around the problem. I am asking out of sheer curiosity, hoping that someone may take the time to figure out what causes this error, so we may all learn our bit (including whoever may stumble on this page).

You automatically assumed that i am a lazy student who has read no books or manuals and was asking for desperate help to get through a class he does not deserve to pass. Sorry, i am not. I am using a book and i am doing the excercises, that's what got me here! I am not asking you to teach me something, nor do i desire it. I am asking you if you can figure what the problem is. That's all.


Thanks to all those who have taken the time to reply.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

Re: Error in writing array to file

Posted 17 January 2013 - 02:52 PM

Quote

I am not accessing element [9][9], i am using a function which wipes out the entire array. It does not work in any other way i tried and i am not sure if it should.

This:
A[9][9]=Clean(A);

You are trying to assign a value to A[9][9], notice the equal sign, which is out of bounds for your array, which is not allowed. Please show your Clean() function.

Quote

You see, the problem does not arise when scanning, but rather when printing. There is no "scanf" in case 2,

Part of what he is saying is that you used the "%d" specifier in your scanf() on line 14 of your main() snippet. This specifier is used for an int, but a is defined as a short. There is a difference between the short and an int. You should be using a "%h" specifier for this short. When you use the wrong specifier with scanf you can get many undefined problems. Always use the correct format specifier with both the scanf() and printf() families of functions.

By the way why are you using a short instead of the int? With todays desktop systems the benifits of using a short are usually outweighed by the negatives.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 Alkoclick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-January 13

Re: Error in writing array to file

Posted 18 January 2013 - 02:55 PM

Yep, i realised what you said and fixed that. Now it only runs
Clean(A)
.
Clean() is pretty simple, it's:
int Clean(int A[9][9])
{
int i,j;
for (i=0;i<9;i++)
    for (j=0;j<9;j++)
        A[i][j]=0;
}



a has been changed to int, without any output difference. What negatives could using a short int have? Can you elaborate a bit? I did not know they had any differences other than size and the choice was based solely on reducing memory consumption (yes, by those few bits :P/>)

Sorry for not using code tags and btw how could i edit? It's usually easy for me to find that button but for some reason i can't spot it... :(/>

This post has been edited by jimblumberg: 18 January 2013 - 03:16 PM

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

Re: Error in writing array to file

Posted 18 January 2013 - 03:37 PM

To me the biggest drawback is that it is easy to forget that you need to use different specifiers with scanf() and printf() and that the range of numbers being held is much less.

In today's desktop machines the amount of storage space required for a particular type is of little consequence, unless used in a very very large array.

If your are still having problems with your program, post a complete program that we can compile.

Also tell us exactly what imputs you are suppling to your program, and show us what your program is producing and what you expect your program to produce. Be as specific as possible.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 undefined behaviour  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 36
  • Joined: 17-January 13

Re: Error in writing array to file

Posted 18 January 2013 - 07:41 PM

If you don't want to be called a lazy student, then answer my questions for yourself, rather than waiting for the others to point out that your problem was addressed in my questions. Otherwise, there is no sense in getting upset about being a lazy student. I'm still waiting for an answer to the questions...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1