linux标准I/O——格式化输出
生活随笔
收集整理的這篇文章主要介紹了
linux标准I/O——格式化输出
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1.格式化輸出函數
- 2.輸出日期到文本文檔和數組中
- 3.練習
- 3.1 題目
- 3.2 提示
- 3.3 實現
1.格式化輸出函數
2.輸出日期到文本文檔和數組中
#include<stdio.h>int main() {int year=2021,month=4,date=8;FILE *fd;char buf[64];fd=fopen("data.txt","a+");fprintf(fd,"%d-%d-%d\n",year,month,date);sprintf(buf,"%d-%d-%d\n",year,month,date);int count=0;while(buf[count]!='\0'){count++;}printf("buf=%scount=%d\n",buf,count);return 0; }3.練習
3.1 題目
3.2 提示
3.3 實現
#include<stdio.h> #include<unistd.h> #include<time.h> #include<string.h>int main() {FILE *fp;char buf[64];int line=0;time_t t;struct tm* tp;fp=fopen("./test.txt","a+");if(fp==NULL){perror("fopen error");return -1;}while(fgets(buf,64,fp)!=NULL){if(buf[strlen(buf)-1]=='\n'){line++;}}while(1){time(&t);tp=localtime(&t);fprintf(fp,"%02d,%d-%02d-%02d %02d:%02d:%02d\n",++line,tp->tm_year+1900,tp->tm_mon+1,tp->tm_mday,tp->tm_hour,tp->tm_min,tp->tm_sec);fflush(fp);sleep(3);}fclose(fp);return 0; }總結
以上是生活随笔為你收集整理的linux标准I/O——格式化输出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MFC关键技术-动态创建
- 下一篇: 【计算机网络复习】1.1.3 速率相关的