| |

VerySource

 Forgot password?
 Register
Search
View: 833|Reply: 8

Puzzling question

[Copy link]

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-25 15:40:01
| Show all posts |Read mode
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "time.h"

#define MAX_PKT 1024

#define MAX_SEQ 7

typedef enum {FALSE, TRUE} boolean;

typedef unsigned int seq_nr;

typedef enum {dat, ack, nak} frame_kind;

typedef enum event_type {timeout, frame_arrival};

typedef struct
{frame_kind kind;
seq_nr seq;
seq_nr ack;
char info [1024];
} frame;

void from_network_layer (frame * fra)
{
printf ("Input information:");
memset (fra-> info, 0, strlen (fra-> info));
for (int i = 0;; i ++)
{
fra-> info [i] = getchar ();
if ('\n' == fra-> info [i])
{
fra-> info [++ i] = '\0';
break;
}
}
}

void to_physical_layer (frame * fra)
{
FILE * fp1;
char buf [1024];
char temp;
memset (buf, 0, strlen (buf));
switch (fra-> kind)
{
case dat:
strcpy (buf, "dat");
break;
case ack:
strcpy (buf, "ack");
break;
case nak:
strcpy (buf, "nak");
break;
}
itoa (fra-> ack,&temp, 10);
strcat (buf,&temp);

itoa (fra-> seq,&temp, 10);
strcat (buf,&temp);

strcat (buf, fra-> info);
// strcat (buf, '/ 0');
if (0 == (fp1 = fopen ("d:\\a.txt", "r")))
{
printf ("File creation failed");
fclose (fp1);
}
else
{
fwrite (buf, 1, 1024, fp1);
fclose (fp1);
}
printf ("success");
}

event_type wait_for_event ()
{
      return frame_arrival;
}
void main ()
{
seq_nr ack = 0;
frame send, rev;
event_type event;
from_network_layer (&send);
send.kind = dat;
send.ack = 0;
send.seq = 0;
to_physical_layer (&send);
         event = wait_for_event ();
}
As soon as you debug to WAIT_FOR_EVENT () RETURN, UNHANDLED EXCEPTION is displayed.If TO_PHYSICAL_LAY () is shielded, you can return normally. The master will give pointers, thank you first!
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-2-20 22:15:01
| Show all posts
memset (buf, 0, sizeof (buf));
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-2-22 16:30:01
| Show all posts
char temp [16];
.....
itoa (fra-> ack, temp, 10);
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-2-22 18:00:01
| Show all posts
printf ("File creation failed");
// fclose (fp1); This close is removed

And all of the above itoas
Reply

Use magic Report

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-3-9 16:15:01
| Show all posts
Thank you, how do you discover these problems?
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-3-11 09:45:01
| Show all posts
printf the contents of buf
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-3-11 10:45:01
| Show all posts
And fopen "wb"
Reply

Use magic Report

0

Threads

22

Posts

18.00

Credits

Newbie

Rank: 1

Credits
18.00

 China

Post time: 2020-3-11 17:15:01
| Show all posts
memset has not seen strlen, and still uses strlen for initialization, otherwise use strcpy ()
fp1 file creation failed, fclose (fp1) will definitely not be used;

As for itoa (fra-> ack,&temp, 10); because it is a statically allocated char [16],&temp and temp are the same and point to the first address of the allocated space.
If it is malloc dynamic allocation,&temp points to the variable that stores the address value, you must use temp
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-12 19:30:02
| Show all posts
char temp [16];
.....
itoa (fra-> ack, temp, 10);
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