freopen用法:若干整数求最大值

描述

输入若干整数,可正可负,输出最大值,将测试数据写入文件,由程序直接读入数据,不使用键盘键入文件。

freopen用法:

此后所有数据输入都来自C:\Users\Lenovo\Desktop\algorithm\test.txt
为何freopen后面都是\\,即双斜杠?
答:因为C++中想要表示单斜杠\,需要用双斜杠\\来表示。
前面是文件位置,后面一定要跟上文件名即test.txt

mycode

# include <iostream>
# include <cstdio>
# include <math.h>
using namespace std;
int main()
{
	freopen("C:\\Users\\Lenovo\\Desktop\\algorithm\\test.txt","r",stdin);
	int n=0,b=0;
	cin>>n;
	b=n;
	while(cin>>n)
	{

		if(b<n)
		{
			b=n;
		}
	}
	cout << b;
	
}

注意

    freopen("C:\\Users\\Lenovo\\Desktop\\algorithm\\test.txt","r",stdin);