Categories
不学无术

1007. Maximum Subsequence Sum (25)

时间限制

400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni+1, …, Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Input Specification:
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.
Output Specification:
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

==============================================
这是一个经典问题,可以利用《编程珠玑》第8章<算法设计技术>里面的扫描算法
算法不写了,书上一模一样的原题,伪代码如下:

maxsofar = 0
maxendinghere = 0
for i = [0,n)
    /* invariant: maxendinghere and maxsofar
       are accurate for x[0..i-1] */
    maxendinghere = max(maxendinghere + x[i], 0)
    maxsofar = max(maxendinghere, maxsofar)

只不过这里输出的时候要输出首末位置的数字,所以加了一些变量。
什么时候会改变首末位置呢:
1. maxEndingHere == 0的时候,表示如果后面还会有更好的结果的话,肯定开始的位置是从这个点之后的一个数字开始的。此时要记录可能会产生gap,留给后续处理
2.当到达vStartIndex,别忘了记下<可能的>新的起点
3.当后面那一坨真的比原来的值还大时,如果存在gap,那要用新的起点替换掉旧的,然后吧gap开关关上,一个新的开始
4.当算出来总数maxEndingHere比maxSofar大时,别忘了记下当前位置的数值作为末尾数值
另外要注意,如果全是负值怎么处理,特别注意这样的输入,不能以maxSofar是否>0判断

3
-1 0 -1

代码中startIndex与endIndex是不必要存储的。
=============================================

#include 
#include  //max
using namespace std;
int main()
{
	int N;
	scanf("%d", &N);
	long maxSofar = 0;
	long maxEndingHere = 0;
	int startVal = 0;
	int startIndex = -1;  //
	int vStartIndex = 0;//
	int vStartVal = 0;
	int endIndex = -1;//
	int endVal = 0;
	bool hasGap = true;
	bool allNegative = true;
	//int *savedVals = new int[N];
	int totalStartVal;
	int totalEndVal;
	for(int i=0; i= 0 && allNegative)
			allNegative = false;
		if(i==0)
			totalStartVal = inputVal;
		else if(i == N-1)
			totalEndVal = inputVal;
		//savedVals[i] = inputVal;
		maxEndingHere = max( maxEndingHere + inputVal, (long)0);
		if(maxEndingHere == 0)
		{
			hasGap = true;
			vStartIndex = i+1;
		}
		if(i == vStartIndex)
			vStartVal = inputVal;
		if( maxEndingHere > maxSofar)
		{
			maxSofar = maxEndingHere;
			endIndex = i;
			endVal = inputVal;
			if(hasGap)
			{
				startIndex = vStartIndex;
				startVal = vStartVal;
				hasGap = false;
			}
		}
	}
	if(!allNegative)
		printf("%ld %d %d", maxSofar, startVal, endVal);
	else
		printf("%ld %d %d", maxSofar, totalStartVal, totalEndVal);
	return 0;
}

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 0 750 2/2
1 答案正确 0 790 1/1
2 答案正确 0 790 3/3
3 答案正确 0 790 4/4
4 答案正确 0 790 4/4
5 答案正确 0 790 3/3
6 答案正确 0 790 3/3
7 答案正确 0 750 5/5

Categories
不学无术

1027. Colors in Mars (20)

1027. Colors in Mars (20)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the last 2 digits for Blue. The only difference is that they use radix 13 (0-9 and A-C) instead of 16. Now given a color in three decimal numbers (each between 0 and 168), you are supposed to output their Mars RGB values.
Input
Each input file contains one test case which occupies a line containing the three decimal color values.
 
Output
For each test case you should output the Mars RGB value in the following format: first output “#”, then followed by a 6-digit number where all the English characters must be upper-cased. If a single color is only 1-digit long, you must print a “0” to the left.
Sample Input

15 43 71

Sample Output

#123456

 

===============================
没啥技术含量。
===============================

#include 
#include 
using namespace std;
string GetRGBVal(int val)
{
	char ret[3];
	ret[2] = '';
	int count = 1;
	while( count >= 0)
	{
		if(val == 0)
			ret[count] = '0';
		int temp = val % 13;
		if(temp >9)
		{
			temp -= 10;
			temp += 0x41;
		}
		else
			temp += 0x30;
		val /= 13;
		ret[count--] = (char)temp;
	}
	return string(ret);
}
int main()
{
	int rgb[3];
	cin >> rgb[0] >> rgb[1] >> rgb[2];
	cout << "#";
	for(int i=0; i<3; i++)
	{
		cout << GetRGBVal(rgb[i]);
	}
	return 0;
}
Categories
不学无术

1039. Course List for Student (25)

1039. Course List for Student (25)

