www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/24/03:01:43

From: "Mark Figura" <nospam AT please DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: reading/writing to COM ports
Date: Mon, 24 Aug 1998 01:56:48 -0400
Organization: "SNET dial access service"
Lines: 62
Message-ID: <6rquoj$dt0@news1.snet.net>
References: <19980823 DOT 222639 DOT 6511 DOT 0 DOT tonyblaha AT juno DOT com>
NNTP-Posting-Host: sttn-sh4-port221.snet.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hello

>Second (and less important) why in the following code does it output the
>strings reversed?

>#include <STDIO.H>
>
>int main()
>{
>
>char strA[255], strB[255], strC[255];
>FILE *fp;
>
>if(!(fp=fopen("TEST", "w"))) {
> printf("Cannot open test file.");
> exit(1);
>}
> SNIPPED getting strings from user...
>fprintf(fp, "%s %s %s", strA, strB, strC);
>
>fclose(fp);
>
>if(!(fp=fopen("TEST", "w"))) {
> printf("Cannot open test file.");
> exit(1);
>}

^^^ That's your problem right there.  You can't read from a file that you
opened for writing.  You have to use "r" instead of "w".  Or you can use
"r+" or "w+" to read and write from that file.

>fscanf(fp, "%s %s %s", strC, strB, strA);

^^^ Now, since you aren't reading these from the file (because it's not open
for reading), the variables aren't changing.

>
>printf("\n%s\n%s\n%s", strC, strB, strA);

^^^ These strings are the ones that you read from the keyboard.  That's why
they're in the wrong order.

>
>fclose(fp);
>
>return(0);
>}
>
>TIA,
>Tony Blaha


Sorry, can't help you with that modem question...

Instead of using "fscanf(stdin, blah blah)", you can just do "scanf(blah
blah)", but other than that, I think you did it the most compact way you
could.

Good luck!
Mark


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019