【Linux】期末复习||scau实验整理||实验期末版精简版+解释

5 篇文章 0 订阅

step by step.

!声明:本实验整理 不完整且不完全按实验册题目,目的为期末考,带思考,不保证实验作业。

原创。 


目录

step by step.

实验一

查看虚拟机IP信息

重命名文件

 转换为unix格式

vi打开文件,在第一行加入内容 

 重定向追加内容

内容追加

搜索文件+追加

当前时间按照 -年-月-日 时:分:秒 的格式追加到work1.txt中

压缩生成文件

通过ls命令长格式查看log.tar.gz文件,追加到work1.txt中

文件转换为windows格式

实验二

查看目录是否存在子目录,如果没有则新建

创建文件夹、文件

创建用户并指定uid(1024)

查看文件信息并追加

指定行数内容追加

 追加myshare所有文件、子目录内容(长格式)至work2

修改文件拥有者并查看、追加

小插曲(移动目录,修改拥有者,修改用户,进入目录)

进入用户

进入目录新建、修改程序

设置文件可读可写可执行权限并追加

执行脚本文件并追加

进入exm用户主目录

 创建软链接

拷贝文件且改名

长格式查看所有(含隐藏)文件并追加

查找/开始的后缀名为.conf的所有(普通)文件并追加

寻找上一步中容量最大的文件并追加

统计用户

转换格式

删除用户下目录

删除目录

实验三

安装telnet 

 安装ftp服务器(standalone)

 创建a2022账号并设置好密码

sudoers中添加权限

查看telnet连接情况

查看vsftpd配置文件信息

windows远程telnetlinux

关闭linux防火墙

 追加内容、修改权限​编辑

追加文件1、3、4段内容至tmp.txt

ftp禁止匿名登录 锁定登录用户目录不可进行目录切换

实验三(课上)vi编辑器操作

生成out.txt

 vi编辑out.txt 显示行号

 vi编辑器光标移动

 vi编辑器搜索

vi编辑器修改

恢复修改

复制65到73之间内容粘贴到最后一行

删除21到42行内容

另存

插入内容

实验五

增加执行权限

执行sh文件 

实验六

1. 根据输入循环执行输出

2. 统计每个ip出现的次数(通过run.log行数)

3.

实验七


实验一

查看虚拟机IP信息

[root@localhost ~]# ip addr

显然ip地址为:192.168.178.130

重命名文件

[root@localhost ~]# vim exam1.txt --新建一个txt随便写入一点东西(Linuxexam1!) 

[root@localhost ~]# mv exam1.txt /tmp/exam1.txt --移动并重命名

 (图片不同,操作不同)

 转换为unix格式

[root@localhost ~]# dos2unix tmp/work1.txt

vi打开文件,在第一行加入内容 

 O

 添加“content...”

 重定向追加内容

[root@localhost ~]# echo 12345 >> tmp/work1.txt --重定向内容至指定文件

查看追加情况:

内容追加

/etc/passwd的最后5行追加到work1.txt

[root@localhost ~]# tail -5 /etc/passwd >> tmp/work1.txt 

搜索文件+追加

搜索 / 下所有 .xml 结尾的普通文件(-type f)

[root@localhost ~]# find / -name "*.xml" -type f

搜索路径中含usr的文件

[root@localhost ~]# find / -name "*.xml" -type f | grep usr  --搜索并增加管道(usr)

搜索路径中含usr的文件追加到work1.txt中

[root@localhost ~]# find / -name "*.xml" -type f | grep usr >> tmp/work1.txt --追加到work1.txt中

当前时间按照 -年-月-日 时:分:秒 的格式追加到work1.txt中

[root@localhost ~]# date +'%Y-%m-%d %H:%M:%S'   --查看当前时间(特殊格式)
2022-05-14 08:30:20
[root@localhost ~]# date +'%Y-%m-%d %H:%M:%S' >> tmp/work1.txt  --重定向追加内容

压缩生成文件

 [root@localhost ~]# tar zcvf log.tar.gz /var/log

通过ls命令长格式查看log.tar.gz文件,追加到work1.txt中

通过式ls命令长格查看log.tar.gz文件

 [root@localhost ~]# ls -l log.tar.gz

追加到work1.txt中

[root@localhost ~]# ls -l log.tar.gz | awk '{print $1}' >> tmp/work1.txt 

文件转换为windows格式

[root@localhost ~]# unix2dos tmp/work1.txt