时间限制
200 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes for a query.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers: N (<=40000), the number of students who look for their course lists, and K (<=2500), the total number of courses. Then the student name lists are given for the courses (numbered from 1 to K) in the following format: for each course i, first the course index i and the number of registered students Ni (<= 200) are given in a line. Then in the next line, Ni student names are given. A student name consists of 3 capital English letters plus a one-digit number. Finally the last line contains the N names of students who come for a query. All the names and numbers in a line are separated by a space.
Output Specification:
For each test case, print your results in N lines. Each line corresponds to one student, in the following format: first print the student’s name, then the total number of registered courses of that student, and finally the indices of the courses in increasing order. The query results must be printed in the same order as input. All the data in a line must be separated by a space, with no extra space at the end of the line.
Sample Input:

11 5
4 7
BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
1 4
ANN0 BOB5 JAY9 LOR6
2 7
ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
3 1
BOB5
5 9
AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9

Sample Output:

ZOE1 2 4 5
ANN0 3 1 2 5
BOB5 5 1 2 3 4 5
JOE4 1 2
JAY9 4 1 2 4 5
FRA8 3 2 4 5
DON2 2 4 5
AMY7 1 5
KAT3 3 2 4 5
LOR6 4 1 2 4 5
NON9 0

=================================
PAT上通过率0.09的题目看着挺可怕
主要是时间的问题我想,做那么多次查询~
第一想法是直接用STL的map给名字做索引
这样的话搜索效率是O(log2n),因为map是用神马树实现的
不过…后来想想这个题目那么多跪了的,估计肯定不能用这个
只能拿空间换时间了~
权衡了一下,因为姓名是3个字母加1个数字,所以可以自己写个浪费空间的Hash函数,将名字的字母部分映射成数组的索引
哈希了一下嘛,时间复杂度就是O(1)了,邪恶一笑…
数组使用map>实现,显然前面那个int就是给字母最后的数字使用的,后面那个存的course的ID
这样可能稍微能节约点空间,不然26*26*26*10需要175760那么多地方…浪费地有点多
其他没有什么可以说的,里头有现成的sort,专门对付STL容器里头的排序,自己写个compare函数就行了,相当方便。
提交一次通过,虽然最长时间是180ms有点危险,不过不想再去折腾这个了。
==================================

#include 
#include 
#include 
#include 
using namespace std;
map< int, vector> stu_cou_map[17576];
inline int GetHashIndex(char name[])
{
	//给出一个名字比如BOB5,得到对应 stu_cou_map的索引号
	int index = 676 * (name[0] - 0x41) + 26 * (name[1] - 0x41) + (name[2] - 0x41);
	return index;
}
inline bool mySortFunc (int i, int j)
{
	return (i s;
				s.push_back(course_id);
				stu_cou_map[index].insert( pair>(suffix, s));
			}
			else
			{
				stu_cou_map[index][suffix].push_back(course_id);
			}
		}
	}
	for(int i=0; i s = stu_cou_map[index][suffix];
		printf(" %d", s.size());
		vector::iterator begin = s.begin();
		vector::iterator end = s.end();
		sort(begin, end, mySortFunc);
		for(vector::iterator it = begin; it!=end; it++)
		{
			printf(" %d", *it);
		}
		printf("n");
	}
	return 0;
}

==========================================
测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 0 1250 15/15
1 答案正确 0 1380 2/2
2 答案正确 0 1380 2/2
3 答案正确 0 1380 2/2
4 答案正确 0 1380 2/2
5 答案正确 180 21700 2/2

Categories
不学无术

1051. Pop Sequence (25)

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, …, N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.
Input Specification:
Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.
Output Specification:
For each pop sequence, print in one line “YES” if it is indeed a possible pop sequence of the stack, or “NO” if not.
Sample Input:

5 7 5
1 2 3 4 5 6 7
3 2 1 7 5 6 4
7 6 5 4 3 2 1
5 6 4 3 7 2 1
1 7 6 5 4 3 2

Sample Output:

YES
NO
NO
YES
NO

=================================
数据结构是个好东西。
输入的序列1,2,3,…,N其实是(废话本来就是)一个队列(下面记作seq),然后在弄一个容易被填满的栈…
以下栈底、队首用#号标识
然后看测试的序列,比如5 6 4 3 7 2 1,
1.先看栈顶元素,如果栈顶元素就是输入的元素的话,那就弹栈,然后继续循环
2.如果不是,那么就要将队列的头一直取出,填到栈里面,
直到:(A)栈满了,那么失败了
(B)现在栈顶的元素就是我们的输入元素,那么继续读下一个测试的数字
测试序列5 6 4 3 7 2 1中,一开始的元素是5,于是不断的从1234567这个队列中取头部出来,压栈,然后
栈S : #1 2 3 4 5
seq: # 6 7
满足调节后,下一个循环查看的时候,发现栈顶的5刚好是我们的输入元素,于是序列指针移到6上面,并且弹出5,
经过差不多的过程,变成这样
栈S : #1 2 3 4 6
seq : #7
之后这样
栈S : #1 2 3 4
seq : #7
,
栈S : #1 2 3
seq : #7
,
栈S : #1 2
seq : #7
,
栈S : #1 2 7
seq : #
,
栈S : #1 2
seq : #
,
。。。直到尽头,然后成功了
=======================================================
具体实现可能跟刚才的说法略有不一致,但是原理是一样的。

