g++调试,C++读取文件,matlab生成txt

g++ -g -o test test.cpp//生成带调试信息的可执行文件
gdb -tui//上面窗口是代码

C++读取txt文件(按行读取)

#include<iostream>
//#include<stdlib.h>
//#include <vector>
//#include <math.h>
#include<string>
#include <fstream>
#include <cassert>
//#include <cmath> 
using namespace std;
void readTxt(string file)
{
    ifstream infile; 
    infile.open(file.data());   //将文件流对象与文件连接起来 
    assert(infile.is_open());   //若失败,则输出错误消息,并终止程序运行 

    string s;
    while(getline(infile,s))
    {
        cout<<s<<endl;
    }
    infile.close();             //关闭文件输入流 
}
int main()
{
    //string str="D:\\BITVehicle_Dataset\\label1.txt ";
    //readTxt(str);
    FILE *fin = fopen("/media/muphy/Elements/BITVehicle_Dataset/label1.txt", "r"); /*假设文件名为file.txt*/
    int a[9850]={0};
int i;
    for (i = 0; i < 9850; i++) {
        fscanf(fin, "%d", &a[i]);
    }
    fclose(fin);
    for(int i=0;i<9850;i++)
        cout<<a[i]<<endl;
    /*此时已经读取完毕,可以进行接下来的处理了*/
    //system("pause");
    return 0;
}


matlab生成label文件,并保存为txt文件

load('VehicleInfo.mat');

a=zeros(9850,1);
for i=1:9850
    c=VehicleInfo(i).vehicles.category;
    %d='Truck'
    if (strcmp(c,'Bus')==1)  
        a(i)=1;
    end
    if (strcmp(c,'Microbus')==1)  
    a(i)=2;
    end
    if (strcmp(c,'Minivan')==1)  
    a(i)=3;
    end
    if (strcmp(c,'Sedan')==1)  
    a(i)=4;
    end
    if (strcmp(c,'SUV')==1)  
    a(i)=5;
    end
    if (strcmp(c,'Truck')==1)  
    a(i)=6;
    end      
end
save label.mat a;
save label.txt -ascii a;
fid=fopen('label1.txt','wt');
fprintf(fid,'%g\n',a);
fclose(fid);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值