如何阅读c中的格式化时间 [英] How to read formating hour in c

查看:144
本文介绍了如何阅读c中的格式化时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从c中的文本文件中读取格式化小时(HH:MM:SS)并将每个数字传递到罚款':'到三个整数: h,m,s



怎么可能?



我写了代码将字符串传递给char但我不知道这是不是最好的选择。



I want to read a formating hour ( HH:MM:SS) from a text file in c and pass every number until to fine ':' to three ints : h , m, s

How can be possible?

I wrote the code to pass the string to a char but i don't know if that's the best option.

/* Source Code to read a text of string from a file. */

#include <stdio.h>
#include <stdlib.h> /* For exit() function */
int main()
{
	
   char c[1000];
   FILE *input;
   if ((input = fopen("time.in","r"))==NULL){
       printf("Error! opening file");
       exit(1);         /* Program exits if file pointer returns NULL. */
   }
   
   fscanf(input,"%[^\n]",c);
   printf("Data from file:\n%s",c);
   fclose(input);
   
   
   
   return 0;
}

推荐答案

如果你的谷歌有几分钟就很容易:

it is so easy if your google some minutes:
int hour, min, sec;
fscanf( input, "%ld:%ld:%ld", &hour, &min, &sec);


这篇关于如何阅读c中的格式化时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