/* Program to create backup of the |
AUTOEXEC.BAT file */ |
#include <stdio.h> |
int main(void) |
{ |
FILE *in, *out; |
if ((in = fopen("\\\\AUTOEXEC.BAT", "rt")) |
== NULL) |
{ |
fprintf(stderr, "Cannot open input \\ |
file.\ "); |
return 1; |
} |
if ((out = fopen("\\\\AUTOEXEC.BAK", "wt")) |
== NULL) |
{ |
fprintf(stderr, "Cannot open output \\ |
file.\ "); |
return 1; |
} |
while (!feof(in)) |
fputc(fgetc(in), out); |
fclose(in); |
fclose(out); |
return 0; |
} |
#include <stdio.h> |
int main() |
{ |
fprintf(stdout, "how are you\ "); |
fprintf(stderr, "Cannot open output\ "); |
fprintf(stdout, "There are %d desks", 4); |
return 0; |
} |
int old_xprintf(const char *format, long a1, long a2, long a3, long a4, |
long a5, long a6, long a7, long a8) |
{ |
printf(format,a1, a2, a3, a4, a5, a6, a7, a8); |
if(printer) |
fprintf(printer, format, a1, a2, a3, a4, a5, a6, a7, a8); |
if(logfile) |
fprintf(logfile, format, a1, a2, a3, a4, a5, a6, a7, a8); |
} |
int main(void) |
{ |
int a1 = 1, a2 = 2, a3 = 3, a4 = 4, a5 = 5, a6 = 6, a7 = 7, a8 = 8, |
a9 = 9; |
printer = fopen("test", "w+"); |
logfile = fopen("test2", "w+"); |
old_xprintf("%d,%d,%d,%d,%d,%d,%d,%d\ ", a1, a2, a3, a4, a5, a6, a7, a8); |
fclose(printer); |
fclose(logfile); |
return 0; |
} |
运行及输出: |
beyes@linux-beyes:~/C/base> ./vsprintf.exe |
1,2,3,4,5,6,7,8 |
beyes@linux-beyes:~/C/base> cat test |
1,2,3,4,5,6,7,8 |
beyes@linux-beyes:~/C/base> cat test2 |
1,2,3,4,5,6,7,8 |
#include <stdio.h>
int snprintf(char *str, size_t size, const char *format, ...);
#include <stdio.h>
int main (void)
{
char buf[10];
snprintf(buf, 10, "%d open", 12);
printf("%s\\n", buf);
snprintf(buf, 10, "%d over size", 12);
printf("%s\\n", buf);
return 0;
}
beyes@linux-beyes:~/C/base> ./snprintf.exe
12 open
12 over s
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |