File Operations
在刷题测试程序时,为了避免每次都手工输入,我们可以把输入数据保存在文件中;为了避免输出太长,我们将输出也写入文件中,方便与标准答案文件进行比较。
文件使用一般有两种方法:输入输出重定向、fopen。
- 重定向
这种方法比较简单,只要在main()函数的开始加上:
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
就可以将标准I/O(键盘输入、屏幕输出)转为读写文件。
万一比赛要求标准I/O,而你还想用文件操作来测试代码时,提交时切记删除重定向语句。
为了避免你忘记这茬,可以如下处理:
#define NATIVE
#include <stdio.h> int main(int argc,char** argv)
{
#ifdef NATIVE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
//your code here return ;
}
这样子,本机测试时可以使用重定向;如果要求标准I/O,提交时只需删掉#define NATIVE
即可。
- fopen
如果比赛要求使用文件读写,但禁止重定向方式,这时可以使用fopen方式:
#include <stdio.h> int main(int argc,char** argv)
{
FILE *fin, *fout;
fin = fopen("input.txt"."rb");
fout = fopen("output.txt","wb"); int a; //把scanf改为fscanf,把printf改为fprintf
fscanf(fin,"%d",&a);
fprintf(fout,"%d",a);
//your code here fclose(fin);
fclose(fout); return ;
}
这时候,如果要求标准I/O,只需要:
fin = stdin;
fout = stdout;
最后,简单说下文件比较(windows下):
进入cmd,如果两个文件在同一目录:
fc .txt .txt
如果两个文件不在同一目录:
fc "c:\1.txt" "d:\2.txt"
可以使用fc /?
查看fc命令的一些参数:
File Operations的更多相关文章
- python file operations
原文地址 总是记不住API.昨晚写的时候用到了这些,但是没记住,于是就索性整理一下吧: python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当 ...
- File operations 1
1:只读(‘r' 和 ’rb'以字节读) f = open('d:\模特主妇护士班主任.txt',mode='r',encoding='UTF-8') content = f.read() print ...
- backup, file manipulation operations (such as ALTER DATABASE ADD FILE) and encryption changes on a database must be serialized.
昨天在检查YourSQLDba备份时,发现有台数据库做备份时出现了下面错误信息,如下所示: <Exec> <ctx>yMaint.ShrinkLog</ctx> ...
- VMWare File Format Learning && Use VHD File To Boot VMWare && CoreOS Docker Configuration And Running
目录 . Virtual Machine Introduce . Vmware Image File Format . VHD File Format . Convert VHD File Into ...
- Python File I/O
File is a named location on disk to store related information. It is used to permanently store data ...
- File Operation using SHFileOperation
SHFILEOPSTRUCT Original link: http://winapi.freetechsecrets.com/win32/WIN32SHFILEOPSTRUCT.htm Refere ...
- python文件和文件夹訪问File and Directory Access
http://blog.csdn.net/pipisorry/article/details/47907589 os.path - Common pathname manipulations 都是和路 ...
- Java-Io之文件File
File是"文件"和"目录路径名"的抽象表示形式.File之间继承Object,实现了Serializable和Comparable接口,因此文件支持File对 ...
- MIT-6.828-JOS-lab5:File system, Spawn and Shell
Lab 5: File system, Spawn and Shell tags: mit-6.828 os 概述 本lab将实现JOS的文件系统,只要包括如下四部分: 引入一个文件系统进程(FS进程 ...
随机推荐
- 中阶 d05 tomcat 安装 eclipse上配置tomcat
eclipse使用参考 https://www.bilibili.com/video/av49438855/?p=24 1. 直接解压 ,然后找到bin/startup.bat 2. 可以安装 启动之 ...
- matplotlib IdentityTransform(原地变换)
2020-04-12 23:33:56 -- Edit by yangrayIdentityTransform继承于Affine2DBase类,它是一个高效实现原地变换的类.(不知道有什么用,变换前后 ...
- pgsql中的行锁
pgsql中的行锁 前言 用户可见的锁 regular Lock 行级别 FOR UPDATE FOR NO KEY UPDATE FOR SHARE FOR KEY SHARE 测试下加锁之后的数据 ...
- readelf命令
//查看文件头信息 readelf -h [file] //查看文件依赖的动态库 readelf -d [file] //查看文件中的符号 readelf -s [file]
- 使用StopWatch类来计时 (perf4j-0.9.16.jar 包里的类)
public class StopWatch { static public int AN_HOUR = 60 * 60 * 1000; static public int A_MINUTE = 60 ...
- JAVA+HttpServletRequest文件上传
public Result fileUp(HttpServletRequest request) { RowsVo vo = new RowsVo(); MultipartHttpServletReq ...
- 1 - Apache HttpClient 简单使用
Apache HttpClient 是Apache 开源的实现Http协议的java开源库. HttpClien 是客户端的HTTP通信实现库,实现HTTP GET 和POST请求,获取响应内容. A ...
- Julia基础语法字符和字符串
1.Julia字符串 2.字符
- I. 蚂蚁上树
蚂蚁上树(Sauteed Vermicelli with minced Pork),又名肉末粉条,是四川省及重庆市的特色传统名菜之一.因肉末贴在粉丝上,形似蚂蚁爬在树枝上而得名.这道菜具体的历史,已不 ...
- Linux protobuf
生成C# protobuf 最终文件Net.cs .protoc --descriptor_set_out=a.protobin a.proto .mono protogen.exe -i:Net.p ...