Qt文件read 写

本文介绍了一个使用C++实现的时间读取和写入功能的示例代码,包括从文件中读取时间数据并进行处理,以及将时间和相关内容写入文件的过程。涉及QTime、文件I/O操作、字符串处理等技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

void  MainWindow::  loadtime1()  //读
{/*//int i=0;
    vector<int>  itime;
    int c;
    int g=0;
    int h=0;
    int nn=0;
    int ca[65535]={0};
vector<string>   itime3;
        //  char* c1;
          // fp1=fopen("./name.dat","r");
           string  c2;//temp用来将所有的内容存入,用来存储“12345678910”的



           ifstream fp( "./time.dat" );

               // cout << ch;
            //  fin.close();



       //    ifstream fp("./time.dat", ios::in);
          // string str;
          // getline(fp, c2);


// itime3=c2;
//     char* strc;
//     strc=(char*)itime3.c_str();
//      char * split = ":";
//      char * p;
//     p = strtok (strc,split);


//     while(p!=NULL) {
//    // printf ("%s\n",p);
//       ca[h++]=atoi(p);
//     p = strtok(NULL,split);
//     }
// c=ca[0]*3600+ca[1]*60+ca[2];
// itime.push_back(c);


//  cout << endl;

//       while (getline(fp, c2))//按行读文件

              while( getline(fp,c2) )
           {



             itime3.push_back(c2);
             printf("%s\n", itime3[g].c_str());
             char* strc;
             strc=(char*)itime3[g++].c_str();
              char * split = ":";
              char * p;
             p = strtok (strc,split);


             while(p!=NULL) {
            // printf ("%s\n",p);
               ca[h++]=atoi(p);
             p = strtok(NULL,split);
             }
             h=0;
         c=ca[0]*3600+ca[1]*60+ca[2];

         itime.push_back(c);
      printf("%d\n",itime[nn++]);





     // cout<<itime3[1]<<endl;
           }


    fp.close( );*/





    //int i=0;
        vector<int>  itime;
        int c;
        int g=0;
        int h=0;
        int nn=0;
        int ca[65535]={0};
    vector<string>   itime3;
            //  char* c1;
              // fp1=fopen("./name.dat","r");
               string  c2;//temp用来将所有的内容存入,用来存储“12345678910”的



               ifstream fp( "./time.dat" );

                   // cout << ch;
                //  fin.close();



           //    ifstream fp("./time.dat", ios::in);
              // string str;
              // getline(fp, c2);


    // itime3=c2;
    //     char* strc;
    //     strc=(char*)itime3.c_str();
    //      char * split = ":";
    //      char * p;
    //     p = strtok (strc,split);


    //     while(p!=NULL) {
    //    // printf ("%s\n",p);
    //       ca[h++]=atoi(p);
    //     p = strtok(NULL,split);
    //     }
    // c=ca[0]*3600+ca[1]*60+ca[2];
    // itime.push_back(c);


    //  cout << endl;

    //       while (getline(fp, c2))//按行读文件

                  while( getline(fp,c2) )
               {



                 itime3.push_back(c2);
                 printf("%s\n", itime3[g].c_str());
                 char* strc;
                 strc=(char*)itime3[g++].c_str();
                  char * split = ":";
                  char * p;
                 p = strtok (strc,split);


                 while(p!=NULL) {
                // printf ("%s\n",p);
                   ca[h++]=atoi(p);
                 p = strtok(NULL,split);
                 }
                 h=0;
             c=ca[0]*3600+ca[1]*60+ca[2];

             itime.push_back(c);
          printf("%d\n",itime[nn++]);





         // cout<<itime3[1]<<endl;
               }


        fp.close( );





















    // fp=fopen("./time.dat","r");

     //  fscanf(fp,"%s", strc);



//     while(!feof(fp))
//     {



      //   itime.push_back(c);
       //    fscanf(fp,"%d",&c);
      // i++;
   //  }


  //  fclose(fp);  //这儿获取的数字最准确  获取时间列表的时间  自己加   Qtime类的时间


////   // printf("%d\n",itime.size()); //调试
////    for(int i=0;i<itime.size();i++)
////    printf("%d\n", itime[i]);

//    QTime n(0, 0, 0);                // n = 00:00:00
//QTime  t1;
////MainWindow w1;
//       for(int i=0;i<itime.size();i++)
//       {
//         t1 = n.addSecs(itime[i]);
//      timeClicked(t1);
//       }

//       //qDebug() << t.toString("hh:mm:ss");      //t = 00:01:10


  //  char*  itime2[65535]={"0"};
vector<char*> itime1;
vector<string>  itime2;

    //  char* c1;
      // fp1=fopen("./name.dat","r");





       string  c1;//temp用来将所有的内容存入,用来存储“12345678910”的
    //   ifstream fp1("./name.dat", ios::in); //原来的
          ifstream fp1("./time.dat", ios::in);//后来的
      // string str;

      string str2,str3;
       getline(fp1, c1);
       istringstream is(c1);
       is>>str2>>str3;
       c1=str3;
        str3="";

 itime2.push_back(c1);
   while (getline(fp1, c1))//按行读文件
       {

       istringstream is(c1);
       is>>str2>>str3;
       c1=str3;

       str3="";

         itime2.push_back(c1);
  cout<<itime2[1]<<endl;





       }


fp1.close( );






//  //fscanf(fp1,"%s\r\n",c1);
//        // fgets(c1,22,fp1);
//// getline(fp1, c1);//按行读文件

//  //cout << str << endl;//这
//  string str;
//  str=c1;
// itime1.push_back(c1);

// itime2.push_back(str);


//    //  itime2[i++]=c1;
//      while(!feof(fp1))
//       //   for(int j=0;j<i;j++)
//       {
//    //  itime2[i++]=c1;
//     itime1.push_back(c1);
//      itime2.push_back(str);
//  // printf("%s\n",itime1[1]);
////   cout<<itime2[1];
//    getline(fp1, c1);//按行读文件
//    //  fscanf(fp1,"%s\r\n",c1);
//          //  fgets(c1,22,fp1);
//        //   printf("%s\n",itime1[2]);
//       }

      //fclose(fp1);  //这儿获取的数字最准确  获取时间列表的时间  自己加   Qtime类的时间







      QTime n(0, 0, 0);                // n = 00:00:00
  QTime  t1;
  //MainWindow w1;


      QString  t2;

      for(int j=0;j<itime.size();j++)
      {    //printf("%s\n",itime1[i]);
          printf("%s\n",itime2[j].c_str());
          t1 = n.addSecs(itime[j]);

          QTextCodec::setCodecForTr(QTextCodec::codecForName("utf-8")); //保证写出的文字显示不乱码 编码格式的转化
           //用GBK编的码就用gbk来解码白框里显示名字
          t2= QString(tr(itime2[j].c_str())); //char 转


                 QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK")); //弹出的对话框中显示的文字

     timeClicked(t1,t2);
      }








}








