超大文件中查找关键字

一个有10亿条记录的文本文件,已按照关键字排好序存储。请设计算法,可以快速的从文件中查找指字关键字的记录。
因为已经排好序了,可以使用 二分查找方法检索。
又因为文件过于庞大,可以将文件File分成1000份子记录集即读入10000次,每次读入10亿/10000条记录;
每次 读入一次 10亿/10000 条记录时,记作,将其关键字保存于Records数组中 ,
 当读入一个子记录集时,说明关键字key>=Records[0],故只需将关键字key与Records[i]最后一条记录关键字key'比较,   

  若key<=key',则key在这个records数组中,使用二分查找法检索,若检索不到,则查找失败,返回false结束循环,检索到了,输出true结束循环。

   若key>key',说明不在该子记录集,则读入下一个子记录集, 循环上述步骤。
若全部读到文件尾,还未查找到,则输出false;

 

本题以6千左右记录,每次读入1千,模拟上题;

/*在文件中查找关键字:
* 文件中大概有 6千到七千条记录
* 每次读入1千条记录,将其关键字,存于数组,在其中查找
j为本次读入的记录数; 最后一条记录的数组下标为j-1
* 因为文件中记录是有序的,故,当读一个记录集时, 必然会有 key>=recored[0];
* 只需要判断 key 与recored[j-1]的大小,
* 若key>record[j-1] 说明关键字不在此记录集,进入下一次循环。
* 若key<=record[j-1] 则到此记录集中查找:
* 若查找到,则返回true结束循环,
* 若查找不到,说明 整个文件中均不存在此关键字,返回false结束循环
当读完整个文件时,还未查找到,返回false;

public static int recordNumber=6292;
public static File file=new File("E:"+File.separator+"program"+File.separator+"FilesIO"+File.separator+"Text.txt");

*/


public static boolean findKey(int key)throws IOException{
  BufferedReader reader=new BufferedReader(new FileReader(file));
  for(int i=0;i<7;i++){
    Integer[] record=new Integer[1000];
    int j;
    String s;
    for(j=0;j<1000;j++){
      if((s=reader.readLine())!=null){
        String[] strarray=s.split(" ");
        record[j]=Integer.parseInt(strarray[0]);
      }else{
        break;
      }
    }
    if(key<=record[j-1]){
      return twoDevideFindKey(record,0,j-1,key);
    }

  }

  return false;
}
//二份查找法
public static boolean twoDevideFindKey(Integer[] array,int low,int high,int key){
  if(low<=high){
    int mid=low+(high-low)/2;
    if(array[mid]==key) return true;
      if(array[mid]>key){
        return twoDevideFindKey(array,low,mid-1,key);
      }else{
        return twoDevideFindKey(array,mid+1,high,key);
    }
  }else
    return false;
}

 

-------------------------------------------文件创建,写入


/*
* 创建文件,写入6千左右条记录:
* 每条记录一行,第一个字符为关键字,后面为内容。
* 没有关键字相同的记录;
* 记录按关键字排序
*/
public static void createRecordFile() throws IOException{
  if(!file.exists()){
    System.out.println("文件不存在,将创建文件");
  if(!file.createNewFile()){
    System.out.println("文件已存在");
  }else{
      System.out.println("已创建文件");
    }
  }
  PrintWriter out=new PrintWriter(new FileWriter(file));
  SortedSet<Integer> keySet=new TreeSet<Integer>();
  for(int i=0;i<10000;i++){
    int key=(int) (Math.random()*10000);
    if(!keySet.contains(key))
        keySet.add(key);
  }
  recordNumber=keySet.size();
  Iterator<Integer> iter=keySet.iterator();
  int counter=0;
  while(iter.hasNext()){
    int key=iter.next();
    out.println(key+" "+"example"+counter++);
  }
  if(out!=null) out.close();
  System.out.println("记录数为:"+recordNumber);
}

 

转载于:https://www.cnblogs.com/dan-cnblogs/p/4744753.html

要在C语言文件查找关键字的位置,可以按照以下步骤进行: 1. 打开文件,读取文件内容。 2. 使用字符串搜索函数(如strstr)在文件内容查找关键字出现的位置。 3. 如果找到了关键字,可以使用文件指针的偏移量计算出关键字文件的位置。 以下是一个简单的示例代码,用于在文件查找关键字的位置: ```c #include <stdio.h> #include <string.h> int main() { char keyword[100] = "int"; // 要查找关键字 char filename[100] = "test.c"; // 文件名 FILE* fp = fopen(filename, "r"); // 打开文件 char line[1000]; // 用于存储每行文件内容 int line_num = 0; // 记录当前行数 int found = 0; // 标记是否找到了关键字 if (fp == NULL) { printf("Failed to open file %s\n", filename); return 1; } // 逐行读取文件内容 while (fgets(line, sizeof(line), fp)) { line_num++; if (strstr(line, keyword) != NULL) { // 如果找到了关键字 found = 1; int offset = ftell(fp) - strlen(line); // 计算关键字文件的位置 printf("Keyword \"%s\" found in line %d, offset %d\n", keyword, line_num, offset); } } if (!found) { printf("Keyword \"%s\" not found in file %s\n", keyword, filename); } fclose(fp); // 关闭文件 return 0; } ``` 该代码使用fgets函数逐行读取文件内容,然后使用strstr函数在每行内容查找关键字。如果找到了关键字,则使用ftell函数计算出关键字文件的位置。注意,该代码仅适用于在ASCII编码的文本文件查找关键字,对于其他类型的文件可能需要采用不同的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值