#include 
#include 
#include 
using namespace std;
int main()
{
    int M, N, K;
    queue sequences;
    queue copy_of_seq; //为了防止重复生成浪费时间
    scanf("%d %d %d", &M, &N, &K);
    //生成seq队列
    for(int i=1; i s;
        bool failed = false;
        int input_val;
        for(int i=0; i= M) //要留一个给后面再读入的相等的元素
                {
                    failed = true;
                    break;
                }
            }
            if( failed )
            {
                printf("NOn");
            }
            else if( copy_of_seq.empty() )
            {
                failed = true;
                printf("NOn");
            }
            else
            {
                //成功了..
                //s.push( copy_of_seq.front() ); (反正还要pop,不搞了)
                copy_of_seq.pop();
            }
            //如果头部还是与数字不同,那就fail
        }
        if (!failed)
            printf("YESn");
    }
    return 0;
}

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 0 740 15/15
1 答案正确 0 750 3/3
2 答案正确 0 790 2/2
3 答案正确 0 790 2/2
4 答案正确 0 750 1/1
5 答案正确 0 780 2/2

Categories
不学无术

1029. Median (25)

Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1={11, 12, 13, 14} is 12, and the median of S2={9, 10, 15, 16, 17} is 15. The median of two sequences is defined to be the median of the nondecreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.
Given two increasing sequences of integers, you are asked to find their median.
Input
Each input file contains one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (<=1000000) is the size of that sequence. Then N integers follow, separated by a space. It is guaranteed that all the integers are in the range of long int.
Output
For each test case you should output the median of the two given sequences in a line.
Sample Input

4 11 12 13 14
5 9 10 15 16 17

Sample Output

13

====================================
这题其实没什么技术含量,因为输入序列都帮你排序好了,时间内存都没啥限制
要注意的一点就是,假定输入序列为A,B,当A或B某个已经被掏空时,需要另外处理,不然就会报段错误什么的…
在优化方面,其实可以先读第一个序列进队列,第二个序列读入时直接处理而不存储,能比原来节省一半空间。
由于题目比较松,没有做这方面处理。
====================================