void MainWindow::addClicked()//写
{



    timeList<<ui->timeEdit->time();//输入时间框获取时间的
    contentList<<(ui->textEdit->toPlainText().isNull() ? " ":ui->textEdit->toPlainText());
    if (ui->textEdit->toPlainText().isEmpty())
    {
        QMessageBox::warning(this, tr("设置时间"),tr("请输入备忘内容"));
         return;
    }
    ui->listWidget->addItem(timeList.last().toString("hh:mm:ss")+"  "+contentList.last());



    QTimer *tempTimer=new QTimer(this);
    timerList<<(tempTimer);



    if(((timeList.last().hour()*3600+timeList.last().minute()*60+timeList.last().second())-(QTime::currentTime().hour()*3600+QTime::currentTime().minute()*60+QTime::currentTime().second()))>0)
    {


//        FILE *  fp=fopen("./time.dat","a");
//        fprintf(fp,"%d:%d:%d\n",timeList.last().hour(),timeList.last().minute(),timeList.last().second());
//        fclose(fp);  //这儿获取的数字最准确  获取时间列表的时间  自己加   Qtime类的时间



        //把Qtime时间转换为妙
//        if(flag==1)
//        {
////            fp=fopen("./time.dat","a");
////            fprintf(fp,"%d\n",timeList.last().hour()*3600+timeList.last().minute()*60+timeList.last().second());
////            fclose(fp);  //这儿获取的数字最准确  获取时间列表的时间  自己加   Qtime类的时间


//                  fp=fopen("./time.dat","a");
//                  fprintf(fp,"%d:%d:%d\n",timeList.last().hour(),timeList.last().minute(),timeList.last().second());
//                   fclose(fp);  //这儿获取的数字最准确  获取时间列表的时间  自己加   Qtime类的时间
//        }





     qstr=ui->textEdit->toPlainText();
     char* ch;
       //  QByteArray ba = contact.toLatin1();
        // QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
      //   QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
         QTextCodec::setCodecForTr(QTextCodec::codecForName("utf-8"));
         QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
         ch = qstr.toLocal8Bit().data();//字符串转化


//         if(flag==1)
//         {
//         fp1=fopen("./name.dat","a");
//        fprintf(fp1,"%s\n",ch);
//        fclose(fp1);  //这儿获取的数字最准确  获取时间列表的时间  自己加    应该写在保存之后的内容里
//        }


                  if(flag==1)
                  {

                      fp=fopen("./time.dat","a");


                 fprintf(fp,"%d:%d:%d ",timeList.last().hour(),timeList.last().minute(),timeList.last().second());
                 fprintf(fp,"%s\n",ch);
                  fclose(fp);  //这儿获取的数字最准确  获取时间列表的时间  自己加   Qtime类的时间

                 }


         cstr.push_back((string)ch); //c语言字符串转c++

  QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
  //QTextCodec::setCodecForTr(QTextCodec::codecForName("utf-8")); //保证写出的文字显示不乱码 编码格式的转化






    timerList.last()->start(((timeList.last().hour()*3600+timeList.last().minute()*60+timeList.last().second())-(QTime::currentTime().hour()*3600+QTime::currentTime().minute()*60+QTime::currentTime().second()))*1000);
    timerList.last()->setSingleShot(true);
    connect(timerList.last(),SIGNAL(timeout()),this,SLOT(showMessage()));

  //int i=ui->listWidget->currentRow();


    }
}






