文件操作

基本用法:

1.  freopen("2010_1.txt","r",stdin)

     freopen("2010_1_e.txt","w",stdout);

2.  FILE *pr,*pw;

     pr = fopen("2010_1.txt","r");

     fgets(tmp,100,pr);  //  tmp 是char[] 数组,100 是数组长度

     pw = fopen("2010_1_e.txt","w");

     fputs(tmp,pw);

     fclose(pr);

     fclose(pw);

3. 在文件操作的过程中,经常需要用到整数和字符数组之间的转换,函数是

int atoi(const char *nptr);  // 字符串转换成整数

以及

char*itoa(intvalue,char*string,intradix);// 整数转换成字符串
sprintf();字符串格式化命令,主要功能是把格式化的数据写入某个字符串中。sprintf 是个变参函数
eg.
 
格式化读写函数:fscanf和fprinf
#include <iostream>
using namespace std;
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <map>

char str[360];
int  key,keys;

int main(){
    FILE *fi = fopen("in.txt","r");
    FILE *out = fopen("out.txt","w");
    int cnt = 0;
    while(fscanf(fi,"%d\t%s",&key,str)!= EOF){
          cnt ++;
          cout << cnt << " " << endl;
          fprintf(out,"%d %s\n",key,str);
    }
    ///system("pause");
}

字符读写函数 :fgetc和fputc

#include <iostream>
using namespace std;
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <map>
    
char str[360];
int  key,keys;
char ch;

int main(){
    //FILE *fi = fopen("in.txt","r");
    FILE *out = fopen("out.txt","w");
    while((ch=getchar()) != EOF){
         fputc(ch,out);
    }
    ///system("pause");
}   

 

#include <iostream>
using namespace std;
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <map>
    
char str[360];
int  key,keys;
char ch;

int main(){
    //FILE *fi = fopen("in.txt","r");
    FILE *out = fopen("out.txt","r");
    while((ch = fgetc(out)) != EOF){
         putchar(ch);
    }
    
    system("pause");
    fclose(out);
}



 

字符串读写函数:fgets和fputs

数据块读写函数:fread和fwrite

 

#include <stdio.h>
#include <stdlib.h>

int main(){
    FILE *fp = fopen("in.txt","r");
    if(fp == NULL){
        printf("不能打开文件\n");
        exit(0);      
    }
    int i;
    struct student{
        char name[10];
        int age;
        double score[2];
        char addr[15];       
    };
    struct student stu;
    for(i = 0;i < 1;i++){
        fread(&stu,sizeof(stu),1,fp);
        printf("%s %d %lf %lf %s\n",stu.name,stu.age,stu.score[0],stu.score[1],stu.addr);
    }
    system("pause");
}


 

 
#include <stdio.h>
#include <stdlib.h>

int main(){
    FILE *fp = fopen("out.txt","w");
    if(fp == NULL){
        printf("不能打开文件\n");
        exit(0);      
    }
    int i;
    struct student{
        char name[10];
        int age;
        double score[2];
        char addr[15];       
    };
    struct student stu;
    printf("输入姓名 年龄 分数1 分数2 地址\n");
    for(i = 0;i < 1;i++){
        scanf("%s %d %lf %lf %s",stu.name,&stu.age,&stu.score[0],&stu.score[1],stu.addr);
        fwrite(&stu,sizeof(stu),1,fp);
    }
}

 
#include <stdio.h>
#include <stdlib.h>

int main(){ 
    char str[20];   
    double f=14.309948;   
    sprintf(str,"%6f%6f%d",f,f,1);
    printf("%s\n",str);
    system("pause");
}

结果:
 

下面直接上代码:

第一个例子:将一个文件中的边输出出来(但是是不带权值的边)

#include <iostream>
using namespace std;
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>

int main(){
    char f[100];
    int ff = 0;
    FILE *pr,*pw;
    int a,b,c;
    pr = fopen("1990_1.net","r");
    pw = fopen("1990_1.txt","w");
    while(fgets(f,100,pr)){
         if(ff == 2){
               fputs(f,pw);
         }
         else{ 
            if(f[0] == '*'){
               ff += 1;
            }
         }
    }
    fclose(pr);
    fclose(pw);
    freopen("1990_1.txt","r",stdin);
    freopen("1990_1_e.txt","w",stdout);
    while(scanf("%d %d %d",&a,&b,&c) != EOF){
         printf("%d %d\n",a,b);                 
    }
    //system("pause");
}


另外一个例子:  实现的功能:将一个文件输出到多个文件

#include <iostream>
using namespace std;
#include <fstream>
#include <string>
int main ()
{    
     string s;
     FILE *pw;
     FILE *pr;
     char tmp[10000];
     char fno[20];   //  
     char st[10000][20];
     int f = 1;
     int t = 0;  //  
     pr = fopen("t_2010.txt","r");
     pw = NULL;
     while(fgets(tmp, 10000, pr))  //
     {
          if(tmp[0] == '*')
          {
              f ^= 1;
              if(f == 0)
              {
                    if(pw != NULL)
                    fclose(pw);
                    t++;
                    itoa(t,fno,10);
                    strcpy(st[t],"2010_");
                    strcat(st[t],fno);    
                    strcat(st[t],".net"); 
                    pw = fopen(st[t],"w");
              }
          }
          fputs(tmp, pw);
     }
     fclose(pw); 
     fclose(pr);
}


 freopne 和getline 用法举例

