Question: I have the following variables:

```
int year = 2013;
int month = 04;
int day = 07;
int hour = 13;
int min = 42;
int sec = 00;

```

...and I need to extract the "epoch" time, or unix time, from them.

How can this be done in standard C?
In this case, the unix time should be: [1365342120](http://www.epochconverter.com/).

Answer: 
A simple way to do this is to read a line with `fgets` in a loop and then just run something like this:

```
sscanf(buf, "%*4d%4d", &a);

```

This will ignore the first four digits, and copy the last four digits into integer `a`.

There's obviously no error checking there (it'll only work if there are 8 digits per line), but it's quick and easy. You could check that the return of `scanf` is 1, which will be the case for a successful 4 digit number read.

**Edit** (by @Grijesh Chauhan)

Sample code:

```
int main(){
    FILE* fp = fopen("file","r");
    char buf[128];
    int a;
    while(!feof(fp)){             // while end of file comes
      fgets(buf, 127, fp);         //read complete line in a buffer 
      sscanf(buf, "%*4d%4d", &a);  //scanf from buffer, last 4 digits
      printf("%d\n",a);         
    }
    return EXIT_SUCCESS;
}

```

and it executes: 

```
$ gcc y.c -Wall -o y
$ ./y
5678
5432
8324
3223

```

for a file: 

```
$ cat file
12345678
98765432
45678324
98653223

```
Following technique in C can be useful to read a file like I have shown below, Is this what you wants? 

while EOF not reach: 

* first read 4 chars in first buffer, and unused
* read number in a line, using `fscanf()`, `%d`
* read `\n`
* print number

The file is like: 

```
$ cat file
12345678
98765432
45678324
98653223

```

And code is (read comments):

```
#include<stdio.h>
#include<stdlib.h>
int main(){
    FILE* fp = fopen("file","r");
    char first[5];
    int second;
    while(!feof(fp)){          //  While EOF loop runs   
     fgets(first, 5, fp);      //  read first 4 chars in a line
     fscanf(fp,"%d",&second);  //  Read next 4 digits in 
     fgetc(fp);                //  read \n
     printf("%d\n",second);
    }
    return EXIT_SUCCESS;
}

```

it runs like:

```
:~$ ./a.out 
5678
5432
8324
3223

```