经典编程900例(c语言)(第十五篇)

这些示例展示了C语言在处理时间、文件、环境变量和标准输出流方面的基本操作。包括读取文件并打印、显示PATH变量、在标准输出和错误流中输出字符串、将输入转换为大写、获取BIOS时间、计算处理器消耗时间、输出当前日期和时间、延迟程序执行以及显示UNIX时间戳。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例168:在控制台打印文件

#include <stdio.h>

int main(int argc, char const *argv[])
{
    FILE *fp;
    char line[255];  

    // 打开第一个程序参数指明的文件
    if ((fp = fopen(argv[1], "r")) == NULL)
        printf("Error opening %s\n", argv[1]);
    else
    {
        // 读一行打印一行
        while (fgets(line, sizeof(line), fp))
            fputs(line, stdout);
        fclose (fp);
    }
    return 0;
}

例169:在控制台打印PATH变量

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    char *entry;

    entry = getenv("PATH");

    if (*entry)
        printf("PATH=%s\n", entry);
    else
        printf("PATH is not defined\n");

    return 0;
}

例170:在标准输出流和错误流中输出用户输入的字符串

#include <stdio.h>

int main(int argc, char const *argv[])
{
    char buffer[256];

    while (fgets(buffer, sizeof(buffer), stdin))
    {
        fputs(buffer, stdout);
        fputs(buffer, stderr);
    }
    
    return 0;
}

例171:将输入字符串转为大写输出

#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    char line[255];

    while (fgets(line, sizeof(line), stdin))
        fputs(strupr(line), stdout);

    return 0;
}

例172:BIOS时间(tc环境编译)

#include <stdio.h>
#include <bios.h>

int main(int argc, char const *argv[])
{
    long ticks;

    ticks = biostime(0, ticks);

    printf("Ticks since midnight %ld\n", ticks);
    _bios_timeofday(_TIME_GETCLOCK, &ticks);
    printf("Seconds since midnight %f\n", ticks / 18.2);

    return 0;
}

例173:计算时间(老编译器)

#include <stdio.h>
#include <time.h>
#include <dos.h>

void main (void)
{
    clock_t processor_time;

    printf("Processor time consumed %ld\n",
           clock() / (long) CLK_TCK);

    delay(2000);

    printf("Processor time consumed %ld\n",
           (long) clock() / (long) CLK_TCK);

    delay(3000);

    printf("Processor time consumed %ld\n",
           clock() / (long) CLK_TCK);
}

例174:输出系统当前时间

#include <stdio.h>
#include <time.h>

int main(int argc, char const *argv[])
{
    time_t current_time;

    time(&current_time);

    printf("The current date and time: %s", ctime(&current_time));
    
    return 0;
}

例175:程序延迟5秒

#include <stdio.h>
#include <time.h>

int main(int argc, char const *argv[])
{
    time_t current_time;
    time_t start_time;

    printf("About to delay 5 seconds\n");

    time(&start_time);

    do {
      time(&current_time);
    } while ((current_time - start_time) < 5);

    printf("Done\n");

    return 0;
}

例176:程序延迟5秒(库函数计算时间差)

#include <stdio.h>
#include <time.h>

int main(int argc, char const *argv[])
{
    time_t start_time;
    time_t current_time;

    time(&start_time);

    printf("About to delay 5 seconds\n");

    do {
        time(&current_time);

    // difftime计算两个时刻之间的时间差
    } while (difftime(current_time, start_time) < 5.0);

    printf("Done\n");

    return 0;
}

例177:输出当天日期(老编译器)

#include <stdio.h>
#include <dos.h>

int main(int argc, char const *argv[])
{
    struct date curr_date;

    getdate(&curr_date);

    printf("Current date: %d-%d-%d\n", curr_date.da_mon, curr_date.da_day, curr_date.da_year);
     
    return 0;
}

例178:输出当前时间

#include <stdio.h>
#include <dos.h>

int main(int argc, char const *argv[])
{
    struct time curr_time;

    gettime(&curr_time);

    printf("Current time %02d:%02d:%02d.%02d\n", curr_time.ti_hour, curr_time.ti_min, curr_time.ti_sec, curr_time.ti_hund);
    return 0;
}

例179:输出UNIX系统时间

#include <stdio.h>
#include <dos.h>
#include <time.h>

void main (void)
{
    struct time dostime;
    struct date dosdate;

    time_t unix_format;
    struct tm *local;

    getdate(&dosdate);
    gettime(&dostime);

    unix_format = dostounix(&dosdate, &dostime);
    local = localtime(&unix_format);
    printf("UNIX time: %s\n", asctime(local));
}

例180:计算时区时差

#include <stdio.h>
#include <time.h>
#include <sys\timeb.h>

int main(int argc, char const *argv[])
{
    struct timeb timezone;

    tzset();

    ftime(&timezone);

    // 1970年1月1日以来的秒数(GMT)
    printf("Seconds since 1 January 1970 (GMT) %ld\n", timezone.time);
    printf("Fractional seconds %d\n", timezone.millitm);

    // GMT时间与本地区时差
    printf("Hours difference between GMT and local zone %d\n", timezone.timezone / 60);
    if (timezone.dstflag)
        printf("Daylight savings time active\n");
    else
        printf("Daylight savings time inactive\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值