课程设计——c++中读取txt文件

#include <iostream>#include <string>#include <fstream>using namespace std;

const int Money = 10000;struct Patrons{	string name;	double money;};

int main(){	ifstream inFile;	inFile.open("demo6_9.txt");	if(!inFile.is_open())	{		cout<<"could not open the file \"demo6_9\"\n";		cout<<"program terminating!\n";		exit(EXIT_FAILURE);	}

	int num;	inFile>>num;	cout<<"the number of Patrons is :"<<num<<"."<<endl;	inFile.get();  .//读取换行符//	string name;//	int money;	Patrons *pt = new Patrons[num];	if(inFile.is_open())	{		int i;		for(i=0;i<num;i++)		{	//		inFile>>name;	//		inFile>>money;	//		pt[i].name = name;	//		pt[i].money = money;			getline(inFile,pt[i].name);  //读取一行输入			(inFile>>pt[i].money).get();		}

		int k=0;		cout<<"Grand Patrons:"<<endl;		for(i=0;i<num;i++)			if(pt[i].money >= Money)			{				cout<<pt[i].name<<" "<<pt[i].money<<endl;				k++;			}			if(k==0)			cout<<"none.\n";

		cout<<"Patrons:"<<endl;		if(k==num)			cout<<"none.\n";		else		{			for(i=0;i<num;i++)				if(pt[i].money < Money)					cout<<pt[i].name<<" "<<pt[i].money<<endl;		}	}

	inFile.close();	delete [] pt;	system("pause");	return 0;

}
这是我以前做的练习,你看看吧,应该有所帮助~我的TXT格式是4Sam Stone2000Freida Flass100500Tammy Tubbs5000Rich Raptor55000至于你说的列确定,行不确定,你也可以用类似cin的方法读取~想想你cin怎么用的,ifstream就这么用~
时间: 2024-08-25 22:01:14

课程设计——c++中读取txt文件的相关文章

Android从assets和raw中读取txt文件

方法一.将要读取的txt文件拷贝到Android工程目录下的assets文件夹下 方法二.在res文件夹下新建raw文件夹,将txt拷贝到该目录下 本方法是从assets中读取 /** * 从assets中读取txt */ private void readFromAssets() { try { InputStream is = getAssets().open("qq.txt"); String text = readTextFromSDcard(is); textView.set

python web开发-flask中读取txt文件内容

某些情况下,需要读取flask网站要目录下的txt文件.但是直接在flask网站的目录下创建一个文件是无法访问的.从网站找了一些资料,最终发现通过写一个方法返回txt内容比较简单方便,不过此方法适用于简单的文件读取以及读取量比较小的时候.详细代码如下: @app.route('/<path>')def today(path):base_dir = os.path.dirname(__file__)resp = make_response(open(os.path.join(base_dir,

winform 读取TXT文件 放在Label中

<span style="font-family: Arial, Helvetica, sans-serif;">#region 读取TXT 文件,放到Label中</span> private void ReadTXT(Label lab) { string strTxtAll = "";//定义一个string变量 string abc = "C:\Users\xxb\Desktop\1.txt";//路径 FileS

利用正则表达式读取txt文件中的邮箱,电话号码,url地址,手机号,将数据一行一个保存到一个新的文件中去

本人最近整合了从文本中提取信息,将之保存的新的文本中,在此处用到的是正则表达式,希望大家一起学习. 文件操作类:fileOperation.java package dyx_13; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.F

Flex读取txt文件中的内容报错

Flex读取txt文件中的内容 1.具体错误如下 2.错误原因 读取文件不存在 var file:File = new File(File.applicationDirectory.nativePath+"/phone.txt"); 3.解决办法 将文件导入进去 Flex读取txt文件中的内容报错

Flex读取txt文件中的内容(一)

Flex读取txt文件中的内容 phone.txt: 13000003847 13000003848 13000003849 13000003850 13000003851 13000003852 13000003853 13000003854 13000003855 13000003856 13000003857 13000003858 13000003859 13000003860 13000003861 13000003862 13000003863 13000003864 1300000

Flex读取txt文件中的内容(二)

Flex读取txt文件中的内容 自动生成的文件 LoadTxt-app.xml: <?xml version="1.0" encoding="utf-8" standalone="no"?> <application xmlns="http://ns.adobe.com/air/application/1.5.3"> <!-- Adobe AIR Application Descriptor Fi

java读取txt文件的2中方法---并将内容(每一行以固定的字符分割切成2段)存到map中去

#java读取txt文件的第一种方法 /** * 方法:readTxt * 功能:读取txt文件并把txt文件的内容---每一行作为一个字符串加入到List中去 * 参数:txt文件的地址 * 返回:Map * @param file * @return * @throws IOException */ public static Map<String, String> readTxt(String file) throws IOException { Map<String, Strin

Javascript写入txt和读取txt文件的方法

文章主要介绍了Javascript写入txt和读取txt文件的方法,需要的朋友可以参考下1. 写入 FileSystemObject可以将文件翻译成文件流. 第一步: 例: 复制代码 代码如下: Var fso=new ActiveXObject(Scripting.FileSystemObject); 创建一个可以将文件翻译成文件流的对象. 第二步:用于创建一个textStream 对象 括号里边有三个属性 1. 文件的绝对路径 2. 文件的常数 只读=1,只写=2 ,追加=8 等权限.(Fo