#include <iostream>
using namespace std;
#include <string>
#include <queue>
#define MAXNode  630000
#define MAXEdge  6000000

int head[MAXNode];
struct edge{
    int next;
    int w;
    int v;          // ±ßµÄÄ©¶Ë½Úµã 
}edge[MAXEdge];

bool hash[MAXNode]; //  ¼Ç¼¸Ã¶¥µãÊÇ·ñÒѾ­±»Ëѹý 
int hash_conn[MAXNode];  // hash_conn[i] ±íʾ ½ÚµãÊýΪiµÄÁ¬Í¨·ÖÖ§¸öÊýΪhash_conn[i]
int edge_num = 0;   //  ¼Ç¼±ßµÄÊýÄ¿
int part_num ;

void add(int v1, int v2, int weight){
        edge[edge_num].next = head[v1];
        edge[edge_num].v = v2;
        edge[edge_num].w = weight;
        head[v1] = edge_num++;
}

void dfs(int m){
       part_num ++;
       //cout << m << " " ;
       hash[m] = true;
       for(int j = head[m];j != -1; j = edge[j].next) 
       {
            if(hash[edge[j].v] == false)
            {
                 dfs(edge[j].v);                  
            }
       }
}

void bfs(int m){
      queue<int>Q; 
      while(!Q.empty())
      {
          Q.pop();
      }
      Q.push(m);
      hash[m] = true;
      while(!Q.empty())
      {
           int tq = Q.front();
           Q.pop();
           //cout << tq << " ";
           part_num ++;
           for(int j = head[tq]; j != -1; j = edge[j].next) 
           {
                  if(hash[edge[j].v] == false)
                  { 
                       hash[edge[j].v] = true;
                       Q.push(edge[j].v);
                  }       
           }           
      }    
}

int main()
{
       freopen("2009.net","r",stdin);
       freopen("2009.txt","w",stdout);
       int t[10];       // ¼Ç¼ÁÙʱµÄ±ßȨÐÅÏ¢ t[0] ´ú±í½Úµãv1£¬t[1]´ú±í½Úµãv2£¬t[2]´ú±íȨֵw 
       int N;          //  ´ú±í×ܵĽڵãÊý
       string str;
       bool tag = false;        //  ÅжÏÊÇ·ñ¶Áµ½±ßȨÐÅÏ¢
       memset(head, -1, sizeof(head)); //  ¶Ôhead¸³Öµ 
       while(getline(cin,str))
       {             
             if(tag == true) 
             {
                  int tmp = 0, num = 0; 
                  for(int i = 0;i < str.length();i++)
                  {
                       if(str[i] >= '0' && str[i] <= '9')
                       {
                            tmp = tmp * 10 + str[i] - '0'; 
                       }  
                       else if(str[i] == ' ')
                       {
                            t[num ++] = tmp;     
                            tmp = 0;
                       } 
                  }
                  t[num] = tmp;
                  add(t[0],t[1],t[2]);
                  add(t[1],t[0],t[2]);
                  continue;
             }
             if(str.substr(0,9) == "*Vertices")
             {
                  int tmp = 0;                                  
                  for(int i = 9;i < str.length();i++)
                  {
                       if(str[i] >= '0' && str[i] <= '9')
                       {
                              tmp = tmp * 10 + str[i] - '0';
                       }
                  }  
                  N = tmp;  
                  //cout << N << endl;                           
             }
             else if(str.substr(0,5) == "*Arcs")
             {
                  tag = true;
             }
       }       
       memset(hash, false, sizeof(hash));
       memset(hash_conn, 0, sizeof(hash_conn));
       int maxt = -1;
       for(int i = 1;i <= N;i++)
       {
             if(hash[i] == false)
             {
                 part_num = 0;
                 //dfs(i);
                 bfs(i);
                 //cout << "part_num = " << part_num << endl;
                 hash_conn[part_num] ++;
                 if(part_num > maxt)
                 maxt = part_num;
             }       
       }
       //cout << endl << maxt << endl;
       for(int i = 1;i <= maxt; i++)
       {
             //if(hash_conn[i])  
             printf(",%d,%d",i,hash_conn[i]);
       }
}


 递归遍历文件夹举例:

#include <io.h>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#include <cstdlib>
#include <fstream>
#include <map> 
using namespace std;

#define MAXNODE 550000
char str[3600000];
char tmp[10000];

// 工具函数:拼接两个字符串s1+s2,返回拼接后的字符串
char* addStr(const char* s1, const char* s2)
{
    int l1 = strlen(s1), l2 = strlen(s2);
    char* ret = new char[l1+l2+1];
    strcpy(ret, s1);
    strcpy(ret+l1,s2);
    ret[l1+l2] = 0;
    return ret;
}

