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,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...
随机推荐
- [转载]如何做到 jQuery-free?
http://www.ruanyifeng.com/blog/2013/05/jquery-free.html jQuery是现在最流行的JavaScript工具库. 据统计,目前全世界57.3%的网 ...
- Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again错误解决
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm 安装了epel源 但 yum -y ...
- Linux日志文件/var/log详解
更多内容推荐微信公众号,欢迎关注: 如果愿意在Linux环境方面花费些时间,首先就应该知道日志文件的所在位置以及它们包含的内容.在系统运行正常的情况下学习了解这些不同的日志文件有助于你在遇到紧急情况时 ...
- php Only variables should be passed by reference 报错问题
这个错误是变量引用引起的非致命错误,可修改php.ini文件的error_reporting = E_ALL & E_NOTICE 使其屏蔽此错误
- web项目打包后在代码中获取资源文件
在web项目里面,有时代码里面需要引用一些自定义的配置文件,这些配置文件如果放在类路径下,项目经过打包后使用的相对路径也会发生变化,所以以下给出了三种解决方案. 一.properties下配置 在类路 ...
- iptables详细设置
1.安装iptables防火墙 怎么知道系统是否安装了iptables?执行iptables -V,如果显示如: iptables v1.3.5 说明已经安装了iptables. 如果没有安装ipta ...
- linux下C获取系统时间的方法
asctime(将时间和日期以字符串格式表示) 相关函数 time,ctime,gmtime,localtime 表头文件 #include 定义函数 char * asctime(const ...
- my.cnf 详解与优化【转】
MySQL配置文件my.cnf 例子最详细翻译,可以保存做笔记用. #BEGIN CONFIG INFO#DESCR: 4GB RAM, 只使用InnoDB, ACID, 少量的连接, 队列负载大#T ...
- ETL利器Kettle实战应用解析系列二
本系列文章主要索引如下: 一.ETL利器Kettle实战应用解析系列一[Kettle使用介绍] 二.ETL利器Kettle实战应用解析系列二 [应用场景和实战DEMO下载] 三.ETL利器Kettle ...
- 基于RESTful API 设计用户权限控制
RESTful简述 本文是基于RESTful描述的,需要你对这个有初步的了解. RESTful是什么? Representational State Transfer,简称REST,是Roy Fiel ...