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,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...
随机推荐
- Python概念-迭代器的__iter__和__next__
大家都知道__iter__是可迭代对象和迭代器的独有方法,也知道__next__是迭代器的 既然已经学了面向对象了,那么如何自己写一个: 代码示例: # 编辑者:闫龙 class Range: def ...
- win7.wifi热点
使用本地连接上网,将网卡设为wifi热点 cmd 管理员身份运行 netsh wlan set hostednetwork mode=allow ssid=4Gtest key=12345678 网络 ...
- GridView Postback后出错Operation is not valid due to the current state of the object.
一.问题起因 最近项目中有一页面第一次search后正常,但是再次点击其它任何按钮都会报错,亦即postback后页面有问题,经检查是由于页面有一GridView且数据量极大,记录大概有上千条,这儿解 ...
- python3之pymysql模块
1.python3 MySQL数据库链接模块 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. PyMySQL 遵循 Pyt ...
- Add Two Numbers I & II
Add Two Numbers I You have two numbers represented by a linked list, where each node contains a sing ...
- Docker技术这些应用场景【转】
场景一:节省项目环境部署时间 1.单项目打包 每次部署项目到测试.生产等环境,都要部署一大堆依赖的软件.工具,而且部署期间出现问题几率很大,不经意就花费了很长时间. Docker主要理念就是环境打包部 ...
- Python爬虫---requests库快速上手
一.requests库简介 requests是Python的一个HTTP相关的库 requests安装: pip install requests 二.GET请求 import requests # ...
- sed实例收集
url:http://blog.csdn.net/hepeng597/article/details/7852468 一.元字符集 1)^锚定行的开始 如:/^sed/匹配所有以sed开头的行. ...
- shell用户管理->
用户的添加与删除练习 -> 脚本1(if then) 思路:1.条件测试, 脚本使用案例, 创建用户[交互式创建] 1.怎么交互式 read -p 2.接收到对应字符串怎么创建用户 userad ...
- idea中JDK失效
[问题] 在没有改变任何东西的情况下,突然间IDEA里面所有的代码都标红,无法找到JDK [解决方法] [File]->[Invalidate Caches],然后就好了