Chapter 11.
Input/Output and Operating System
Home
|
#include <stdio.h>
int main(void)
{
char c = 'A';
char s[] = "Blue moon!";
printf("[%c]\n[%2c]\n[%-3c]\n[%s]\n[%3s]\n[%.6s]\n[%-11.8s]\n",
c, c, c, s, s, s, s);
return 0;
}
|
กก
|
#include <stdio.h>
int main(void)
{
int i = 123;
double x = 0.123456789;
printf("[%d]\n[%05d]\n[%7o]\n[%-9x]\n[%-#9x]\n[%10.5f]\n[%-12.5e]\n",
i, i, i, i, i, x, x);
return 0;
}
|
|
#include <assert.h>
#include <stdio.h>
#include <string.h>
#define MAXSTRING 100
int main(void)
{
char input[MAXSTRING];
char string[15];
char *val;
char c;
int cnt;
int i;
int n;
FILE *ifp;
ifp = fopen("input", "r");
assert(ifp != NULL);
val = fgets(input, MAXSTRING, ifp);
assert(val != NULL);
fclose(ifp);
/*
// Delete the newline at the end of the input string.
*/
n = strlen(input);
*(input + n - 1) = '\0';
cnt = sscanf(input, "%d , %*s %% %c %5s %s", &i, &c, string,
&string[5]);
printf("%s%s%s%c%s%d%s%s%s%s%s%d%s\n",
"---\n"
"Control string: \"%d , %*s %% %c %5s %s\"\n"
"\n"
" Input string: \"", input, "\"\n"
"\n"
"Here are the values obtained from the input string:\n"
"\n"
" c = ", c, "\n"
" i = ", i, "\n"
" string = ", string, "\n"
" string + 5 = ", string + 5, "\n"
"\n"
" number of successful conversions = ", cnt, "\n");
return 0;
}
|
|
45 , ignore_this % C read_in_this**
|
|
#include <stdio.h>
#include <stdlib.h>
void double_space(FILE *, FILE *);
void prn_info(char *);
int main(int argc, char **argv)
{
FILE *ifp, *ofp;
if (argc != 3) {
prn_info(argv[0]);
exit(1);
}
ifp = fopen(argv[1], "r"); /* open for reading */
ofp = fopen(argv[2], "w"); /* open for writing */
double_space(ifp, ofp);
fclose(ifp);
fclose(ofp);
return 0;
}
void double_space(FILE *ifp, FILE *ofp)
{
int c;
while ((c = getc(ifp)) != EOF) {
putc(c, ofp);
if (c == '\n')
putc('\n', ofp); /* found a newline - duplicate it */
}
}
void prn_info(char *pgm_name)
{
printf("\n%s%s%s\n\n%s%s\n\n",
"Usage: ", pgm_name, " infile outfile",
"The contents of infile will be double-spaced ",
"and written to outfile.");
}
|
|
PROGRAM 115 : backwards.c
|
|
/* Write a file backwards. */
#include <stdio.h>
#define MAXSTRING 100
int main(void)
{
char fname[MAXSTRING];
int c;
FILE *ifp;
fprintf(stderr, "\nInput a filename: ");
scanf("%s", fname);
ifp = fopen(fname, "rb"); /* binary mode for MS_DOS */
fseek(ifp, 0, SEEK_END); /* move to end of the file */
fseek(ifp, -1, SEEK_CUR); /* back up one character */
while (ftell(ifp) > 0) {
c = getc(ifp); /* move ahead one character */
putchar(c);
fseek(ifp, -2, SEEK_CUR); /* back up two characters */
}
return 0;
}
|
|
PROGRAM 116 : change_case.c
|
|
/* Change the case of letters in a file. */
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h> /* use io.h in MS_DOS */
#define BUFSIZE 1024
int main(int argc, char **argv)
{
char mybuf[BUFSIZE], *p;
int in_fd, out_fd, n;
in_fd = open(argv[1], O_RDONLY);
out_fd = open(argv[2], O_WRONLY | O_EXCL | O_CREAT, 0600);
while ((n = read(in_fd, mybuf, BUFSIZE)) > 0) {
for (p = mybuf; p - mybuf < n; ++p)
if (islower(*p))
*p = toupper(*p);
else if (isupper(*p))
*p = tolower(*p);
write(out_fd, mybuf, n);
}
close(in_fd);
close(out_fd);
return 0;
}
|
กก
[Last Update: 2001.5.25] Dongseo University Cyber Campus
|