I tryed that too, but it didn't work at all. I can get around that, though, if I can change the name of the .txt file according to a string. How would I do that?
-Matt
I tryed that too, but it didn't work at all. I can get around that, though, if I can change the name of the .txt file according to a string. How would I do that?
-Matt
You mean with sprintf() or something?
Can you be more specific?Code:char command[BUFSIZ]; sprintf(command, "print \"%s\\%s\"", "C:\\", "text.txt"); system(command);
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
More specific:
So, to open the file for what is stored in char filename, I would do:
Except I don't want the error messages. Any advice?Code:char filename[4] = 'text'; flpt = fopen("C:\\Documents and Settings\\USERNAME\\Desktop\\%s.txt","w",filename);
Thanks
-Matt
filename in your example needs to be
or better yet, leave the size out. And it appears that you do indeed want sprintf, as so:Code:char filename[5] = "text";
and then you can open real_filename.Code:char real_filename[128]; /* some large enough number */ sprintf(real_filename, "C:\\Documents and Settings\\USERNAME\\Desktop\\%s.txt", filename);
As an aside, I think I'll try to use "USERNAME" the next time I register for something....
In case tabstop wasn't clear: only the printf() and scanf() family of functions uses format specifiers like %s and %d. All other standard C functions generally just take single strings. If you need to use strings with format specifiers with other functions, you can use sprintf() first to create a single string from your printf-style arguments.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
It should definitely work - remove does return an error code, so perhaps something like this:
Also, bear in mind the above comments on filenames and 8.3 format if you are using for example Turbo C to make a DOS executable (although this should apply to when you are creating the file too).Code:if (remove(...) != 0) { perror("Remove(...):"); // This will show an error such as "file not found" or such things exit(1); // May not be the right thing. }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
As far as I'm aware, perror() automatically adds a colon to the message.
(I guess perror() must also set errno to whatever corresponds to illegal seeks on my system, too.)Code:$ cat ./perror.c #include <stdio.h> int main() { perror(""); perror("function"); perror("function:"); return 0; } $ ./perror Success function: Illegal seek function:: Illegal seek $
So the best way to use perror is something like this:
Code:perror("remove"); perror("remove()"); perror(filename); fprintf(stderr, "remove(): %s: ", filename), perror("");
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
New problem. Everything worked, except when I got to the compile screen, it said it was printing, but it didn't. Any ideas as to why not?
Is that XP command 100%, because everything worked, but my printer did nothing? Why?
Okay, I tried fclose, but that didn't hep. Here's what I have:
Code:#include <stdio.h> #include <stdlib.h> int main() { FILE *flpt; flpt = fopen("C:\\Documents and Settings\\alpha\\Desktop\\Test.txt", "w"); fprintf(flpt, "Test print.\n\nTest print.\n\nTest print.\n\n"); fclose(flpt); system("PRINT /D:LPT1 \"C:\\Documents and Settings\\alpha\\Desktop\\Test.txt\""); }
Last edited by mcotter222; 04-21-2008 at 08:41 PM. Reason: Updated the code.
I'm pretty sure this only works for "DOS compatible printers" - I just tried on my machine, and it queued the print job, but it's not printing to my default printer, but the one on LPT1:, which is no longer available on my machine.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I edited the code. Change the LPT1 to the printer your port is in, then try it again.
Did it work for anyone? I editted it again, and this time it removes the file aswell.
Sorry, here is the newest code:
Code:#include <stdio.h> #include <stdlib.h> int main() { FILE *flpt; flpt = fopen("C:\\Documents and Settings\\alpha\\Desktop\\Test.txt", "w"); fprintf(flpt, "Test print.\n\nTest print.\n\nTest print.\n\n"); fclose(flpt); system("PRINT /D:LPT1 \"C:\\Documents and Settings\\alpha\\Desktop\\Test.txt\""); remove("C:\\Documents and Settings\\alpha\\Desktop\\Test.txt"); }