#include 
#include 
using namespace std;
int main()
{
	///////////Init
	queue q_a ,q_b;
	long N1, N2; //两个序列的长度
	//不存储第二个序列,直接用来比较处理
	///////////
	//读第一个序列
	scanf("%ld", &N1);
	long temp;
	for(long i=0; i

另外测试结果比较恐怖,哈哈
测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 0 660 4/4
1 答案正确 0 790 2/2
10 答案正确 320 17270 3/3
11 答案正确 320 17260 1/1
12 答案正确 320 17260 3/3
13 答案正确 0 740 1/1
2 答案正确 0 740 4/4
3 答案正确 0 750 1/1
4 答案正确 0 790 1/1
5 答案正确 0 750 1/1
6 答案正确 150 9000 1/1
7 答案正确 160 9000 1/1
8 答案正确 160 9010 1/1
9 答案正确 150 8870 1/1

Categories
不学无术

C++ 精确到纳秒计时

#include 
using namespace std;
inline unsigned __int64 GetCycleCount()
{
	__asm _emit 0x0f
	__asm _emit 0x31
}
int main()
{
	unsigned long t0, t1;
	t0 = GetCycleCount();
	t1 = GetCycleCount();
	cout << t1 - t0 << endl;
	return 0;
}

参考文献:http://yanlinjust79.blog.163.com/blog/static/16950848220107254332975/

在Intel Pentium以上级别的CPU中,有一个称为“时间戳(Time Stamp)”的部件,它以64位无符号整型数的格式,记录了自CPU上电以来所经过的时钟周期数。由于目前的CPU主频都非常高,因此这个部件可以达到纳秒级的计时精度。这个精确性是上述两种方法所无法比拟的。
在Pentium以上的CPU中,提供了一条机器指令RDTSC(Read Time Stamp Counter)来读取这个时间戳的数字,并将其保存在EDX:EAX寄存器对中。由于EDX:EAX寄存器对恰好是Win32平台下C++语言保存函数返回值的寄存器,所以我们可以把这条指令看成是一个普通的函数调用。
这个方法的优点是:
1.高精度。可以直接达到纳秒级的计时精度(在1GHz的CPU上每个时钟周期就是一纳秒),这是其他计时方法所难以企及的。
2.成本低。timeGetTime 函数需要链接多媒体库winmm.lib,QueryPerformance* 函数根据MSDN的说明,需要硬件的支持(虽然我还没有见过不支持的机器)和KERNEL库的支持,所以二者都只能在Windows平台下使用(关于DOS平台下的高精度计时问题,可以参考《图形程序开发人员指南》,里面有关于控制定时器8253的详细说明)。但RDTSC指令是一条CPU指令,凡是i386平台下Pentium以上的机器均支持,甚至没有平台的限制(我相信i386版本UNIX和Linux下这个方法同样适用,但没有条件试验),而且函数调用的开销是最小的。
3.具有和CPU主频直接对应的速率关系。一个计数相当于1/(CPU主频Hz数)秒,这样只要知道了CPU的主频,可以直接计算出时间。这和QueryPerformanceCount不同,后者需要通过QueryPerformanceFrequency获取当前计数器每秒的计数次数才能换算成时间。
这个方法的缺点是:
1.现有的C/C++编译器多数不直接支持使用RDTSC指令,需要用直接嵌入机器码的方式编程,比较麻烦。
2.数据抖动比较厉害。其实对任何计量手段而言,精度和稳定性永远是一对矛盾。如果用低精度的timeGetTime来计时,基本上每次计时的结果都是相同的;而RDTSC指令每次结果都不一样,经常有几百甚至上千的差距。这是这种方法高精度本身固有的矛盾。

Categories
不学无术

1008. Elevator (20)

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.
For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.
Input Specification:
Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.
Output Specification:
For each test case, print the total time on a single line.
Sample Input:

3 2 3 1

Sample Output:

41

==============================================
此题初看还以为是电梯算法,其实啥都不是。。。。
注意的是第一个输入的数字是后面的总的输入个数,不是别的其他什么…
答案也很短…好久没碰到这么简单的了
==============================================

#include 
using namespace std;
int main(int argc, char* argv[])
{
	int N;
	cin >> N;
	int floor_now = 0; //当前在0层
	int next_floor = 0;
	int total_time = 0;
	for(int i=0; i> next_floor;
		int time_per_floor = (next_floor - floor_now > 0) ? 6 : -4; //上楼还是下楼
		total_time = total_time + time_per_floor * (next_floor - floor_now) + 5;
		floor_now = next_floor;
	}
	cout << total_time;
	return 0;
}
Categories
不学无术

1035. Password (20)

To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) from O (o in uppercase). One solution is to replace 1 (one) by @, 0 (zero) by %, l by L, and O by o. Now it is your job to write a program to check the accounts generated by the judge, and to help the juge modify the confusing passwords.
Input Specification:
Each input file contains one test case. Each case contains a positive integer N (<= 1000), followed by N lines of accounts. Each account consists of a user name and a password, both are strings of no more than 10 characters with no space.
 
Output Specification:
For each test case, first print the number M of accounts that have been modified, then print in the following M lines the modified accounts info, that is, the user names and the corresponding modified passwords. The accounts must be printed in the same order as they are read in. If no account is modified, print in one line “There are N accounts and no account is modified” where N is the total number of accounts. However, if N is one, you must print “There is 1 account and no account is modified” instead.
Sample Input 1:

3
Team000002 Rlsp0dfa
Team000003 perfectpwd
Team000001 R1spOdfa

Sample Output 1:

2
Team000002 RLsp%dfa
Team000001 R@spodfa

Sample Input 2:

1
team110 abcdefg332

Sample Output 2:

There is 1 account and no account is modified

Sample Input 3:

2
team110 abcdefg222
team220 abcdefg333

Sample Output 3:

There are 2 accounts and no account is modified

======================================
这题没啥技术含量,直接发代码

#include 
#include 
using namespace std;
int main()
{
	int N;
	scanf("%d", &N);
	bool isModified = false;
	int modCount = 0;
	char name[11], pswd[11];
	char *ptrPswd;
	string strOut;
	for(int i=0; i

		
Categories
不学无术

C++ Gossip: 隨機存取檔案

本文来自:http://openhome.cc/Gossip/CppGossip/RadomAccessFile.html

使用 get 指標或 put 指標,可以自由的移動至檔案中指定的位置進行讀取或寫入的動作,通常隨機存取檔案會使用二進位模式進行,文字模式開啟的檔案並不適合作隨機存取的動作。
如何利用隨機存取來讀寫所有的資料,必須視您的需求而定,需求決定您的資料結構,這邊以一個最簡單的例子來示範隨機存取,寫入檔案時都是使用固定大小的struct,由於資料大小固定,這可以方便明確的指定檔案中讀取的位置。
假設有一個簡單的學生成績資料如下:

  • Student.h
#ifndef DATASTRU_H
#define DATASTRU_H
struct Student {
    int studyNumber;
    char name[80];
    double score;
};
#endif

一個結構的大小是固定的,當要寫入一個結構時,可以使用這樣的語法:

fout.write(reinterpret_cast<const char*> (&student),
            sizeof(Student));


其中student是Student自訂struct所宣告的變數名稱,由於write接受const char*型態的變數,所以使用reinterpret_cast<const char*> 將之轉換為const char*指標。
下面這個程式示範,如何建立一個檔案,當中包括50筆空的資料:

  • main.cpp
#include <iostream>
#include <fstream>
#include "Student.h"
using namespace std;
int main(int argc, char* argv[]) {
    if(argc != 2) {
        cout << "指令: create <filename>" << endl;
        return 1;
    }
    ofstream fout(argv[1], ios::binary);
    if(!fout) {
        cerr << "檔案輸出失敗" << endl;
        return 1;
    }
    int count;
    cout << "要建立幾筆資料? ";
    cin >> count;
    Student student = {0, "", 0.0};
    for(int i = 0; i < count; i++) {
        fout.write(reinterpret_cast<const char*> (&student),
            sizeof(Student));
    }
    fout.close();
    return 0;
}

執行結果:

create data.bin
要建立幾筆資料? 50

接下來可以使用下面這個程式進行隨機存取,使用學號來作資料的位置指定,將之儲存在檔案中的指定位置:

#include <iostream>
#include <fstream>
#include "Student.h"
using namespace std;
int main(int argc, char* argv[]) {
    Student student;
    int count = 0;
    if(argc < 2) {
        cerr << "指令: write <filename>";
        return 1;
    }
    fstream fio(argv[1], ios::in | ios::out | ios::binary);
    if(!fio) {
        cerr << "無法讀取檔案" << endl;
        return 1;
    }
    while(true) {
        fio.read(reinterpret_cast<char *> (&student),
            sizeof(Student));
        if(!fio.eof())
            count++;
        else
            break;
    }
    fio.clear();
    cout << "輸入學號(1-" << count << ")"  << endl
         << "輸入0離開";
    while(true) {
        cout << "n學號? ";
        cin >> student.studyNumber;
        if(student.studyNumber == 0)
            break;
        cout << "輸入姓名, 分數" << endl
             << "? ";
        cin >> student.name >> student.score;
        fio.seekp((student.studyNumber - 1) * sizeof(Student));
        fio.write(reinterpret_cast<const char*> (&student),
            sizeof(Student));
    }
    fio.close();
    return 0;
}

執行結果:

write data.bin
輸入學號(1-50)
輸入0離開
學號? 1
輸入姓名, 分數
? 良葛格 88
學號? 2
輸入姓名, 分數
? 毛妹妹 94
學號? 5
輸入姓名, 分數
? 毛毛蟲 75
學號? 0

接下來可以使用下面這個程式讀取方才所輸入的資料:

#include <iostream>
#include <fstream>
#include "Student.h"
using namespace std;
int main(int argc, char* argv[]) {
    Student student;
    int count = 0, number;
    if(argc != 2) {
        cout << "指令: read <filename>" << endl;
        return 1;
    }
    ifstream fin(argv[1], ios::in | ios::binary);
    if(!fin) {
        cerr << "無法讀取檔案" << endl;
        return 1;
    }
    while(true) {
        fin.read(reinterpret_cast<char *> (&student),
            sizeof(Student));
        if(!fin.eof())
            count++;
        else
            break;
    }
    fin.clear();
    cout << "輸入學號(1-" << count << ")"  << endl
         << "輸入0離開";
    while(true) {
        cout << "n學號? ";
        cin >> number;
        if(number == 0)
            break;
        else if(number > count) {
            cout << "輸入學號(1-" << count << ")" << endl;
            continue;
        }
        cout << "n學號t姓名tt分數" << endl;
        fin.seekg((number - 1) * sizeof(Student));
        fin.read(reinterpret_cast<char*> (&student),
            sizeof(Student));
        cout << student.studyNumber << "t"
             << student.name << "tt"
             << student.score << endl;
    }
    fin.close();
    return 0;
}

執行結果:

read data.bin
輸入學號(1-50)
輸入0離開
學號? 1
學號    姓名            分數
1       良葛格          88
學號? 2
學號    姓名            分數
2       毛妹妹          94
學號? 3
學號    姓名            分數
0                       0
學號? 5
學號    姓名            分數
5       毛毛蟲          75
學號? 0

這幾個程式是隨機存取的簡單示範,您也可以結合起來,製作一個簡易的成績登記程式。
在判斷資料筆數時還有更簡單的方法,就是開啟檔案後先使用ios::end將指標移至檔案尾,然後使用tellg()得到目前的檔案指標位置,再除以資料結構的大小除可得知資料筆數,例如:

file.seekg(0, ios::end);
count = file.tellg() / sizeof(資料結構);
 


 

关闭提示 关闭

确 认 取 消

Categories
不学无术

WINDOWS API 文件操作

1. 打开和关闭文件:

  HANDLE CreateFile(

  LPCTSTR lpFileName,                         // file name

  DWORD dwDesiredAccess,                      // access mode

  DWORD dwShareMode,                          // share mode

  LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD

  DWORD dwCreationDisposition,                // how to create

  DWORD dwFlagsAndAttributes,                 // file attributes

  HANDLE hTemplateFile                       // handle to template file

);

CreateFile函数相当强大,Windows下的底层设备差不多都是由它打开的。它可以创建或打开文件、目录、物理磁盘、控制台缓冲区、邮槽和管道等。具体参考MSDN。

2. 移动文件指针:

  DWORD SetFilePointer(
  HANDLE hFile,                // handle to file
  LONG lDistanceToMove,        // bytes to move pointer
  PLONG lpDistanceToMoveHigh,  // bytes to move pointer
  DWORD dwMoveMethod           // starting point
);
 
设置文件结尾标志:
BOOL SetEndOfFile(
  HANDLE hFile   // handle to file
);
该函数移动指定文件的结束标志到文件指针指向的位置。可用来截断或者扩展文件,如果文件扩展,旧的EOF位置和新的EOF位置间的内容是未定义的。参考MSDN。

3. 文件读写:
  BOOL ReadFile(
  HANDLE hFile,                // handle to file
  LPVOID lpBuffer,             // data buffer
  DWORD nNumberOfBytesToRead// number of bytes to read
  LPDWORD lpNumberOfBytesRead, // number of bytes read
  LPOVERLAPPED lpOverlapped    // overlapped buffer
);
 
 文件读取函数
 
  BOOL WriteFile(
  HANDLE hFile,                    // handle to file
  LPCVOID lpBuffer,                // data buffer
  DWORD nNumberOfBytesToWrite,     // number of bytes to write
  LPDWORD lpNumberOfBytesWritten// number of bytes written
  LPOVERLAPPED lpOverlapped        // overlapped buffer
);

 文件写入函数,当Windows文件写文件时,写的的文件通常被Windows暂时保存在内部的高速缓存中,等合适的时间再一并写入磁盘,可以调用FlushFileBuffer(HANDLE hFile  // handle to file)函数来清空数据缓冲区

这两个函数可以同步读写文件,又可以异步读写文件。另外两个函数ReadFileEx和WriteFileEx只能异步读写文件。比上面函数多了一个参数 :

LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine // completion routine

4. 文件锁定与解锁:

  BOOL LockFile(
  HANDLE hFile,                   // handle to file
  DWORD dwFileOffsetLow,          // low-order word of offset
  DWORD dwFileOffsetHigh,         // high-order word of offset
  DWORD nNumberOfBytesToLockLow// low-order word of length
  DWORD nNumberOfBytesToLockHigh  // high-order word of length
);
  BOOL UnlockFile(
  HANDLE hFile,                    // handle to file
  DWORD dwFileOffsetLow,           // low-order word of start
  DWORD dwFileOffsetHigh,          // high-order word of start
  DWORD nNumberOfBytesToUnlockLow, // low-order word of length
  DWORD nNumberOfBytesToUnlockHigh // high-order word of length
);

这两个函数用于当对文件数据一致性要求较高时,为防止程序在写入的过程中其他进程刚好在读取写入的内容,可以对已打开文件的某个部分进行加锁。

函数参数中dwFileOffsetLow和dwFileOffsetHigh参数组合起来指定了加锁区域的开始位置,nNumberOfBytesTLockLow和nNumberOfBytesToLockHigh参数组合起来指定了加锁区域的大小。如果对文件进行了加锁后,当确定程序不在使用时,最好调用UnlockFile显式的解锁,虽然操作系统会对其自动解锁,但操作系统解锁的用的时间取决于当前可用的系统资源,所以有可能造成文件的无法访问。

5. 获取文件信息

  DWORD GetFileType(
  HANDLE hFile   // handle to file
);

获取文件类型,其返回值可参考MSDN。

  DWORD GetFileSize(
  HANDLE hFile,           // handle to file
  LPDWORD lpFileSizeHigh  // high-order word of file size
);

获取文件的大小,函数执行成功将返回文件大小的低双字,如果lpFileSizeHigh参数不是NULL,函数将文件大小的高双字放入它指向的DWORD变量中

  DWORD GetFileAttributes(
  LPCTSTR lpFileName   // name of file or directory
);

如果要查看文件或者目录的属性,可以使用GetFileAttributes函数,它会返回一系列的FAT风格属性信息。返回值可参考MSDN。

  BOOL SetFileAttributes(
  LPCTSTR lpFileName,      // file name
  DWORD dwFileAttributes   // attributes
);

设置文件属性

  BOOL GetFileTime(
  HANDLE hFile,                 // handle to file
  LPFILETIME lpCreationTime,    // creation time
  LPFILETIME lpLastAccessTime// last access time
  LPFILETIME lpLastWriteTime    // last write time
);

获取文件时间,有三个文件时间可供获取:创建时间、最后访问时间、最后写时间。参数具体信息可参考MSDN

  BOOL GetFileInformationByHandle(
  HANDLE hFile,                                  // handle to file
  LPBY_HANDLE_FILE_INFORMATION lpFileInformation // buffer
);

获取所有文件信息,该函数能够获取文件的所有信息,如大小,属性等,另外还有一些其它信息,如文件卷标,索引和链接信息。返回信息保存在lpFileInformation所指向的BY_HANDLE_FILE_INFORMATION中

typedef struct _BY_HANDLE_FILE_INFORMATION {
  DWORD    dwFileAttributes;
  FILETIME  ftCreationTime;
  FILETIME  ftLastAccessTime;
  FILETIME  ftLastWriteTime;
  DWORD    dwVolumeSerialNumber;
  DWORD    nFileSizeHigh;
  DWORD    nFileSizeLow;
  DWORD    nNumberOfLinks;
  DWORD    nFileIndexHigh;
  DWORD    nFileIndexLow;
} BY_HANDLE_FILE_INFORMATION, *PBY_HANDLE_FILE_INFORMATION;
关于此结构的具体信息参考MSDN。
  DWORD GetFullPathName(
  LPCTSTR lpFileName// file name
  DWORD nBufferLength, // size of path buffer
  LPTSTR lpBuffer,     // path buffer
  LPTSTR *lpFilePart   // address of file name in path
);
获取文件的完整路径,需要提醒的是:只有当该文件在当前目录下,结果才正确。如果要得到真正的路径。应该用GetModuleFileName函数。
  DWORD GetModuleFileName(
  HMODULE hModule,    // handle to module
  LPTSTR lpFilename// path buffer
  DWORD nSize         // size of buffer
);

  hModule HMODULE 装载一个程序实例的句柄。如果该参数为NULL,该函数返回该当前应用程序全路径。

   lpFileName LPTSTR 是你存放返回的名字的内存块的指针,是一个输出参数 nSize DWORD ,装载到缓冲区lpFileName的最大值

如果返回为成功,将在lpFileName的缓冲区当中返回相应模块的路径,如果所为的nSize过小,哪么返回仅按所设置缓冲区大小返回相应字符串内容。如果函数失败,返回值将为0,并返回GetLastError异常代码。

  DWORD GetModuleFileNameEx(
  HANDLE hProcess,    // handle to process
  HMODULE hModule,    // handle to module
  LPTSTR lpFilename// path buffer
  DWORD nSize         // maximum characters to retrieve
);
与上一个函数的不同在于多了一个与进程相关的句柄,如果为NULL则lpFilename的值为当前程序的全路径。
  DWORD GetTempPath(
  DWORD nBufferLength// size of buffer
  LPTSTR lpBuffer       // path buffer
);
获取Windows临时文件路径。
  UINT GetTempFileName(
  LPCTSTR lpPathName,      // directory name
  LPCTSTR lpPrefixString// file name prefix
  UINT uUnique,            // integer
  LPTSTR lpTempFileName    // file name buffer
);
在Windows临时目录下创建一个唯一的临时文件。具体参数信息参考MSDN。
 
 
WINSHELLAPI DWORD WINAPI SHGetFileInfo(
LPCTSTR pszPath, 
DWORD dwFileAttributes, 
SHFILEINFO FAR *psfi, 
UINT cbFileInfo, 
UINT uFlags );
获取系统文件信息,如文件,文件夹,目录,驱动器目录

 pszPath 参数:指定的文件名。

  当uFlags的取值中不包含 SHGFI_PIDL时,可直接指定;

  当uFlags的取值中包含 SHGFI_PIDL时pszPath要通过计算获得,不能直接指定;

  dwFileAttributes参数:文件属性。

  仅当uFlags的取值中包含SHGFI_USEFILEATTRIBUTES时有效,一般不用此参数;

  psfi 参数:返回获得的文件信息,是一个记录类型,有以下字段:

  _SHFILEINFOA = record

  hIcon: HICON; { out: icon } //文件的图标句柄

  iIcon: Integer; { out: icon index } //图标的系统索引号

  dwAttributes: DWORD; { out: SFGAO_ flags } //文件的属性值

  szDisplayName: array [0..MAX_PATH-1] of AnsiChar; { out: display name (or path) } //文件的显示名

  szTypeName: array [0..79] of AnsiChar; { out: type name } //文件的类型名

  end;

  cbFileInfo 参数:psfi的比特值;

  uFlags 参数:指明需要返回的文件信息标识符,常用的有以下常数:

  SHGFI_ICON; //获得图标

  SHGFI_DISPLAYNAME; //获得显示名

  SHGFI_TYPENAME; //获得类型名

  SHGFI_ATTRIBUTES; //获得属性

  SHGFI_LARGEICON; //获得大图标

  SHGFI_SMALLICON; //获得小图标

  SHGFI_PIDL; // pszPath是一个标识符

  函数SHGetFileInfo()的返回值也随uFlags的取值变化而有所不同。

  可见通过调用SHGetFileInfo()可以由psfi参数得到文件的图标句柄。但要注意在uFlags参数中不使用SHGFI_PIDL时,SHGetFileInfo()不能获得“我的电脑”等虚似文件夹的信息。

  应该注意的是,在调用SHGetFileInfo()之前,必须使用 CoInitialize 或者OleInitialize 初始化COM,否则表面上能够使用,但是会造成不安全或者丧失部分功能

6. 文件拷贝、删除、移动

  BOOL CopyFile(
  LPCTSTR lpExistingFileName, // name of an existing file
  LPCTSTR lpNewFileName,      // name of new file
  BOOL bFailIfExists          // operation if file exists
);
 
  BOOL CopyFileEx(
  LPCTSTR lpExistingFileName,           // name of existing file
  LPCTSTR lpNewFileName,                // name of new file
  LPPROGRESS_ROUTINE lpProgressRoutine, // callback function
  LPVOID lpData,                        // callback parameter
  LPBOOL pbCancel,                      // cancel status
  DWORD dwCopyFlags                     // copy options
);

文件拷贝函数,CopyFileEx函数附加功能是允许指定一个回调函数,在拷贝过程中,函数每拷贝完一部分数据,就会调用回调函数,回调函数则可由用户指定操作。

  BOOL DeleteFile(
  LPCTSTR lpFileName   // file name
);

文件删除函数,当文件不存在或者文件属性为只读,函数会执行失败,对于只读文件,在删除前就去掉其只读属性。用DeleteFile函数删除的文件不会被放到回收站,它们将永远丢失。

  BOOL MoveFile(
  LPCTSTR lpExistingFileName, // file name
  LPCTSTR lpNewFileName       // new file name
);
  BOOL MoveFileEx(
  LPCTSTR lpExistingFileName// file name
  LPCTSTR lpNewFileName,       // new file name
  DWORD dwFlags                // move options
);

文件移动函数,其主要功能都是用来移动一个存在的文件或目录,当需要指定文件如何移动时,则用MoveFileEx函数,其dwFlags指定文件移动方式,具体说明可参考MSDN。

7. 文件查找

  HANDLE FindFirstFile(
  LPCTSTR lpFileName,               // file name
  LPWIN32_FIND_DATA lpFindFileData  // data buffer
);
  HANDLE FindFirstFileEx(
  LPCTSTR lpFileName,              // file name
  FINDEX_INFO_LEVELS fInfoLevelId, // information level
  LPVOID lpFindFileData,           // information buffer
  FINDEX_SEARCH_OPS fSearchOp,     // filtering type
  LPVOID lpSearchFilter,           // search criteria
  DWORD dwAdditionalFlags          // reserved
);

该函数到一个文件夹(包括子文件夹)去搜索指定文件 如果要使用附加属性去搜索文件的话 可以使用FindFirstFileEx函数。HANDLE hFindFile搜索的文件句柄 函数执行的时候搜索的是此句柄的下一文件

  LPWIN32_FIND_DATA lpFindFileData 指向一个用于保存文件信息的结构体

函数调用成功后,其返回值可用作FindNextFile或FindClose函数的参数。

  BOOL FindNextFile(
  HANDLE hFindFile,                // search handle
  LPWIN32_FIND_DATA lpFindFileData // data buffer
);

在FindFirstFile函数调用成功后继续查找文件。

8. 压缩文件操作

  INT LZOpenFile(
  LPTSTR lpFileName,       // file name
  LPOFSTRUCT lpReOpenBuf// file information buffer
  WORD wStyle              // action to take
);

创建,打开,删除压缩文件。参数信息参考MSDN。

  VOID LZClose(
  INT hFile   // LZ file handle
);

关闭压缩文件,参数为LPOpenFile返回的值。

  LONG LZCopy(
  INT hfSource// LZ file handle for source file
  INT hfDest     // LZ file handle for destination file
);

复制压缩文件并在处理过程中展开,函数的两个参数都可以用LPOpenFile函数得到,只要指定LPOpenFile函数wStyle的不同值。

INT GetExpandedName(
  LPTSTR lpszSource// name of compressed file
  LPTSTR lpszBuffer   // original file name
);

从压缩文件中返回文件名称,参数信息参考MSDN。