| |

VerySource

 Forgot password?
 Register
Search
View: 791|Reply: 3

File manipulation problem! Write a time-keeping program, how about it?

[Copy link]

1

Threads

13

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-3-23 14:00:01
| Show all posts |Read mode
#include <stdio.h>
#include <time.h>
#include <windows.h>

struct timer
{
 int hour;
 int minite;
 int second;
} t;

int main ()
{
 FILE * fp;
 if (NULL == (fopen ("f:\\timeRed.txt", "r")))
 {
  printf ("Get the file Failed!");
  exit (1);
 }
 fread (&t, sizeof (struct timer), 1, fp);
 while (1)
 {
  rewind (fp);
  Sleep (1000);
  fread (&t, sizeof (struct timer), 1, fp);
  if (t.second == 59)
  {
   t.minite = t.minite + 1;
   if (t.minite == 60)
   {
    t.hour = t.hour + 1;
   }
   t.second = 0;
  }
  else
t.second ++;
  printf ("% d% d% d", t.hour, t.minite, t.second);
  fwrite (&t, sizeof (struct timer), 1, fp);
  fclose (fp);
 }
}

When running under Vc, there is a problem of ms memory error ~~~ expert advice!
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-7-2 21:30:01
| Show all posts
Is there correct format in advance in the operation file?

First define a structure variable t,
Use fwrite(&t, sizeof(struct timer), 1, fp);
Generate file content...
Reply

Use magic Report

1

Threads

13

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

 Author| Post time: 2020-7-3 06:45:02
| Show all posts
The operation file was created in advance, but it is empty.

That does not work?

t is defined...

What's going on? Ha ha
Reply

Use magic Report

0

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-7-3 18:45:01
| Show all posts
Your file pointer: FILE *fp; There is no initialization at all, how can it be used in fwrite()?
FILE *fp;
if(NULL == (fopen("f:\\timeRed.txt","r")))

To:

FILE *fp = fopen("f:\\timeRed.txt","r")
if(NULL == fp)
{
...
}

Try again!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list