Try to debug this code, fscanf_s does not read from an input text file
numbers.txt that has 20 integers separated by a space. What I am doing
wrong ??

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

#define SIZE 20 /* size of elements in the the sequence of numbers */

int _tmain(int argc, _TCHAR* argv[])
{
int array[SIZE];
int i, pass, hold;
FILE *fp;

/* opens the file in in read only mode */
if(fopen_s(&fp,"d:\\numbers.txt","r")!=0)
{
printf("Error opening the file\n");
exit(1);
}
/* load the data in the array */

else
{
for(i=0;i<=SIZE-1;i++)
{
fscanf_s(fp,"%d",&array[i]);
}
}
fclose(fp);
/* show data */
for(i=0;i<=SIZE-1;i++)
{
printf("%d ", array[i]);
}
printf("\n");
return 0;
}

Re: Why fscanf_s does not work ?? by jean-f-j

jean-f-j
Mon Jun 05 22:07:25 CDT 2006


> numbers.txt that has 20 integers separated by a space. What I am doing
> wrong ??

What is the error message ?

--
jean-f-j

Re: Why fscanf_s does not work ?? by Tim

Tim
Tue Jun 06 00:50:45 CDT 2006

"Fernik" <fernik@gmail.com> wrote:
>
>Try to debug this code, fscanf_s does not read from an input text file
>numbers.txt that has 20 integers separated by a space. What I am doing
>wrong ??

I don't know. Your code works perfectly for me. What are you seeing?

By the way, in C, it is more traditional to write this:
for( i = 0; i < SIZE; i++ )
than this:
for( i = 0; i <= SIZE-1; i++ )
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Re: Why fscanf_s does not work ?? by Fernik

Fernik
Tue Jun 06 08:06:43 CDT 2006

I see memory addresses, even while I am debugging the for loop I do not
see the array to be modified. I am using VC++ 2005 Standard Ediiton.

Fernik

Tim Roberts ha escrito:

> "Fernik" <fernik@gmail.com> wrote:
> >
> >Try to debug this code, fscanf_s does not read from an input text file
> >numbers.txt that has 20 integers separated by a space. What I am doing
> >wrong ??
>
> I don't know. Your code works perfectly for me. What are you seeing?
>
> By the way, in C, it is more traditional to write this:
> for( i = 0; i < SIZE; i++ )
> than this:
> for( i = 0; i <= SIZE-1; i++ )
> --
> - Tim Roberts, timr@probo.com
> Providenza & Boekelheide, Inc.


Re: Why fscanf_s does not work ?? by Tim

Tim
Thu Jun 08 00:12:29 CDT 2006

"Fernik" <fernik@gmail.com> wrote:
>
>I see memory addresses, even while I am debugging the for loop I do not
>see the array to be modified. I am using VC++ 2005 Standard Ediiton.

Are you talking about what you see in the debugger? &array[i] *IS* an
address. It's no surprise if it shows as one. Is the output correct?
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Re: Why fscanf_s does not work ?? by Fernik

Fernik
Thu Jun 08 07:53:59 CDT 2006

Yes in the debugger, I want to see the contents of the array and I see
random numbers, event in the output of the printf function, it seems
that fscanf does not read the integers fropm the file

Fernik

Tim Roberts ha escrito:

> "Fernik" <fernik@gmail.com> wrote:
> >
> >I see memory addresses, even while I am debugging the for loop I do not
> >see the array to be modified. I am using VC++ 2005 Standard Ediiton.
>
> Are you talking about what you see in the debugger? &array[i] *IS* an
> address. It's no surprise if it shows as one. Is the output correct?
> --
> - Tim Roberts, timr@probo.com
> Providenza & Boekelheide, Inc.


Re: Why fscanf_s does not work ?? by Tim

Tim
Fri Jun 09 23:33:15 CDT 2006

"Fernik" <fernik@gmail.com> wrote:
>
>Yes in the debugger, I want to see the contents of the array and I see
>random numbers, event in the output of the printf function, it seems
>that fscanf does not read the integers fropm the file

How can you possibly come to that conclusion? The OUTPUT of the program is
correct, so clearly fscanf IS reading the integers from the file. Now, the
Visual Studio debugger sometimes has trouble locating the values you want
to see, especially in a release build with optimization turned on. Blame
the fancy new debugger, not an API that's been studied and understood for
more than 30 years.
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.