前言
实践一只是熟悉在linux环境下的编程,还没有开始搞服务端和客户端那些。
然后我是使用ubuntu虚拟机进行实践。当然,也不是必须用虚拟机才可以,也可以下个VScode,然后配置个linux环境就行,挺多同学都是这样的。
题目1:
1.在Linux环境下编写Hello World程序。要求给出源代码、编译命令、运行结果。
源代码:
#include<stdio.h>
int main(int argc,char *argv[]){
printf("Hello World!\n");
return 0;
}
编译命令:
需要先在ubuntu安装vim和gcc
sudo apt install vim
sudo apt install gcc
首先在ubuntu桌面打开终端
输入ls查看当前目录下的文件
然后使用vim命令创建C语言文件hello.c:vim hello.c
使用gcc命令进行编译:gcc hello.c
发现已经生成out后缀文件后执行该文件即可:./a.out
(./是指当前目录下)
运行结果:
题目2:
在Linux下编程实现文件的创建、读取、写入、关闭。要求给出源代码、编译命令、运行结果。
源代码:
#include<stdio.h>
int main(int argc,char *argv[]){
FILE *fp1=fopen("test.txt","w+");//file creat and write
if(fp1==NULL){
printf("file open error!\n");
return 0;
} else printf("creat and read file success!\n");
fprintf(fp1,"this is a test file!\n");//input the data
fclose(fp1);//close the file
//and now begin file's read
FILE *fp2=fopen("test.txt","r");
if(fp2==NULL){
printf("file open error!\n");
return 0;
}
char test[10]={0};
while(~fscanf(fp2,"%s",test)){
printf("%s ",test);
}
printf("\n");
fclose(fp2);
return 0;
}
编译命令:
通过vim指令创建C语言文件:vim file.c
进行编译:gcc file.c
运行程序:./a.out
运行结果:
(中间加了一次换行语句)
结语
这篇文章仅仅是一些基础,后面实践二才开始用到服务端和客户端。会进行服务端和客户端的信息交互,还有文本文件的上传和下载。后面有时间我再发出来。