void MainWindow::timeClicked(QTime  T,QString T2) //读
{





   // timeList<<ui->timeEdit->time();//输入时间框获取时间的
    timeList<<T;
    contentList<<(T2.isNull() ? " ":T2);
    ui->listWidget->addItem(timeList.last().toString("hh:mm:ss")+"  "+contentList.last());



    QTimer *tempTimer=new QTimer(this);
    timerList<<(tempTimer);



    if(((timeList.last().hour()*3600+timeList.last().minute()*60+timeList.last().second())-(QTime::currentTime().hour()*3600+QTime::currentTime().minute()*60+QTime::currentTime().second()))>0)
    {


//        FILE *  fp=fopen("./time.dat","a");
//        fprintf(fp,"%d:%d:%d\n",timeList.last().hour(),timeList.last().minute(),timeList.last().second());
//        fclose(fp);  //这儿获取的数字最准确  获取时间列表的时间  自己加   Qtime类的时间



        //把Qtime时间转换为妙

//                fp=fopen("./time.dat","a");
//                fprintf(fp,"%d\n",timeList.last().hour()*3600+timeList.last().minute()*60+timeList.last().second());
//                fclose(fp);  //这儿获取的数字最准确  获取时间列表的时间  自己加   Qtime类的时间






     qstr=T2;
     char* ch;
       //  QByteArray ba = contact.toLatin1();
        // QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
      //   QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
         QTextCodec::setCodecForTr(QTextCodec::codecForName("utf-8"));
         QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
         ch = qstr.toLocal8Bit().data();//字符串转化
     //   FILE *  fp1=fopen("./name.dat","a");
    //    fprintf(fp1,"%s\n",ch);
    //    fclose(fp1);  //这儿获取的数字最准确  获取时间列表的时间  自己加    应该写在保存之后的内容里

         cstr.push_back((string)ch); //c语言字符串转c++

  QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
  //QTextCodec::setCodecForTr(QTextCodec::codecForName("utf-8")); //保证写出的文字显示不乱码 编码格式的转化

    timerList.last()->start(((timeList.last().hour()*3600+timeList.last().minute()*60+timeList.last().second())-(QTime::currentTime().hour()*3600+QTime::currentTime().minute()*60+QTime::currentTime().second()))*1000);
    timerList.last()->setSingleShot(true);
    connect(timerList.last(),SIGNAL(timeout()),this,SLOT(showMessage()));

  //int i=ui->listWidget->currentRow();


   }


}








                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值