C/C++技术栈 —— C语言基础篇

由于要准备算法赛,所以写下这篇博客,用以巩固基础,对于一些很基础的东西,这里略去。

C语言部分

一、基础语法

scanf()的用法

scanf("%d %*c %d", &var1, &var2);

此处的%*c是用于过滤中间输入的字符的,表示忽略掉一个字符项的输入型。如输入1,d,2
var1得到1,var2得到2

如何防止头文件重复定义?

宏定义法

其中,_NAME_H 是宏的名称,这个名称是唯一的,并且在整个项目中不应该与其他宏名称冲突。当程序首次#include该文件时,由于_NAME_H尚未定义,所以会定义它并执行头文件内容。但当发生多次#include时,由于_NAME_H已经被定义,所以不会再次执行头文件内容,这种写法是写在C/C++标准里的,所有编译器都支持它,优先使用该方式。

#ifndef _NAME_H
#define _NAME_H
// 头文件内容
#endif
pragma once指令法

这条指令用于确保头文件只被编译一次,但不一定被所有编译器支持,所以请忘记它

#pragma once
// 头文件内容

《#pragma once用法总结,及与 #ifndef方式的区别》

二、文件读写

从操作系统的角度看,每一个与主机相连的输出输入设备都
可以看作是一个文件。
大家可以在自己的e盘下,创建这些txt文件,或者让程序去创建。

fileOper1——打开文件

#include<cstdio>
#include<cstdlib>
using namespace std;

int main(){
	FILE *fp;
	fp = fopen("E:\\data.txt","w");//没有将会被创建 
	if(fp){
		printf("can open data.txt!\n");
		printf("hello1");
		
		exit(0);
	}
	if(fclose(fp) != 0){
		cout<<"无法关闭此文件";
		exit(0);
	}
	
	printf("hello2");
	return 0;
}

fileOper2——将控制台的语句写入到文件

#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;

int main(){
	FILE *fp;
	char ch;
	fp = fopen("e:\\data.txt","w");
	if(!fp){
		std::cout<<"无法打开"; 
	}
	else cout<<"file had been opened\n";
		 cout<<"enter sth";
	ch = getchar();		//1步骤 
	while(ch != '#'){
		putchar(ch);
		fputc(ch,fp);//将字符数据输出到fp所指向的磁盘文件
		ch = getchar();//继续1步骤 
	}
	printf("file had been writed, this is the screen!");
	fclose(fp);
	return 0;
}

fileOper3 —— 文件的复制

#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;

int main(){
	FILE *fin,*fout;
	char ch;
	fin = fopen("e:\\data.txt","r");	//注意是r,打开文本文件,只读 
		if(!fin){
			cout<<"无法打开"; 
			exit(0);
		}
	fout = fopen("e:\\data1.txt","w");	//执行的是文件的拷贝 
		if(!fout){
			cout<<"无法打开"; 
			exit(0);
		}
	ch = fgetc(fin);
	while(! feof(fin)){	//file end of fin,是测试文件结尾的函数 
		fputc(ch,fout);//将字符put到fout中,也就是data1.txt中
		putchar(ch);	//显示自己干了什么
		ch = fgetc(fin); 
	}
	//cout<<"file had read over, this is the screen!";
	cout<<"文件已读写完毕,这是显示器输出效果";
	fclose(fin);
	fclose(fout);
	return 0;
}
//这个时候可以看见,data1已经将data的内容复制进来了 

fileOper4——文件的读取

#include<bits/stdc++.h>
using namespace std;

int main(){
	FILE *fin;
	char w[81];
	fin = fopen("e:\\data.txt","w");
	if(!fin){
		cout<<"无法打开此文件";
		exit(0);	//原式的库函数是在cstdlib,或者说是stdlib.h中 
	}
	while(strlen(gets(w)) > 0){	//将控制台的输入内容与形式,原封不动的移动到文件中 
		fputs(w, fin);
		fputs("\n",fin);		//但是会覆盖原有的内容 
	}
	cout<<"over";
	fclose(fin);
	return 0;
}

fileOper5——文件的读取,与复制

#include<bits/stdc++.h>
using namespace std;

//读取第一份文件,然后输出到屏幕上,并且将内容复制到另外一个文本文件中 
int main(){
	FILE *fin, *fout;
	char w[81];
	fin = fopen("e:\\data.txt","r");
		if(!fin){
			cout<<"无法打开data.txt";
			exit(0);
		}
	fout = fopen("e:\\data3.txt","w");
		if(!fout){
			cout<<"无法打开data3.txt";
			exit(0);
		}
	while(fgets(w,80,fin)){	//读取80-1 = 79个字符,如果有这么多的话,若在n-1个字符前遇到回车换行或文件结束符,则读操作结束
		fputs(w,fout); 
		cout<<w;
	}
	cout<<"文件读完毕,这是显示器效果";
	fclose(fin);
	fclose(fout);
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值