实验二

查看目录是否存在子目录,如果没有则新建

[root@localhost ~]# ls -l tmp/myshare  --查看是否存在(-l长格式打印)
ls: cannot access tmp/myshare: No such file or directory  
[root@localhost ~]# mkdir -p tmp/myshare   --新建目录(-p自动创建不存在子目录)
[root@localhost ~]# ls -l tmp/myshare 
total 0

创建文件夹、文件

[root@localhost ~]# mkdir -p tmp/myshare/2022 --创建文件夹
[root@localhost ~]# touch tmp/myshare/work2.txt --touch改变文件时间记录、创建空文件

创建用户并指定uid(1024)

[root@localhost ~]# useradd -u 1024 test

查看文件信息并追加

查看文件信息 

[root@localhost ~]# cat /etc/passwd /etc/shadow

追加信息 

[root@localhost ~]# cat /etc/passwd /etc/shadow | grep test  --增加管道

[root@localhost ~]# cat /etc/passwd /etc/shadow | grep test >> tmp/myshare/work2.txt --增加管道并重定向

指定行数内容追加

[root@localhost ~]# head -13 /etc/passwd >> tmp/myshare/work2.txt --追加前13行至work2


 

 追加myshare所有文件、子目录内容(长格式)至work2

[root@localhost ~]# ls -lR tmp/myshare/ >> tmp/myshare/work2.txt

修改文件拥有者并查看、追加

修改文件拥有者并查看修改结果

[root@localhost ~]# chown -R test:mail tmp/myshare --‘修改后拥有者:组名(test:mail)’

 追加并查看

[root@localhost ~]# ls -lR tmp/myshare/ >> tmp/myshare/work2.txt
[root@localhost ~]# tail tmp/myshare/work2.txt 

小插曲(移动目录,修改拥有者,修改用户,进入目录)

[root@localhost ~]# mv tmp/myshare/ /tmp/myshare --修改为/tmp下文件(权限问题)
[root@localhost ~]# chown -R exm:mail /tmp/myshare --修改拥有者:组名
[root@localhost ~]# su exm --进入用户
[exm@localhost root]$ cd /tmp/myshare/2022 --进入目录

 [exm@localhost 2022]$

进入用户

 [root@localhost ~]# su exm --进入用户exm

进入目录新建、修改程序

 [exm@localhost 2022]$ ls
 [exm@localhost 2022]$ vi hello.sh

设置文件可读可写可执行权限并追加

设置文件权限 

[exm@localhost 2022]$ chmod u=rwx,g=rx,o=x hello.sh --拥有者可读可写可执行(u=rwx),同组可读可执行(g=rx),其他人可执行(o=x)

[root@localhost ~]# ls -l test.sh
-rw-r--r--. 1 root root 124 May 17 18:53 test.sh
[root@localhost ~]# chmod a=rwx test.sh   --'a='设置全部权限都为rwx
[root@localhost ~]# ls -l test.sh
-rwxrwxrwx. 1 root root 124 May 17 18:53 test.sh 

 追加:

[exm@localhost 2022]$ ls -l hello.sh >> /tmp/myshare/work2.txt

执行脚本文件并追加

[exm@localhost 2022]$ ./hello.sh --执行
app start

hello Linux!

app end
[exm@localhost 2022]$ ./hello.sh >> /tmp/myshare/work2.txt

进入exm用户主目录

[exm@localhost 2022]$ cd ~     --~为用户主目录

 创建软链接

[exm@localhost ~]$ ln -s /tmp/myshare/2022/hello.sh myhello.sh  --创建软链接

拷贝文件且改名

[exm@localhost ~]$ cp /tmp/myshare/2022/hello.sh hello.sh.bak  -拷贝至当前目录并改名为hello.sh.bak
[exm@localhost ~]$ ls -l  --长格式查看

长格式查看所有(含隐藏)文件并追加

[root@localhost ~]# ls -al /home/exm

[root@localhost ~]# ls -al /home/exm >> /tmp/myshare/work2.txt

查找/开始的后缀名为.conf的所有(普通)文件并追加

[root@localhost ~]# find / -name "*.conf" -type f

寻找上一步中容量最大的文件并追加

先按容量排序

[root@localhost ~]# ls -lS `find / -name "*.conf" -type f` --单引号`括起上一部命令,再进行排序

 再选取第一个(即容量最大的)

