Home > Software engineering >  The stream!=NULL, ask the teacher for help.
The stream!=NULL, ask the teacher for help.

Time:10-04

The code is as follows:
# include & lt; stdio.h>
# define MAX_DOUBLE 1.79769 e+308
3 # define E

Struct point
{
Int x, y;
};

Void compress ()
{
The FILE * fin=fopen (" rand. TXT ", "r");
The FILE * fout=fopen (" out. TXT ", "w");

//the door and door, initial door was closed when
Double up_gate=- MAX_DOUBLE;
Double down_gate=MAX_DOUBLE;

//the current data on the slope and lower slope
Double now_up now_down;

The int data;//the current data read
Int last_read_data;//the current data of the previous data
Int last_stored_data;//save point recently

//read and save the first data
The fscanf (fin, "% d", & amp; Last_stored_data);
Fprintf (fout, "d" 0%, last_stored_data);

Last_read_data=https://bbs.csdn.net/topics/last_stored_data;

Int last_stored_t=0;//save data time recently, init 0

//loop processing data
Int t=0;
While (+ + t, fscanf (fin, "% d", & amp; Data)!=(EOF)
{
Now_up=double (data - last_stored_data - E)/(t - last_stored_t);
If (now_up & gt; Up_gate)
Up_gate=now_up;

Now_down=double (data - last_stored_data + E)/(t - last_stored_t);
If (now_down & lt; Down_gate)
Down_gate=now_down;

If (up_gate & gt;=down_gate)
{
//save the previous point
Fprintf (fout, "% d % d", t - 1, last_read_data);

Last_stored_t=t - 1;//modify recently saved data point
Last_stored_data=https://bbs.csdn.net/topics/last_read_data;

//initialize the two doors to the current point and slope of a point on the
Up_gate=double (data - last_stored_data - E);
Down_gate=double (data - last_stored_data + E);
}
Last_read_data=https://bbs.csdn.net/topics/data;
}
//sava end point
Fprintf (fout, "% d % d", t - 1, last_read_data);

The fclose (fin);
The fclose (fout);
}

Void uncompress ()
{
The FILE * fin=fopen (" out. TXT ", "r");
The FILE * fout=fopen (" SRC. TXT ", "w");

Point a, b;
The fscanf (fin, "% d % d", & amp; A.x, & amp; A.y);

While (fscanf (fin, "% d % d", & amp; B.x, & amp; B.y)!=(EOF)
{
1
//Step.Fprintf (fout, % d ", "a.y);

//Step 2
If (a.x + 1!=b.x)
{
Double k=double (b.y - a.y)/(b.x - a.x);//calculate the slope
For (int I=a.x + 1; i{
//linear interpolation for data to be compressed
Fprintf (fout,. % 0 lf, k * (I - a.x) + a.y);
}
}

A.x=b.x;
A.y=b.y;
}
Fprintf (fout, "% d", b.y);
The fclose (fin);
The fclose (fout);
}

Int main (void)
{
Compress ();
Uncompress ();
return 0;
}
}

CodePudding user response:

why program error at the end of the input data stream.=NULL, by a single step debugging have found an error, but don't know why, don't know, how to solve, the local error is marked out, solve, thank you!

CodePudding user response:

Fopen don't check the return value, IO error opportunity is really too much.

CodePudding user response:

Beginner, don't know how to change, a great god give directions, thank you

CodePudding user response:

While (+ + t, fscanf (fin, "% d", & amp; Data)==1

The Return Value

Each of these functions provides returns the number of fields successfully converted and assigned; The return value does not include fields that were read but not assigned. A return value of 0 are that no fields were assigned. If an error occurs, or If the end of the file stream is reached before the first conversion, the return value is EOF for fscanf or WEOF for fwscanf.

CodePudding user response:

While (+ + t, fscanf (fin, "% d", & amp; Data)==1

The Return Value

Each of these functions provides returns the number of fields successfully converted and assigned; The return value does not include fields that were read but not assigned. A return value of 0 are that no fields were assigned. If an error occurs, or If the end of the file stream is reached before the first conversion, the return value is EOF for fscanf or WEOF for fwscanf.

CodePudding user response:

The building Lord solved?
  • Related