Mail Archives: djgpp/1998/08/24/03:01:43
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 -