[root@localhost ~]# ls -lS `find / -name "*.conf" -type f` | head -1  --选取head第一个
-rw-r--r--. 1 root root 153591 Oct 19  2020 /usr/src/kernels/3.10.0-1160.el7.x86_64/include/config/auto.conf

 追加

[root@localhost ~]# ls -lS `find / -name "*.conf" -type f` | head -1 >> /tmp/myshare/work2.txt

统计用户

[root@localhost ~]# cat /etc/passwd | wc -l  --统计行数即用户数
47 --输出47即47个用户

转换格式

[root@localhost ~]# dos2unix /tmp/myshare/work2.txt

删除用户下目录

 [root@localhost ~]# userdel -r exm --删除用户exm下所有目录文件

删除目录

[root@localhost ~]# rm -rf /tmp/myshare/  --删除myshare目录
[root@localhost ~]# ls -l /tmp/myshare  --查看,果然没啦
ls: cannot access /tmp/myshare: No such file or directory


实验三

安装telnet 

[root@localhost ~]# cd /etc/xinetd.d/
[root@localhost xinetd.d]# ls
chargen-dgram   daytime-stream  echo-dgram     telnet       vsftpd
chargen-stream  discard-dgram   echo-stream    time-dgram
daytime-dgram   discard-stream  tcpmux-server  time-stream

[root@localhost xinetd.d]# vi telnet

[root@localhost xinetd.d]# service xinetd restart
Redirecting to /bin/systemctl restart xinetd.service
[root@localhost xinetd.d]# cd ~

 安装ftp服务器(standalone)

[root@localhost ~]# yum install -y vsftpd ftp

[root@localhost ~]# service vsftpd restart

 创建a2022账号并设置好密码

[root@localhost ~]# useradd a2022
[root@localhost ~]# passwd a2022

sudoers中添加权限

[root@localhost ~]# ls -l /etc/sudoers  --查看sudoers中的用户
-rw-r-----. 1 root root 4364 Apr 17 19:39 /etc/sudoers
[root@localhost ~]# chmod +w /etc/sudoers --添加w权限(请记住是+不是-)

[root@localhost ~]# vi /etc/sudoers
打开后添加信息

快捷查看 /root

 添加用户信息:

进入用户并查看

[root@localhost ~]# su a2022
[a2022@localhost root]$ sudo cat /etc/shadow

成功!

查看telnet连接情况

[root@localhost ~]# chkconfig --list |grep telnet
[root@localhost ~]# chkconfig --list |grep telnet >> /root/work3.txt

查看vsftpd配置文件信息

[root@localhost ~]# ps -ef | grep vsftpd

windows远程telnetlinux

打开windows的cmd(要通过‘控制面板’-程序与功能打开telnet、ftp)

出错原因:linux防火墙未关闭。 

关闭linux防火墙

[root@localhost ~]# service firewalld stop

成功! 

 追加内容、修改权限

 [a2022@localhost ~]$ sudo cp /root/work3.txt ~/tmp.txt --复制文件
[a2022@localhost ~]$ ls -l tmp.txt
-rw-r--r--. 1 root root 20 May 15 03:08 tmp.txt
[a2022@localhost ~]$ sudo chown a2022:a2022 tmp.txt --修改权限
[a2022@localhost ~]$ ls -l tmp.txt
-rw-r--r--. 1 a2022 a2022 20 May 15 03:08 tmp.txt
[a2022@localhost ~]$ pwd >> tmp.txt
[a2022@localhost ~]$ date >> tmp.txt

[root@localhost ~]# ls -l test.sh
-rw-r--r--. 1 root root 124 May 17 18:53 test.sh
[root@localhost ~]# chmod a=rwx test.sh   --'a='设置全部权限都为rwx
[root@localhost ~]# ls -l test.sh
-rwxrwxrwx. 1 root root 124 May 17 18:53 test.sh

追加文件1、3、4栏内容至tmp.txt

[a2022@localhost ~]$ cat /etc/passwd | cut -d: -f1,3,4 >> tmp.txt

ftp禁止匿名登录 锁定登录用户目录不可进行目录切换

