[comp.lang.c] fscanf question

SMITHJ@ohstpy.mps.ohio-state.edu (12/28/89)

Sorry if this has come up before but I'm puzzled as to why the following code
doesn't work.  The fprintf works fine but nothing is read in by the fscanf.

Thanx in advance.

#include <stdio>

int main(void)
{
	FILE *stream;
	char outstr1[]="Trying", outstr2[]="Hoping";
	char instr1[30], instr2[30];

	stream = fopen("try2.gbk","w");
	fprintf(stream,"%-30s %-30s",outstr1,outstr2);
	fclose(stream);

	stream = fopen("try2.gbk","r");
	fscanf(stream,"%-30s %-30s",instr1,instr2);
	fclose(stream);
}

--
I knew how the game was going to end when I took Brenda 
	into the filing room but I took her in anyway.	--Al
They have one big advantage over us: 
		*they* know where they're going.	--Batman
Has your family tried 'em, Powdermilk?			--Garrison Keillor

/* Jeffery G. Smith, BS-RHIT (AKA Doc. Insomnia, WMHD-FM)       *
 *    The Ohio State University, Graduate Physics Program       *
 *        3193 Smith Lab, Columbus, OH 43210  (614) 292-5321    *
 *    smithj@ohstpy.mps.ohio-state.edu                          */

cpcahil@virtech.uucp (Conor P. Cahill) (12/28/89)

In article <6983@ohstpy.mps.ohio-state.edu>, SMITHJ@ohstpy.mps.ohio-state.edu writes:
> 	fscanf(stream,"%-30s %-30s",instr1,instr2);
		        ^     ^ these are the problem.

(F)scanf does not understand the left hand justification indicator (-).  
Delete the dashes and it will work as you want it to.

-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

dskim@eng.umd.edu (Daeshik Kim) (12/28/89)

In article <6983@ohstpy.mps.ohio-state.edu> SMITHJ@ohstpy.mps.ohio-state.edu writes:
>
>#include <stdio>
	oops, "...h"
>
>	char instr1[30], instr2[30];
>
>	fprintf(stream,"%-30s %-30s",outstr1,outstr2);
>	fclose(stream);

	Perhaps out of the topic;
	You may want to put 'EOF' before closing the "ASCII TEXT" file.

>	fscanf(stream,"%-30s %-30s",instr1,instr2);

	I do NOT think you want to use '-' here!

--
	Daeshik Kim	H: (301) 445-0475/2147 O: (703) 689-7308 (M,W,F)
	SCHOOL:	dkim@cscwam.umd.edu (uunet!haven!cscwam.umd.edu!dkim)
		dskim@eng.umd.edu (uunet!haven!eng.umd.edu!dskim)
	WORK:	dkim@daffy.uu.net (uunet!daffy!dkim)

randolph@ektools.UUCP (Gary L. Randolph) (01/03/90)

In article <6983@ohstpy.mps.ohio-state.edu> SMITHJ@ohstpy.mps.ohio-state.edu writes:
/Sorry if this has come up before but I'm puzzled as to why the following code
/doesn't work.  The fprintf works fine but nothing is read in by the fscanf.
/#include <stdio>
***********>you mean stdio.h, right?
/
/int main(void)
/{
/       FILE *stream;
/       char outstr1[]="Trying", outstr2[]="Hoping";
/       char instr1[30], instr2[30];
/
/       stream = fopen("try2.gbk","w");
***********>In the 'real' world, of course, you check
that this worked.
/       fprintf(stream,"%-30s %-30s",outstr1,outstr2);
/       fclose(stream);
/
/       stream = fopen("try2.gbk","r");
/       fscanf(stream,"%-30s %-30s",instr1,instr2);
**********>the minus sign says left justify the *output*, this is all that
**********>is confusing your compiler. Take them out, and all is well.
/       fclose(stream);
/}

And I can't help it,

                WAR EAGLE!!!!!!!!!!!!!!!!!!!!!!!!

                        Gary