[comp.lang.c] C Programming Problem

tab@auc.UUCP (Terrence Brannon ) (01/03/89)

Ok, this is my source:
---------------------

#include <stdio.h>

main()
{
	FILE	*fp;
	char	*a;
	int 	i, q, r, s, t, u, v;
		

	fp = fopen("tab.dat", "r");
	a = (char *) calloc (80, sizeof(char));

	for (i=0, i < 3; ++i);
	{
	fscanf(fp, "%s %d %d %d %d %d %d", a, q, r, s, t, u, v);
	printf("%s %d %d %d %d %d %d", a, q, r, s, t, u, v);
	}
}

And this is tab.dat:
-------------------	

I 11 16 88 22 20 10
O 11 16 88 22 23 21
I 11 16 88 22 23 30
O 11 16 88 22 23 39

When I do the following:
-----------------------
cc -g filename.c
sdb
r

I get a "doscan.c no such file or directory" error from sdb

This snippet of code works when i read character values from the 
file (ie: fscanf(fp, "%s %s %s %s %s %s %s", c1, c2, c3, c4, c5 ,c6, c7)
but if doesnt work when trying to read integer values from the file as
with the above fscanf in the source code.

Any ideas why valid integers from a file can't be read in as integers but
only can be read in a char strings?

...!gatech!auc!tab

gpasq@picuxa.UUCP (Greg Pasquariello X1190) (01/04/89)

In article <32229@auc.UUCP> tab@auc.UUCP (0-Terrence Brannon ) writes:
-Ok, this is my source:
----------------------
-
-#include <stdio.h>
-
-main()
-{
-	FILE	*fp;
-	char	*a;
-	int 	i, q, r, s, t, u, v;
-		
-
-	fp = fopen("tab.dat", "r");
-	a = (char *) calloc (80, sizeof(char));
-
-	for (i=0, i < 3; ++i);
-	{
-	fscanf(fp, "%s %d %d %d %d %d %d", a, q, r, s, t, u, v);
-	printf("%s %d %d %d %d %d %d", a, q, r, s, t, u, v);
-	}
-}
-
-Any ideas why valid integers from a file can't be read in as integers but
-only can be read in a char strings?
-
-...!gatech!auc!tab

They can, but you are doing it incorrectly.  Look at the man page for fscanf, 
and you'll see that the parameters that fscanf takes are stream, format, and
_POINTER_!  You are not passing pointers to ints, you are only passing ints.

Greg
-- 
=============================================================================
Dirt is the mother o' lunch 	  	Greg Pasquariello AT&T/EDS
					att!picuxa!gpasq  
=============================================================================

rock@cbnews.ATT.COM (Y. Rock Lee) (01/05/89)

In article <32229@auc.UUCP> tab@auc.UUCP (0-Terrence Brannon ) writes:
>
>	char	*a;
>	int 	i, q, r, s, t, u, v;
>
>	a = (char *) calloc (80, sizeof(char));
>	fscanf(fp, "%s %d %d %d %d %d %d", a, q, r, s, t, u, v);

>I get a "doscan.c no such file or directory" error from sdb

"doscan.c" is part of the standard C library and can be found in
/usr/src/lib/libc/port/stdio.c.
You can include it in the sdb's source list (see sdb(1)),
but there's no reason to do this. Simply ignore the warning message.

>This snippet of code works when i read character values from the file ... 
>but if doesnt work when trying to read integer values from the file ...

"fscanf(fp, "%s %d %d %d %d %d %d", a, &q, &r, &s, &t, &u, &v);"
 will do it.

rae98@wash08.UUCP (rae98) (01/05/89)

In article <32229@auc.UUCP> tab@auc.UUCP (0-Terrence Brannon ) writes:
>	int 	i, q, r, s, t, u, v;
>	fscanf(fp, "%s %d %d %d %d %d %d", a, q, r, s, t, u, v);
>
>Any ideas why valid integers from a file can't be read in as integers but
>only can be read in a char strings?
>
>...!gatech!auc!tab

Yes.  You'll probably get a hundred responses, but scanf requires
pointers for values, so the line should read:

fscanf(fp, "%s %d %d %d %d %d %d", a, &q, &r, &s, &t, &u, &v);

This should take care of your problem.
-- 
-------------------------------------------------------
UUNET:	uunet!wash08!rae98		should work.
BITNET:	I don't know...can you tell me?
UUCP:	rae98@wash08.UUCP		does this work?

laman@ivory.SanDiego.NCR.COM (Mike Laman) (01/05/89)

In article <32229@auc.UUCP> tab@auc.UUCP (0-Terrence Brannon ) writes:
	:
	:
	:
>#include <stdio.h>
>
>main()
>{
>	FILE	*fp;
>	char	*a;
>	int 	i, q, r, s, t, u, v;
>		
>
>	fp = fopen("tab.dat", "r");
>	a = (char *) calloc (80, sizeof(char));
>
>	for (i=0, i < 3; ++i);
		^            ^
	 Change to ';'   Remove this ';'.  Now you will get 3 loop iterations.
	 Syntax error?! I guess you typed in this one for the message.

>	{
>	fscanf(fp, "%s %d %d %d %d %d %d", a, q, r, s, t, u, v);

	Change this fscanf() to:
	fscanf(fp, "%s %d %d %d %d %d %d", a, &q, &r, &s, &t, &u, &v);
				You must pass the data addresses to fscanf()
				so it can change your data variables!
				The "a" variable is the address of the area;
				that's why "a" was proprly initialized.
				Next time read the manual more carefully!

>	printf("%s %d %d %d %d %d %d", a, q, r, s, t, u, v);
>	}
>}

Mike Laman

guy@auspex.UUCP (Guy Harris) (01/07/89)

>"doscan.c" is part of the standard C library and can be found in
>/usr/src/lib/libc/port/stdio.c.

Assuming you have source; MANY people with UNIX don't (something often
forgotten by many of those who do).  (If you *don't* have UNIX, you may
not have source either, and even if you do it's quite likely not to be
in that location.)