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

Design and write an application system

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

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.

Output a message to the screen, for example, "Hello" is

# include.h >

Int main ()

{

Printf ("Hello\\ 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. Structures and structural variables that can store multiple information

I learned last semester that a variable can hold a piece of data, such as

Int a; double b; char c [20]

Then a can save an integer, b can save a real number, and c can save a string of characters (strings).

However, if a system, such as student information management system, needs to save a student's name, gender, age and other information at the same time, then it is a bit inconvenient to set variables to save the information of many students. Similar to a registration book, only names can be written on each page, if you want to register other information, add a registration book, write only gender, if you register age, add another registration book, only write age, … How inconvenient and easy to get confused. The solution you must think, how simple it is, isn't it enough to write your name, sex and age on one page of a registration book? The same is true of C language. If you define a form and a variable records a lot of information at the same time, it will be much more convenient in the management system. This is the structure.

The structure should be defined first, because the information used in each program is different, for example, in the student information management system, the student number, name, gender, age and class may be combined together, so the first line tells the program with struct, the following is a structure, and the following stu is the type of the structure, similar to the integer expressed by int, this stu is your own name.

Typedef stu

{int num;/* student number * /

Char name [20]; / * name * /

Char sex [5]; / * gender * /

Int age;/* Age * /

Char sclass [20]; / * Class * /

}

There are five variables in this structure.

This gives you a new variable type, stu, which you define yourself, the same as int,float.

If you define an integer variable a, you must know that it is

Int a

Axi3

The variable a that defines a stu type that you define yourself, in which the previous struct tells the compiler that the stu is your own defined structure type, and the following an is a structure variable.

Struct stu a

But when assigning a value, because an at this time represents a lot of information, there is num,name,sex,class, that is, an actually includes a lot of variables, so how to assign a value? Use the dot operator ".", which is a. NumMaga.name. A. Sextile. Class. This is similar to the fact that each page of the notebook just mentioned records a lot of information. This a represents a page of paper with a lot of information columns. The student number column of this page is indicated by a.num, and other similar. What if there are many students? Then set up a few more variables of type stu, such as a1Percenta2mema3. Just put a lot of students' information, you must think, this is not a good way, because the information is the same, that is, the format of each page of the registration book is the same, this way is suitable for using arrays

Stu a [80]

In this way, use a [0], a [1], a [2], respectively. You can store the information of up to 80 students.

So the program can be like this.

# include.h >

Struct stu

{int num; / * student number * /

Char name [20]; / * name * /

Char sex [5]; / * gender * /

Int age;/* Age * /

Char sclass [20]; / * Class * /

}

Int main ()

{

Struct stu a

Printf ("enter the student number, name, gender, age, class, enter and the system will display the input\\ n").

Scanf ("% d%s%s%d%s", & a. NumMagol. Name. Name. Gender.

Printf ("the following is the result after the information just entered is saved in the stu type variable a")

Printf ("% d%s%s%d%s", & a. NumMagol. Name. Name. Gender.

}

Now that you have made a lot of progress, you can use a variable to store a lot of information.

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: 0

*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