This is written for the push box game, some time learning C language, recently turned to Windows design, do not know whether there is the same feeling, the back of the things learned, the front is a little rusty. In fact, my understanding is that attention shifts, and when you focus on the current problem, you ignore some of the previous knowledge. This also shows that people's attention is changing with time and the difference in learning content. However, just a little refresher can quickly recover.
It is more intuitive to put forward a question before discussing it. For example, the content of a text file is as follows:
[1]
0000000000000000
0000000000000000
0000000000000000
0000011100000000
0000013100000000
0000012111100000
0001114243100000
0001324611100000
0001111410000000
0000001310000000
0000001110000000
0000000000000000
0000000000000000
0000000000000000
[2]
0000000000000000
0000000000000000
0001111100000000
0001622100000000
0001244101110000
0001242101310000
.................. (censored)
[] Inside is the ordinal , extends downward, each [] below is 14 row 16 column of data, I want to obtain the given ordinal number below this two-dimensional array data, for example, the ordinal [10] below the 14*16 column the data.
————————————————————————————————————————————————————————————
Before this problem, first understand the concept of a nasty, simple record:
There are many kinds of flows, including input streams, output streams, and so on, here only the input stream, such as stdin, when calling the scanf function to read this stream, there are a few points to note:
First, scanf ignores the preceding spaces, enter and so on characters
Second, at the time of reading, there is a pointer in the stream that is moving continuously, that is, the cursor. Make a diagram to illustrate:
Want to enter a data abcd into a string, at the beginning, deliberately typed space + ENTER, which has no effect on scanf, it will ignore, always read the ABCD, and encountered a carriage return, the cursor finally stay on the character of carriage return.
Did you get the return key by scanf? In fact, there is no, such as the next can be used C=getchar (), you can capture the carriage return character.
In the same vein, replace the stdin with the file FP and follow this rule. because it is not a matter of special consolidation flow, it is simply summed up here.
————————————————————————————————————————————————————————
The solution to the above problem is to read a string from the FP and compare it to the ordinal number to be matched, for example, compared to the string "[10]", if it is equal, the cursor is positioned after [10].
Observe the above text file, each line is followed by a carriage return , so each time from the FP read a string, either Read [XX], or read is a two-dimensional array of data, of course, the string storage length is at least a row of data, at least 16.
Specific
First gets the ordinal number to take, defines a string, writes it to it.
Char str[];sprintf (str,"[%d]", N);
Next, the FP is read by string, compared with it.
FILE *fp=fopen ("Map.txt","RB");//Open a text file if(fp==NULL)return; Chardata[ -]; FSCANF (FP,"%s", data);//first read into a string while(STRNCMP (Str,data,strlen (str))! =0)//equal return 0 { if(FP, fscanf"%s", data) ==null)//Loop read, cursor is also moving Break; }
If read to [10], conforms to test instructions, then the cursor is stuck in the back of [10], the following is the [10] ordinal of the 14*16 data. This place has another use , for example, a total of 40, if the player clearance, and then scan the file will certainly not find [41] serial number, you can determine the player clearance.
Because to read 14*16, the equivalent of a two-dimensional array is handled as follows:
for(inty=0;y< -; y++)//Line{fscanf (FP),"%s", data);//read a row for(intx=0;x< -; x + +)//column{map[y][x]=data[x]-'0';//character integer, fill map[][],map[][] is a two-dimensional array of integers, so you need to convert ...... } }
In this way, the problem is solved.
The complete sample code
voidLoadmap (intN//load map, mainly responsible for filling map[][] of the two-dimensional array, map[][] is the key to manipulate the drawing{ Charstr[Ten]; sprintf (str,"[%d]", N); FILE*fp=fopen ("Map.txt","RB");//Open the file, read the map data, the so-called map, is to use numbers to manipulate the bitmap block if(fp==NULL)return; Chardata[ -]; FSCANF (FP,"%s", data);//first read into a string while(STRNCMP (Str,data,strlen (str))! =0)//equal return 0 { if(FP, fscanf"%s", data) ==null)//Loop read, cursor is also moving Break; } for(inty=0;y< -; y++)//Line{fscanf (FP),"%s", data);//Read 14 rows for(intx=0;x< -; x + +)//column{map[y][x]=data[x]-'0';//char Integer, fill map[][] if(map[y][x]==manatroad| | map[y][x]==manatdestination) {manposition.x=x;//person's positionmanposition.y=y; }}} fclose (FP);}
View Code
This program, each time to scan the entire file, get the same serial number, because the file is relatively small, the impact is not too large.
——————————————————————————————————————————————————
Summary: The search process for a text file.
scanf and fscanf reading files