annoymous_enable=NO

 chroot_local_usere=YES 打开 (去掉#)

 allow_writeable_chroot=YES


实验三(课上)vi编辑器操作

生成out.txt

[root@localhost ~]# man ls > out.txt

 vi编辑out.txt 显示行号

:set nu

 vi编辑器光标移动

99G  --跳到第99行

10l    --右移10字符

gg     --移动到首行

 

 vi编辑器搜索

/1024   --搜索’1024‘

vi编辑器修改

:50,100 s/o/O/g --50至100行o修改成O 'g':全局

恢复修改

u

复制65到73之间内容粘贴到最后一行

65G

9yy --复制9行

G

p --粘贴

删除21到42行内容

:21,42 d

另存

:w out.txt.bak --另存为out.txt.bak 

插入内容

28G --定位28行

3dw --删除3个word

gg --定位到首行


实验五

#!/bin/bash
options_show(){
        echo "Use one of the following options:"
        echo "P:To display current directory"
        echo "S:To display the name of running file"
        echo "D:To display today's date and present time"
        echo "L:To see the list of files in your present working directory"
        echo "W:To see who is logged in"
        echo "I:To see the ip address of this local machine"
        echo "Q:To quit this program"
        echo "Enter your option and hit:"
}

while true
do
        options_show
        read input
        case ${input} in
        p|P)
         'pwd'
         ;;
        s|S)
         echo $0
         ;;
        d|D)
         date +"%Y-%m-%d %H:%M:%S" 
         ;;
        l|L)
         ls
         ;;
        w|W)
         who
         ;;
        i|I)
         ifconfig
         ;;
        q|Q)
         exit
         ;;
         *)
         echo "usage error!"
         ;;
        esac
done
options_show
done

增加执行权限

[root@localhost ~]# chmod +x *.sh

执行sh文件 