// 工具函数:拼接三个字符串s1+s2+s3,返回拼接后的字符串
char* addStr(char* s1, char* s2, char* s3)
{
    return addStr(addStr(s1,s2),s3);
}

// 工具函数:检测是否为正常目录名,即并非仅由'.'组成
inline bool normal_dir_name(char* name)
{
    char* p = name;
    while(*p == '.')
        p++;
    return *p;
}

/**********************************************************************
功能: 递归遍历给定目录中所有子目录与文件
参数: path为将进行遍历的目标目录,必须以\\结尾的字符串,例如".\\"

注释: _finddata_t 结构体中包含以下重要的字段:
long attrib;  // 文件属性,_A_HIDDEN(隐藏)、_A_NORMAL(正常)
              // _A_RDONLY(只读)、_A_SUBDIR(文件夹)、_A_SYSTEM(系统)
time_t time_create;  // 文件创建时间
time_t time_access;  // 文件最近被访问时间
time_t time_write;   // 文件最近被修改时间
_fsize_t size;       // 文件大小,字节为单位
char name[_MAX_FNAME]; // 文件名
 
**********************************************************************/
void dirList(char* path)
{
    printf("-------------------%s------------------------\n",path);
    struct _finddata_t info;
    //printf("%s\n",(addStr(path,"*")));
    long handle = _findfirst(addStr(path,"*"), &info); // *为通配符,如*.exe亦可
    if(handle == -1)
    {
        printf("Fail to read the dir!\n");
        _findclose(handle);
        return;
    }
    
    bool over = false;
    while(!over)
    {
        if(normal_dir_name(info.name)) // .与..不纳入处理范围
        {
            if((info.attrib&_A_SUBDIR) && (info.attrib&_A_HIDDEN)==0){ //子目录递归处理  == 比 && y优先级高 
                dirList(addStr(path,info.name,"\\"));// 做了转义处理 
            }
            else{      // 普通文件处理,Here to add your code....
                tmp[0] = 0;
                strcpy(tmp,path);
                printf("%s ",info.name);
                strcat(tmp,info.name);
                // FILE *pr_pre = freopen(tmp,"r",stdin);
                FILE *pr_pre = fopen(tmp,"r");
                /*if(pr_pre == NULL)
                printf("error\n");
                else{
                   printf("open() succ\n");     
                }*/
                map <int,bool> maps;
                while(fgets(str,3600000,pr_pre)){
                      int i;
                      int key = 0;
                      for(i = 0;;i++){
                          if(str[i] != ':'){
                              key = key * 10 + str[i] - '0';
                          }
                          else
                          break;
                      }
                      while(str[i++] != 0){
                         int tp = 0;
                         for(;str[i] != ' ' && str[i] != 0;i++){
                             tp = tp * 10 + str[i] - '0';
                         }
                         maps[tp] = true;
                         if(str[i+1] == '\n')
                         break;
                      }
                  }
                  int cnt = 0;
                  for(int i = 0;i < MAXNODE;i++){
                      if(maps[i] == true)
                      cnt++;      
                  }
                  printf("%d\n",cnt);
             }
        }
        over = _findnext(handle, &info);
    }
    
    _findclose(handle);
    return;
}

int main() // test
{
    freopen("out.txt","w",stdout);
    dirList("F:\\2014-06-20\\社团演化谱系图\\CPM结果\\"); // for   \\ 也是做了转义处理 
    getchar();
    return 0;   
}


Nano-ESG数据资源库的构建基于2023年初至2024年秋季期间采集的逾84万条新闻文本,从中系统提炼出企业环境、社会及治理维度的信息。其构建流程首先依据特定术语在德语与英语新闻平台上检索,初步锁定与德国DAX 40成分股企业相关联的报道。随后借助嵌入技术对文本段落执行去重操作,以降低内容冗余。继而采用GLiNER这一跨语言零样本实体识别系统,排除与目标企业无关的文档。在此基础上,通过GPT-3.5与GPT-4o等大规模语言模型对文本进行双重筛选:一方面判定其与ESG议题的相关性,另一方面生成简明的内容概要。最终环节由GPT-4o模型完成,它对每篇文献进行ESG情感倾向(正面、中性或负面)的判定,并标注所涉及的ESG具体维度,从而形成具备时序特征的ESG情感与维度标注数据集。 该数据集适用于多类企业可持续性研究,例如ESG情感趋势分析、ESG维度细分类别研究,以及企业可持续性事件的时序演变追踪。研究者可利用数据集内提供的新闻摘要、情感标签与维度分类,深入考察企业在不同时期的环境、社会及治理表现。此外,借助Bertopic等主题建模方法,能够从数据中识别出与企业相关的核心ESG议题,并观察这些议题随时间的演进轨迹。该资源以其开放获取特性与连续的时间覆盖,为探究企业可持续性表现的动态变化提供了系统化的数据基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值