プログレスバー †
#include <stdio.h>
#include <time.h>
void hoge();
int prog(int val, int max_val);
main(){
int i,j;
int times=113;
printf("Program Start !\n\n");
for(i=0;i<times;i++){
hoge();
prog(i, times-1);
}
printf("\n");
printf("Finish\n");
}
void hoge(){
clock_t t;
t = clock() + CLOCKS_PER_SEC/20;
while(t>clock());
}
int prog(int val, int max_val) {
int i;
int div = 20;
printf("|");
for (i=0; i<div; i++) {
if ((i+1)*(1e0*max_val/div) <= val) {
printf("#");
}
else {
printf(" ");
}
}
printf("|\n");
printf("%3d%%\n", 100*val/max_val);
if (val != max_val) {
printf("\x1b[2A");
}
return 0;
}
置換 †
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PRE_DISP "Unknown"
#define REP_DISP 134
int main(void)
{
FILE *fp;
char *buf;
unsigned int len;
int i;
char *search_p;
int search_index;
unsigned int pre_disp_len;
unsigned int final_len;
fp = fopen("test.txt", "r+");
fseek(fp, 0, SEEK_END);
len = ftell(fp);
buf = (char *)malloc(len+1);
fseek(fp, 0, SEEK_SET);
for (i=0;i<len;i++) {
buf[i] = getc(fp);
}
buf[len] = '\0';
printf("%s\n", buf);
search_p = strstr(buf, PRE_DISP);
if (search_p==NULL) return 0;
search_index = search_p - buf;
pre_disp_len = strlen(PRE_DISP);
fseek(fp, 0, SEEK_SET);
for (i=0;i<search_index;i++) {
putc(buf[i], fp);
}
fprintf(fp, "%d", REP_DISP);
for (i=search_index+pre_disp_len;i<len;i++) {
putc(buf[i], fp);
}
final_len = ftell(fp);
if (final_len<len) {
for (i=0;i<(len-final_len);i++) {
putc('\n', fp);
}
}
free(buf);
fclose(fp);
}
getopt †
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int opt;
opterr = 0;
/* if (argc != 2) { */
/* printf("Error : only one option is allowed\n"); */
/* return -1; */
/* } */
while ((opt = getopt(argc, argv, "da:l:")) != -1) {
switch (opt) {
case 'd':
if (optind != 2 || argc != 2) {
printf("Error : When d option is set, any other option can't be set\n");
return -1;
}
printf("d option\n");
break;
case 'a':
if (optarg[0] == '-') {
printf("a option\n");
optind -= 1;
}
else {
printf("a option [%s]\n", optarg);
}
break;
case 'l':
if (optarg[0] == '-') goto OPTION_VALUE_ERROR;
printf("l option [%s]\n", optarg);
break;
case '?':
if (!strcmp(argv[optind-1], "-a")) {
printf("a option\n");
continue;
}
else if (!strcmp(argv[optind-1], "-l")) {
printf("%c option needs value\n", argv[optind-1][1]);
return -1;
}
printf("%s is unknown option\n", argv[optind-1]);
break;
default:
printf("default\n");
}
}
printf("\n%d option detected\n", optind-1);
return 0;
OPTION_VALUE_ERROR:
printf("Error : %c option needs value\n", opt);
return -1;
}
最新の20件
2021-03-31
2020-06-09
2020-05-30
2019-12-02
2018-10-18
2018-07-05
2018-01-11
2015-12-07
2015-08-03
2015-07-20
2012-07-19
2012-06-30
2012-06-23
2012-01-25
2011-11-09
2011-11-08
2011-10-02
今日の6件
- counter: 126
- today: 1
- yesterday: 0
- online: 1