[root@localhost ~]# ./menu.sh  --执行menu.sh
Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today's date and present time
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
p
/root


Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today's date and present time
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
s
./menu.sh

Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today's date and present time
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
d
2022-05-15 06:45:33


Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today's date and present time
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
l
1.sh          exam2.txt  hello.s          program.sh   say_hello.o    tmp
201925320219.tar  exam3.txt  log.sh          Public       score.sh    tmp.txt
201925320219.zip  file.sh    log.tar.gz       run1.sh       sum        tool1
4.sh          ftp.sh     menu.sh          run2.sh       sum.c    tool2
anaconda-ks.cfg   hello      Music          run3.sh       sum.i    var
Desktop          hello.c    original-ks.cfg  run4.sh       sum.o    Videos
Documents      hello.i    out.txt          say_hello.c  Templates    work3.txt
Downloads      hello.o    Pictures          say_hello.h  test
Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today's date and present time
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
w
root     :0           2022-05-15 06:24 (:0)
root     pts/0        2022-05-15 06:25 (:0)


Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today's date and present time
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
i
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.178.130  netmask 255.255.255.0  broadcast 192.168.178.255
        inet6 fe80::b507:8c64:3e97:76c  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:06:e0:ca  txqueuelen 1000  (Ethernet)
        RX packets 693  bytes 62281 (60.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 237  bytes 23378 (22.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 64  bytes 5568 (5.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 64  bytes 5568 (5.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:e5:b3:0e  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today's date and present time
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
q

#!/bin/bash
while true
do
        read score
        if[ $score -lt 0 ];then
         exit
        elif[ $score -lt 60 ];then
         echo "Failed"
        elif[ $score -lt 70 ];then
         echo "Passed"
        elif[ $score -lt 80 ];then
         echo "Medium"
        elif[ $score -lt 90 ];then
         echo "Good"
        elif[ $score -le 100];then
         echo"Excellent"
        else
         echo"input error!"
        fi
done

  1 #!/bin/bash
  2 while true
  3 do
  4         echo -e "input a directory:"
  5         read dir
  6         if [ $dir = "q" ];then
  7                 exit
  8         elif [ -e $dir -a -d $dir ];then
  9                 for file in `ls $dir`
 10                         do
 11                                 if [! -e "$dir$file" ];then
 12                                         dd="$dir/$file"
 13                                 else
 14                                         dd="$dir$file"
 15                                 fi
 16                   type=`ls -ld $dd | cut -c1`
 17                                 echo "${dd}_${type}"
 18                   done
 19         else
 20                 echo "$dir is not a directory"
 21         fi
 22         echo -e
 23 done

[root@localhost ~]# ./file.sh  --执行
input a directory:
/etc
./file.sh: line 11: [!: command not found
ls: cannot access /etcabrt: No such file or directory
/etcabrt_


input a directory:
/etc_d
/etc_d is not a directory


input a directory:
/etc/passwd_
/etc/passwd_ is not a directory

  1 #!/bin/bash
  2 yum install -y vsftpd
  3 setenforce 0
  4 sed -i 's?anoymous_enable=YES?anonymous_enable=NO?' /etc/vsftpd/vsftpd.conf
  5 service vsftpd start

 [root@localhost ~]# ./ftp.sh
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.ustc.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.ustc.edu.cn
Package vsftpd-3.0.2-29.el7_9.x86_64 already installed and latest version
Nothing to do
Redirecting to /bin/systemctl start vsftpd.service
Job for vsftpd.service failed because the control process exited with error code. See "systemctl status vsftpd.service" and "journalctl -xe" for details.


实验六

1. 根据输入循环执行输出

编写⼀段bash shell程序,保存为 program.sh 完成以下输出,可循环执行:

please,input a number:

5(回⻋)

5 4 3 2 1

4 3 2 1

3 2 1

2 1

1

!/bin/bash
echo "please, input a number:"
read num
while [ $num -gt 0 ]
do
        i=$num
        while [ $i -gt 0 ]
        do      
                echo -n "$i  "
                i=`expr $i - 1`
        done
        echo -e 
        let num=$num-1
done

注意一些空格的地方! 

2. 统计每个ip出现的次数(通过run.log行数)

 run.log是⼀个具有固定格式的⽇志⽂件。每⼀⾏的格式为: ip[]time[]runner[]command[]result ,其中[]表示空格,ip格式为点分⼗进制表示法。

编写⼀个名为log.sh的shell脚本统计每个ip出现的次数。 例⼦:

10.10.10.1[]32

10.10.10.2[]2

10.10.10.3[]5

#!/bin/bash
ips=`cat run.log | awk '{print $1}' | sort -u`
for ip in ${ips}
do
        count=`cat run.log | grep ${ip} | wc -l`
        echo -e ${ip} ${count}
done

3.

管理员root每天需要完成以下⼯作:

2.1 每天早上8点30分启动服务器的ftp服务,在每天晚上23点30分就关闭ftp服务 。如果启动成功把ftp的进程信息写⼊ /var/ftp/年-⽉-⽇.log ⽂件中,如果启动失败,需要给root发⼀封邮件。邮件内 容为: start ftp error

2.2 在早上8点30分到晚上23点30分之间,每隔1⼩时ping⼀下百度域名(每次ping 发4次),保证⽹络畅通,并把ping的结果追 加到 /var/ftp/年-⽉-⽇.log ⽂件中。

2.3 每天晚上11点50分30秒备份ftp⽬录(/var/ftp)⽣成名为 年-⽉-⽇.tar.gz 的 压缩包,并把压缩包的权限修改为只有root有 读权限,其他都没有权限。把压缩包移动到root主⽬录下。然后清空/var/ftp下的 所有内容。 假设/var/ftp⽬录已存在


实验七

【linux】scau期末复习||Linux C||实验_bdy_y9的博客-CSDN博客https://blog.csdn.net/weixin_51159944/article/details/124734594?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22124734594%22%2C%22source%22%3A%22weixin_51159944%22%7D&ctrtid=azCEb

  • 6
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第十三章 文件 对数据的管理无论是用数组还是链表,都是存储在内存中的,程序结束后都会丢失,下一次运行程序时,要重新输入或运算生成数据。要把程序运行的数据保存起来以便下次运行继续使用,在计算机中持久保存数据的方式是利用文件保存。 13.1 文件概述 文件一般是指存储在外部介质上数据的集合。文件以数据形式存放在外部介质上,操作系统以文件为单位对数据进行管理。想找到存在外部介质上的数据,必须按文件名找到指定的文件然后再从该文件中读取数据。要想在外部介质上存储数据必须先建立一个文件(以文件名标识),才能向它输出数据。从操作系统角度,每一个与主机相连的输入输出设备都可以被看作一个文件。在程序运行时,常常需要将一些数据(运行的最终结果或中间数据)输出到磁盘上保存,以后要用时再从磁盘中输入到计算机的内存,这就要用到磁盘文件。 操作系统的文件标识包括三部分: (1)文件路径:表示文件在外部存储设备中的位置。 (2)文件名:遵循标识符的命名规则。 (3)文件扩展名:表示文件的性质(.txt .dat .c)。 如:d:\c++\temp\flie1.dat 文件操作是一种典型的IO操作(输入输出操作)。输入输出是针对内存而言的,进内存为输入,出内存为输出。 标准输入输出就是标准输入设备(键盘)和标准输出设备(显示器),键盘和显示器就是一种文件。C语言将文件看成字符(字节)的序列,即由一个一个字符(字节)的数据顺序组成。C语言中对文件的存取是以字符(字节)为单位的,输入输出数据流的开始和结束仅受程序控制不受物理符号控制(如回车换行符)。输出时不会自动增加回车换行符作为记录结束的标志,输入时不以回车换行符作为记录的间隔(实际上C文件不是由记录构成的),这种文件称为流式文件。 ANSI新标准文件采用缓冲方式,系统自动地在内存区为每一个正在使用的文件开辟一个缓冲区。从内存向磁盘输出数据必先送到输出缓冲区,装满缓冲区后才一起送到磁盘。从磁盘向内存输入数据先送到输入缓冲区,程序需要数据时去缓冲区读取,若缓冲区无数据,则程序进入阻塞状态(等待数据)。 C语言把数据看作是一连串的字符(字节),根据数据的组成形式,分为ASCII文件和二进制文件。 文本文件又称ASCII文件,每个字节存放一个ASCII码,代表一个字符。如1、2的ASCII码分别为49、50,所以整数12用ASCII文件存放时,存放形式为00110001 00110010 二进制文件是直接用数据的二进制形式存放的,即把内存中的数据按其在内存中的存储形式原样输出到磁盘上存放。 程序中实现对文件的处理通常分三步: (1)打开文件:将程序与文件建立联系。 (2)操作文件:对文件进行读写操作,即输入输出。 (3)关闭文件:操作完成应当切断文件与程序的联系。 C语言中没有输入输出语句,对文件的读写都是用库函数实现的。ANSI规定了标准输入输出函数,用它们对文件进行读写,这些函数的声明包含在头文件stdio.h中。 13.2 文件的打开与关闭 文件进行读写操作前先要打开,使用完毕要关闭。打开文件,是建立文件的各种有关信息,并使文件指针指向该文件,以便进行其他操作。关闭文件则断开指针与文件之间的联系。 1.打开文件(fopen函数) 原型:FILE *fopen(char *filename, char *mode); FILE是在stdio.h头文件中定义的一个结构体,用来保存文件信息。 fopen函数用来打开一个文件,其调用的一般形式为: 文件指针名=fopen(文件名,使用文件方式); 其中: ·“文件指针名”必须是被说明为FILE类型的指针变量。 ·“文件名”是被打开文件的文件名,是字符串常量或字符串数组。 ·“使用文件方式”是指文件的类型和操作要求。 如: FILE *fp; fp=fopen("file1.txt","r"); 其意义是在当前目录下打开文件file1.txt,只允许读操作,并使fp指向该文件。 又如: FILE *fp; fp=fopen("c:\\file2.txt","rb"); 意义是打开C驱动器磁盘根目录下的文件file2.txt,对其按二进制方式进行读操作。两个反斜线\\中的第一个表示转义字符,第二个表示根目录。 文件打开方式 r 只读打开一个文本文件,只允许读数据 w 只写打开或建立一个文本文件,只允许写数据 a 追加打开一个文本文件,并在文件末尾写数据 r+ 读写打开一个文本文件,允许读和写 w+ 读写打开或建立一个文本文件,允许读和写 a+ 读写打开一个文本文件,允许读和在文件末尾写数据 rb 只读打开一个二进制文件,只允许读数据 wb 只写打开或建立一个二进制文件,只允许写数据 ab 追加打开一个二进制文件,并在文件末尾写数据 rb+ 读写打开一个二进制文件,允许读和写 wb+ 读写打开或建立一个二进制文件,允许读和写 ab+ 读写打开一个二进制文件,允许读和在文件末尾写数据 (1)文件使用方式由r、w、a、b、+拼成,含义分别为: r(read) 读 w(write) 写 a(append) 追加 b(binary)二进制文件 + 读和写 (2)用r方式打开文件时文件必须存在,只能从该文件读出。 (3)用w方式打开只能向文件写入。若打开的文件不存在,则以指定的文件名建立新文件。若存在,则将存在的文件删除,重建新文件。 (4)向一个已存在的文件追加新的信息,只能用a方式打开。文件必须存在,否则会出错。 (5)打开文件出错时fopen将返回一个空指针值NULL。程序中可用这一信息判断是否完成打开文件的工作,并做相应的处理。因此常用以下程序段打开文件: if((fp=fopen("c:\\file2.txt","rb"))==NULL) { printf("\nerror on open c:\\file2.txt!"); exit(0); } (6)把文本文件读入内存时,要将ASCII码转换成二进制码。把内存数据以文本方式写入磁盘时,要将二进制码转换成ASCII码。因此文本文件的读写花费较多的转换时间,对二进制文件的读写不存在这些转换。 (7)标准输入文件(键盘)、标准输出文件(显示器)、标准出错输出(出错信息)是由系统打开的,可直接使用。 2.关闭文件(fclose函数) 文件使用完毕,应用关闭文件函数把文件关闭,从而把缓冲区的数据写入文件中。否则程序结束时可能造成数据丢失。 fclose函数使用形式: fclose(文件指针); 如:fclose(fp); 正常完成关闭文件操作时,fclose函数返回值0。发生错误时返回EOF。 13.3 文件读写 对文件的读和写是最常用的文件操作,在C语言标准库中提供了多种文件读写的函数。 字符读写函数:fgetc和fputc 字符串读写函数:fgets和fputs 数据块读写函数:fread和fwrite 格式化读写函数:fscanf和fprintf 13.3.1 字符读写函数 1.读字符函数fgetc 规格:int fgetc(FILE * stream); 功能:从指定的文件中读一个字符。 参数:stream为指向文件的指针。 返回值:从stream所指的文件流中读取一个字符,转换为int类型返回。若已到文件尾返回EOF,文件状态改为结束状态。若读错误返回EOF,文件改为错误状态。EOF在stdio.h中定义为-1。 例如: ch=fgetc(fp); 其含义是从打开的文件fp中读取一个字符并送入ch中。 对于fgetc函数的使用有几点说明: (1)在fgetc函数调用中,读取的文件必须是以读或读写方式打开的。 (2)文件内部有一个位置指针,用来指向文件的当前读写字节,文件打开时,位置指针总指向文件的第一个字节。使用fgetc函数后位置指针向后移动一个字节。因此可连续多次使用fgetc函数读取多个字符。文件指针是指向整个文件的,要在程序中定义说明,只要不重新赋值文件指针值不变。文件内部的位置指针用于指示文件内部的当前读写位置,每读写一次,位置指针向后移动,它无需在程序中定义说明,由系统自动设置。 例:读取文本文件file.txt,把其中所有非空格字符输出在标准输出设备上。 #include<stdio.h> #include<conio.h> #include<stdlib.h> int main() { FILE *fp; char ch; if((fp=fopen("file.txt","r"))==NULL) { printf("\nCan't open file,strike any key exit!"); getch(); /*等待敲键盘,显示Can't open file,strike any key exit!*/ exit(1); /*结束程序*/ } ch=fgetc(fp); while(ch!=EOF) /*文件结束时读取得到EOF*/ { if(ch!=' ') putchar(ch); ch=fgetc(fp); } fclose(fp); return 0; }
适合人群:计算机初学者,学习DOS命令 第1课:进入DOS的世界 第2课:DOS命令行的详解 第3课:命令格式的分类、命令行的编辑与窗口的美化 第4课:创建目录之MD命令的详解 第5课:文件管理之删除目录(RD)命令的详解 第6课:显示文件目录之DIR命令的讲解 第7课:文件管理之文件的复制--copy命令 第8课:文件管理之文件的删除--del命令 第9课:文件管理之文件的重命名--ren命令 第10课:文件管理之文件的移动--move命令 第11课:文件管理之type命令的使用 第12课:文件管理之文件属性的设置-attrib命令 第13课:ipconfig 命令详解 第14课:设置启动选项与服务--MSConfig命令 第15课:查看计算端口信息 第16课:测试网络连接命令 第17课:系统设置与管理之自动关机命令详解 第18课:系统设置与管理之任务管理命令详解 第19课:telnet命令的使用演示 第20课:系统设置与管理之时间和日期的更改命令详解数据报 第21课:DOS命令跟踪诊断数据报 第22课:磁盘管理之磁盘碎片整理-defrag命令 第23课:磁盘管理之格式化命令-format命令 第24课:磁盘管理之卷标更改命令--label命令 第25课:文件传输FTP命令的讲解(上) 第26课:文件传输FTP命令的讲解(下) 第27课:系统设置与管理之更改标题与窗口颜色命令-title和color命令 第28课:net user命令详解 第29课:net localgroup命令详解 第30课:net share命令详解 以后会陆续更新,请期待
文件批处理百宝箱提供了几十种功能丰富强大的文件批量更名功能,以及支持世界各国语言的源匹配多国语言批量更名功能;支持对文件夹名的更名功能。   本工具箱还提供了: 图片压大小缩尺寸,按指定大小或尺寸压缩图片,图片转灰度黑白,MP3标签信息的显示与重写,给图片添加竖向/倒向/任意角度文字或图片水印,对图片进行相对/绝对、区域/保留/固定(圆角矩形、椭圆、圆形裁剪)、按大小、缩略缩放压缩、分切分隔、贴边、拼合、颜色魔术棒等10种模式的智能批量裁剪功能;提供对视频音频文件批量裁剪转换,录音录像功能等; 提供了对doc/ppt/txt/asp/html等文档进行智能字符批量替换(支持正则,Word通配符),对txt文本行的添加、删除、提取、统计分割等的处理,对doc/txt类型互转,对xls表合并 ,对ppt转word,Office文档密码筛查, txt文本合并/编码类型检测,文件摘要的显示与改写,     批量随机数的产生,41种特定的txt批量处理功能,文件批量复制搜索(支持文件/文件夹,网上邻居,可复制、移动、删除、更名等),图像仿Photoshop综合处理功能,文件时间属性修改,数码照片视频等的拍摄日期的更改等功能。 通过单次/组合使用,可实现常见的文件批量更名需要和一些文件批量处理需要。集专一性、普适性、灵活性。界面友好,人性化设计,功能实用,是一款不可多得的实用性强的小工具箱;具体用法和操作,会有实时的操作提示、功能提示、操作通知、状态显示等。   文件批量处理百宝箱 v10.0 Build 140103更新内容: 1.在主界面-批替换-txt类型-添加功能模式下,新增在txt文档的开头或末尾插入txt文件内容的功能; 2.在主界面和拓展界面的自动加载文件或者文件夹功能中,新增备用加载方式; 3.在音视频媒体批量裁剪转换功能模块中,新增媒体文件系列分割功能; 4.修正文件夹批量更名功能中,仅添加功能卡中的更名文件夹名生效,而其他功能卡中更改文件夹名未生效的问题; 5.新增文件合并、网址合并入文件中、文件加密/解密功能模块,适合商业广告推广和高强度保密文件场合; 6.修正文件时间属性修改功能模块中,导入文件列表异常的问题; 7.在主界面-xls转txt中,增加了转换格式选项设置 8.修正更名功能模块中,当文件名/文件夹总长度太长时,更名失败的问题; 9.修正文件时间属性修改功能模块中不能加载子文件夹的错误; 10.修正自动加载文件列表或拖放文件加载列表加载结束时,加载文件数不为0但列表的起始记录行出现空白行的异常; 11.修正自动加载文件列表或拖放文件加载列表时,有时出现加载文件数重置归零的异常; 12.修正主界面-批替换-txt类型中,当文档太大,大于几百M,文档页数太多,超过上限32767页时,程序死机问题; 13.修正拓展界面-文档批处理-doc类型的第18项功能中提取doc中图片时提取不完整,漏提的问题; 14.修正主界面-批替换功能卡中,自动加载文件列表后批处理设置按钮未启用问题; 15.修正主界面、拓展界面文件加载异常和未剔除重复加载的文件问题; 16.修正拓展界面-文档批处理-doc-第18项功能中运行是卡死或者报告内存不能为read的问题; 并增加同时提取或处理对象图片的功能; 17.修正Office文档图片批量提取功能模块,修正文件时间属性修改功能模块,以及其他方面的; 18.在Excel文档批量处理功能模块的新增第7项功能,将汇总表中的数据提取后填充空白文档模板中特定的区域; 可用于制衣、制鞋等行业的文档处理需求; 19.在Excel文档批量处理功能模块的第一项中,新增多列首列功能和提取的数据在合并表中的间隔行数指定功能; 20.在主界面-批替换-txt类型中,新增备用处理方式、分块处理方式、边处理边保存方式等功能选项,可处理几十几百M的txt大文件; 21.在主界面-批替换-doc类型中,新增支持其他文件类型如*.h,*.ini,*.php等其他十类文件类型; 22.在主界面更多功能按钮中,右键更多功能按钮,可删除清理垃圾文件和删除赞助程序的功能; 23.修正完善其他性能;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值