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;
}

时间: 2024-09-27 19:39:57

FILE operattion的相关文章

JavaSE8基础 File lastModified 获取文件夹的修改日期

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) information: 通过这张截图可以看到 测试文件夹 的修改日期. code: package jizuiku0; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; /* * @version V17.09 */ pu

JavaSE8基础 File createNewFile 在一个不存在的文件夹中创建文件时 会抛IO异常(绝对路径)

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) information: 在编写代码时,javase8文件夹的情况截图. code: package jizuiku0; import java.io.File; import java.io.IOException; /* * @version V17.09 */ public class FileDemo_1 { public

JavaSE8基础 File getAbsolutePath 获取当前默认的工作文件夹路径

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) code: package jizuiku0; import java.io.File; /* * @version V17.09 */ public class GetDemo { public static void main(String[] args) { System.out.println("当前的默认工作路径是:&q

JavaSE8基础 File FilenameFilter 筛选出指定后缀名的文件

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) code: package jizuiku0; import java.io.File; import java.io.FilenameFilter; /* * @version V17.09 */ public class Filter { public static void main(String[] args) { //

JavaSE8基础 File list 获取指定文件夹下的第一层文件和文件夹的名字

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) information: 被查看文件夹中的内容截图. code: package jizuiku0; import java.io.File; /* * @version V17.09 */ public class GetName { public static void main(String[] args) { String

设计一个程序能够将某一个目录下面的所有文件名打印出来---File类的使用

,设计一个程序能够将某一个目录下面的所有文件名打印出来 运用到的方法有:返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录:list()           测试此抽象路径名表示的文件是否是一个目录:isDirectory()           返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件:listFiles() package printfilename; import java.io.File; public class PrintFileNam

cnmp安装失败,报错npm ERR! enoent ENOENT: no such file or directory,

1.cnmp安装失败 2.提示如下: bogon:node_modules liangjingming$ sudo npm install cnpm -g --registry=https://registry.npm.taobao.org Password: /usr/local/lib └── (empty) npm ERR! Darwin 16.4.0 npm ERR! argv "/usr/local/Cellar/node/7.8.0/bin/node" "/usr

概述File i/o

1.File对象既可表示文件,也可表示目录(文件夹). 2. 创建一个File对象 File file = new File (String pathName[文件路径名]); 3.在Windows操作系统中,文件路径名中的分隔符可用正斜杠"/",也可用反斜杠"\\". * File类的常用方法 方法名称 说明 boolean exists ( ) 判断文件是否存在 boolean isFile ( ) 判断是否是文件 boolean isDriectory( )

关于利用input的file属性在页面添加图片的问题

在页面添加图片涉及到兼容的问题怎么解决兼容问题呢?请看下面分析: 在IE浏览器上面我们能直接通过获取其input的value值来获取其图片的路径. 在火狐和谷歌需要用createObjectURL((input的元素).files.item(0))来获取其路径: 现在请看代码: css设置样式部分(可以自己设置好看的样式): *{ margin: 0; padding:0; } #img{ width:50px; } .box{ width: 100px; margin:20px auto; }