FILE operattion
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QFileDialog>
#include <string>
#include <math.h> /* cos */
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(findCliecked()));
connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(parseLogCliecked()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::findCliecked()
{
QStringList fileNameList;
QFileDialog* fd = new QFileDialog(this);
fd->resize(240,320);
//fd->setFixedHeight(tr("Allfile(*.*);;vplog(*.vpl)")); //?置文件??器
fd->setViewMode(QFileDialog::List);
if ( fd->exec() == QDialog::Accepted )
{
fileNameList = fd->selectedFiles();
ui->textEdit->setText(fileNameList[0]);
}
else
{
fd->close();
}
}
void MainWindow::parseLogCliecked()
{
QString qfilename = ui->textEdit->toPlainText();
if (qfilename.isEmpty())
{
return;
}
std::string filename = qfilename.toStdString();
FILE * fp = fopen(filename.c_str(),"rb");
FILE * wfp = NULL;
if (fp)
{
int n = filename.rfind('/');
std::string cvsfilename;
if (n != std::string::npos)
{
cvsfilename = filename.substr(0,n+1);
cvsfilename += "vp.csv";
wfp = fopen(cvsfilename.c_str(),"wb");
if (!wfp)
{
fclose(fp);
return;
}
char buff[] = "Time,X,Y,Z\n";
fwrite(buff, 1, strlen(buff),wfp);
}
char line[1024] = {0};
char outline[1024] = {0};
while(fgets(line,1024,fp))
{
std::string strline = line;
std::string strtime = parseLine(strline,"time:","]");
int m = strtime.find(',');
if (m != std::string::npos)
{
strtime.replace(m,1," ");
}
std::string strX = parseLine(strline,"X:",",");
std::string strY = parseLine(strline,"Y:",",");
std::string strZ = parseLine(strline,"Z:",",");
if (!strX.empty())
{
sprintf(outline,"%s,%s,%s,%s\n",strtime.c_str(),strX.c_str(),strY.c_str(),strZ.c_str());
fwrite(outline,1,strlen(outline),wfp);
}
}
}
fclose(fp);
fclose(wfp);
QMessageBox message(QMessageBox::NoIcon, "Title", "finished.");
message.exec();
}
std::string MainWindow::parseLine(const std::string & strline, const char* start,
const char* end)
{
std::string strResutl;
int pos = strline.find(start);
if (pos != std::string::npos)
{
int pos2= strline.find(end,pos);
if (std::string::npos != pos2)
{
strResutl = strline.substr(pos+2,(pos2 -(pos+2)));
}
}
return strResutl;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QFileDialog>
#include <string>
#include <math.h> /* cos */
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(findCliecked()));
connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(parseLogCliecked()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::findCliecked()
{
QStringList fileNameList;
QFileDialog* fd = new QFileDialog(this);
fd->resize(240,320);
//fd->setFixedHeight(tr("Allfile(*.*);;vplog(*.vpl)")); //?置文件??器
fd->setViewMode(QFileDialog::List);
if ( fd->exec() == QDialog::Accepted )
{
fileNameList = fd->selectedFiles();
ui->textEdit->setText(fileNameList[0]);
}
else
{
fd->close();
}
}
void MainWindow::parseLogCliecked()
{
QString qfilename = ui->textEdit->toPlainText();
if (qfilename.isEmpty())
{
return;
}
std::string filename = qfilename.toStdString();
FILE * fp = fopen(filename.c_str(),"rb");
FILE * wfp = NULL;
if (fp)
{
int n = filename.rfind('/');
std::string cvsfilename;
if (n != std::string::npos)
{
cvsfilename = filename.substr(0,n+1);
cvsfilename += "vp.csv";
wfp = fopen(cvsfilename.c_str(),"wb");
if (!wfp)
{
fclose(fp);
return;
}
char buff[] = "Time,X,Y,Z\n";
fwrite(buff, 1, strlen(buff),wfp);
}
char line[1024] = {0};
char outline[1024] = {0};
while(fgets(line,1024,fp))
{
std::string strline = line;
std::string strtime = parseLine(strline,"time:","]");
int m = strtime.find(',');
if (m != std::string::npos)
{
strtime.replace(m,1," ");
}
std::string strX = parseLine(strline,"X:",",");
std::string strY = parseLine(strline,"Y:",",");
std::string strZ = parseLine(strline,"Z:",",");
if (!strX.empty())
{
sprintf(outline,"%s,%s,%s,%s\n",strtime.c_str(),strX.c_str(),strY.c_str(),strZ.c_str());
fwrite(outline,1,strlen(outline),wfp);
}
}
}
fclose(fp);
fclose(wfp);
QMessageBox message(QMessageBox::NoIcon, "Title", "finished.");
message.exec();
}
std::string MainWindow::parseLine(const std::string & strline, const char* start,
const char* end)
{
std::string strResutl;
int pos = strline.find(start);
if (pos != std::string::npos)
{
int pos2= strline.find(end,pos);
if (std::string::npos != pos2)
{
strResutl = strline.substr(pos+2,(pos2 -(pos+2)));
}
}
return strResutl;
}
FILE operattion的更多相关文章
- 记一个mvn奇怪错误: Archive for required library: 'D:/mvn/repos/junit/junit/3.8.1/junit-3.8.1.jar' in project 'xxx' cannot be read or is not a valid ZIP file
我的maven 项目有一个红色感叹号, 而且Problems 存在 errors : Description Resource Path Location Type Archive for requi ...
- HTML中上传与读取图片或文件(input file)----在路上(25)
input file相关知识简例 在此介绍的input file相关知识为: 上传照片及文件,其中包括单次上传.批量上传.删除照片.增加照片.读取图片.对上传的图片或文件的判断,比如限制图片的张数.限 ...
- logstash file输入,无输出原因与解决办法
1.现象 很多同学在用logstash input 为file的时候,经常会出现如下问题:配置文件无误,logstash有时一直停留在等待输入的界面 2.解释 logstash作为日志分析的管道,在实 ...
- input[tyle="file"]样式修改及上传文件名显示
默认的上传样式我们总觉得不太好看,根据需求总想改成和上下结构统一的风格…… 实现方法和思路: 1.在input元素外加a超链接标签 2.给a标签设置按钮样式 3.设置input[type='file' ...
- .NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍
1年前,我在文章:这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)中(第9个项目),给大家推荐了一个开源免费的PDF读写组件 PDFSharp,PDFSharp我2年前就看过 ...
- [笔记]HAproxy reload config file with uninterrupt session
HAProxy is a high performance load balancer. It is very light-weight, and free, making it a great op ...
- VSCode调试go语言出现:exec: "gcc": executable file not found in %PATH%
1.问题描述 由于安装VS15 Preview 5,搞的系统由重新安装一次:在用vscdoe编译go语言时,出现以下问题: # odbcexec: "gcc": executabl ...
- input type='file'上传控件假样式
采用bootstrap框架样式 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &l ...
- FILE文件流的中fopen、fread、fseek、fclose的使用
FILE文件流用于对文件的快速操作,主要的操作函数有fopen.fseek.fread.fclose,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...
随机推荐
- Diabetic Retinopathy Winner's Interview: 1st place, Ben Graham
Diabetic Retinopathy Winner's Interview: 1st place, Ben Graham Ben Graham finished at the top of the ...
- bootstrap-tooltip+validate
名称 返回类型 描述 validate(options) 返回:Validator 验证所选的FORM valid() 返回:Boolean 检查是否验证通过 rules() 返回:Options 返 ...
- Javascript实现返回上一页面并刷新
今天写了一个小小的提示成功的页面,同时要求返回上一页面,并实现对上一页面的操作进行刷新(例如删除的,添加的),在网上搜寻了一遍,基本上90%的都是说的是用window.history.go(-1), ...
- js中call与apply的区别以及使用~
今天看了一下call与apply的区别~~ <!DOCTYPE html> <html> <head> <title>testCall</titl ...
- python概念-Socket到底有多骚
Socket究竟是什么呢? 简单来说Socket就是用来完成客户端与服务器之间的通信 例如浏览器访问网页,例如网络游戏等一切基于客户端服务器来实现的C/S架构程序 Socket是基于互联网OSI七层协 ...
- UNIX环境高级编程 第3章 文件I/O
前面两章说明了UNIX系统体系和标准及其实现,本章具体讨论UNIX系统I/O实现,包括打开文件.读文件.写文件等. UNIX系统中的大多数文件I/O只需要用到5个函数:open.read.write. ...
- 2016.6.21——Climbing Stairs
Climbing Stairs 本题收获: 1.斐波那契函数f(n) = f(n-1) + f(n -2) 题目: You are climbing a stair case. It takes n ...
- 2016.5.14——leetcode-HappyNumber,House Robber
leetcode:HappyNumber,House Robber 1.Happy Number 这个题中收获2点: 1.拿到题以后考虑特殊情况,代码中考虑1和4,或者说<6的情况,动手算下.( ...
- Hibernate5笔记3--详解Hibernate的API
详解Hibernate的API: (1)Configuration接口: org.hibernate.cfg.Configuration接口的作用是加载主配置文件及映射文件,以实现对Hibernate ...
- innobackupex 相关语法讲解【转】
innobackupex 相关语法讲解 连接服务器 The database user used to connect to the server and its password are speci ...