Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

C language student dormitory management system

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

After the last plane game production, I feel that C language programming is more interesting, and it also makes me have a deeper understanding and understanding of this course. This time we learned how to learn C language operation files to permanently save data and how to write large-scale structured programs based on functions. This study is also a little more complicated than last time.

What we need to do this time is a management system for books. It can make it more convenient for us to use the program to find the location of books and some basic information.

I. the operation of C language files

1. Basic methods of file operation:

The C language regards the input and output devices of the computer as files. For example, keyboard files, screen files, etc.

You should remember to output a message to the screen, such as the classic "HelloWorld!" Yes

# include.h >

Int main ()

{

Printf ("HelloWorld!\\ n")

}

Receives a string from the keyboard and displays yes

# include.h >

Int main ()

{

Char a [10]

Scanf (&% s')

Printf ("% s\\ n", a)

}

So what is the way to write a string to a file? The default output file is displayed on the screen. If it is a file on the hard disk, you must first open a file before you can write it. Then you should tell the program where the file is and how to open it (read, write, read and write, add, overwrite, etc.), and then give the open file a symbol (pointer variable). Indicates that subsequent reads and writes are for the file, not to the screen, and the pointer variable will later represent the file itself.

Define a form such as a symbol (pointer variable) that represents a file

FILE * fp

Among them, FILE is written in a fixed way, followed by the pointer variable name, which can be started at will.

At this time, fp is still an empty pointer variable, nothing represents, like the label of a shelf in a warehouse, nothing is written.

Then let the pointer variable point to a file, that is, open a file, and then let the pointer variable point to the open file, and the subsequent operations on the pointer variable are all operations on the file.

The statement to open the file is

Fopen (file location, open mode)

The location of the file is easy to understand, that is, the location of the file, such as c://test.txt

There are several open modes

R (read): read

W (write): write

A (append): append

T (text): text file, which can be omitted or not written

B (banary): binary file

+: read and write

Generally speaking, rt+, can also be written as ringing and writing, that is, reading and writing, which can retain the original content.

So the statement to open the file c:\\ my\\ test.txt in the my directory on disk C is

Fp=fopen ("c://my/test.txt", "r +")

Notice that the folder symbol is different from the one above.

At this time, if there is something wrong with the open file, it is dangerous to write something into an empty pointer, and it is easy to crash the system. Therefore, before the subsequent read and write operations, it is best to test whether the file pointer fp is still empty after opening the file. If it is empty, it cannot be executed further. So the statement looks like this.

If ((fp=fopen ("c://my/test.txt", "r +")) = = NULL)

{

Printf ("the file is not opened correctly and cannot be executed further.\ n");

Exit (1)

}

The whole program looks like this.

# include.h >

Int main ()

{

FILE * fp

If ((fp=fopen ("c://my/test.txt", "r +")) = = NULL)

{

Printf ("the file is not opened correctly and cannot be executed further.\ n")

Return (1)

}

}

You will see that the program reported an error, because there is no my directory, so you have to create a my directory under disk C, and then create a test.txt file. Or if you open it and choose to write and read, if there is no directory or file, the program will automatically create a file for you. okay

If ((fp=fopen ("c://my/test.txt", "w +")) = = NULL)

Have time you can Baidu C language to open the type of files, the function is very rich.

Then you can use the previous input and output statements to write to the file, which is the same as what we learned last semester, that is, add f before printf and scanf to become fprint and fscanf, indicating output to and input from the file, not to the screen and input from the keyboard.

The following program inputs a character from the keyboard and writes it to a file.

# include.h >

Int main ()

{

Char a [20]

Char b [20]

Printf ("Please enter a string (less than 20 characters) and write it to c://my/test.txt\\ n")

Scanf (&% s')

FILE * fp

If ((fp=fopen ("c://my/test.txt", "w +")) = = NULL)

{

Printf ("the file is not opened correctly and cannot be executed further.\ n")

Return (1)

}

Fprintf (fp, "% s", a)

Fclose (fp)

}

After the execution, you open the file to see if the characters you have written have been written in.

Then, you write a string in the file, such as your name, in notepad and read it with fscanf instead of typing it on the screen.

# include.h >

Int main ()

{

Char a [20]

FILE * fp

If ((fp=fopen ("c://my/test.txt", "r +")) = = NULL)

{

Printf ("the file is not opened correctly and cannot be executed further.\ n")

Return (1)

}

Fscanf (fp,% s', & a)

Printf ("% s\\ n", a)

Fclose (fp)

}

When reading a file, there is a pointer to ensure that it is read down in order. If you want to start all over again after reading the beginning, like reading a page after reading, you can use the function rewind (fp). If you want to locate a certain position, similar to turning the book to a certain page, with fseek (offset, benchmark), it is in the benchmark (0 for the head of the file, 1 for the current position). 2 represents how much cheaper it is based on the end of the file.

This is the basic read and write statement of a file. Only when you have the reading and writing of the file can you develop the management system, because a lot of your information has to be stored in the file, otherwise in the program, there will be no data after shutdown.

2. Application of function and structured programming.

If there is no class, all the students in our school are in one class, and we can imagine how chaotic the management will be. We thought of a way to divide the students into unused classes according to their majors, so that it would be much more convenient to manage, send notices in the department, etc., just send them to the monitor, convey them down by the monitor, and change the contents of the activities within the class. it has nothing to do with other classes, just modify it behind closed doors. So it's the same with the program. If you put all the codes together, it's like all the students in the school are in the same class, they are related to each other, the program code is short, there is no problem, and the amount of program code is more. It will be very difficult to manage. So according to the function, put the different codes together, enclose them in curly braces, and then give the code a name similar to the name of our class, and then set the parameters to pass the actual value to it when you use this function, similar to an addition function, to complete the addition of two numbers, we can write

Myadd (int a, int b)

{

Int c

C=a+b

Return c

}

Myadd is the name we gave to this function, so that we can use the functions of an and b to receive the number passed by the caller, and then add the two received numbers and return them. The value of this function is the returned value c after the call, so if we want to calculate how much 99 percent 23 equals, just call the function we wrote.

Myadd (999923). At this time, the value of the function is 999923. If the result is saved in the variable d, it can be written as

D=myadd (99pr 23)

With functions, we can organize the code of different functions together, not only convenient for ourselves, but also the program is very simple and easy to modify, and we can also use the functions written by others directly.

So our student information management system is very simple, with a total of three functions.

Int main ()

{

Int i,sum

Pagedis ()

Check ()

Menu ()

}

The first is to display the welcome page, the second is to verify the user name and password, and the third is to display the menu and perform the appropriate action according to the input of the user.

There are other functions, which are subfunctions called by the menu () menu function.

This is the approximate form of the structural speech program.

# include.h >

Void pagedis ()

Void check ()

Void menu ()

Int main ()

{

Pagedis ()

Check ()

Menu ()

}

Void pagedis () {

Printf ("I am the module that displays the welcome interface, and the specific content needs to be further improved!\ n")

}

Void check () {

Printf ("I am the module for verifying the validity of users, and the specific content needs to be further improved!\ n")

}

Void menu () {

Printf ("I am the module that guides the user to perform various functions, and the specific content needs to be further improved!\ n")

}

This is structured programming, put the function into the function, the next step can focus on improving the content of the function.

source code

# include "stdio.h"

# include "stdlib.h"

# include "string.h"

# include "conio.h"

# define PAGE 3

# define MAX 1000

# define N 5

Int kryp0

/ * structure type * /

Typedef struct

{int num;/* student number * /

Char name [20]; / * name * /

Char sex [5]; / * gender * /

Int age;/* Age * /

Char studentclass [20]; / * Class * /

Int dorm;/* dormitory number * /

} STUDENTS

Int read_file (STUDENTS stu [])

{FILE * fp

Int iTunes 0

If ((fp=fopen ("stu.txt", "rt")) = = NULL)

{printf ("\ n\ n* inventory file does not exist! please create")

Return 0

}

While (feof (fp)! = 1)

{

Fread (& stub [I], sizeof (STUDENTS), 1meme FP)

If (StuI] .num = = 0)

Break

Else

ITunes +

}

Fclose (fp)

Return i

}

Void save_file (STUDENTS stu [], int sum)

{FILE*fp

Int i

If ((fp=fopen ("stu.txt", "wb")) = = NULL)

{printf ("file write error!\ n")

Return

}

For (iSuppli)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 225

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report