1. 程式人生 > >Matlab 與 c++對txt 文檔的讀寫格式

Matlab 與 c++對txt 文檔的讀寫格式

space 控制臺 描述 col 什麽 let 數據 ret open

  1. 學習g++能夠讀取什麽格式的txt文件。

    讀基本指令:

    >sprintf(filename, "doc_%d.txt", d);

    >fileptr = fopen(filename, "r");

    >while ((fscanf(fileptr, "%d ") != EOF))

    > {fscanf(fileptr, "[%d,%d]", &s1, &s2);

    }

    fclose(fileptr);

    寫基本指令:

    >fileptr = fopen("result_1.txt", "w");

    >fprintf(fileptr, "(%d)", x);

    >fprintf(fileptr, "\n");

    >fclose(fileptr);

具體的代碼及要完成的簡單任務描述如下:

// TestForReadTXT_Windows.cpp : 定義控制臺應用程序的入口點。

//,讀取doc_1.txt中的n行數據,並寫入vector中,這裏不進行運算,直接將xy坐標寫回名為result_1.txt的文件中。

//數據在txt中的存放格式是3條軌跡以固定格式存儲的軌跡數據

//5 [3,0](448,26,11)(447,26,13)(447,27,16)(447,28,17)(448,28,20)

//9 [0, 2](282, 101, 1436)(282, 102, 1437)(283, 102, 1440)(283, 101, 1442)(284, 101, 1445)(285, 101, 1447)(284, 101, 1448)(283, 101, 1451)(282, 101, 1455)

//3 [0,0](407,37,678)(406,37,682)(405,37,684)

//其中第一個代表元素個數,[]代表起止點,(x,y,t)代表軌跡信息

#include "stdafx.h"

#include <iostream>

#include <vector>

using namespace std;

typedef struct{

int x;

int y;

size_t t;

} Point;

class Trajectory{

public:

//Trajectory();

size_t length;

char source;

char sink;

vector<Point> d_point;

};

int _tmain(int argc, _TCHAR* argv[])

{

size_t d, i;

int length, x, y, t, s1, s2, numTrk = 0;

Point p;

Trajectory* traj;

vector<Trajectory> TD;

char filename[100];

FILE *fileptr;

printf("load data ...\n");

for (d = 1; d < 2; d++){

//sprintf(filename, "trks_grand_ss8.txt");

sprintf(filename, "doc_%d.txt", d);

//sprintf(filename, "parkinglot_trk.txt");

fileptr = fopen(filename, "r");

while ((fscanf(fileptr, "%d ", &length) != EOF))//原始數據中的每行第一個數代表軌跡的長度

{

traj = new Trajectory();

fscanf(fileptr, "[%d,%d]", &s1, &s2);//原始數據中的每行第2個代表source,sink點是否被觀察

/* traj->d_source=s1;

traj->d_sink=s2;*/

for (i = 0; i < length; i++){

fscanf(fileptr, "(%d,%d,%d)", &x, &y, &t);

p.x = x; p.y = y; p.t = t;

traj->d_point.push_back(p);// (i, p, s1, s2);

}

fscanf(fileptr, "\n");

traj->sink = s1;

traj->source = s2;

traj->length = length;

TD.push_back(*traj);

//d_trajSet.push_back(*traj);

//d_trajSet[numTrk].trkSource = s1;

//d_trajSet[numTrk].trkSink = s2;

//printf("cur TrkSource is %d, curTrkSink is %d. \n", d_trajSet[numTrk].trkSource,d_trajSet[numTrk].trkSink);

//system( "pause ");

numTrk++;

delete traj;

}

fclose(fileptr);

}

printf("%d trajectories are loaded. \n", numTrk);

fileptr = fopen("result_1.txt", "w");

for (int k = 0; k < numTrk; k++){

for (int w = 0; w < TD.at(k).length; w++){

fprintf(fileptr, "(%d)", TD.at(k).d_point.at(w).x);

}

fprintf(fileptr, "\n");

for (int w = 0; w < TD.at(k).length; w++){

fprintf(fileptr, "(%d)", TD.at(k).d_point.at(w).y);

}

fprintf(fileptr, "\n");

}

fclose(fileptr);

return 0;

}

寫入result_1之後的結果如下:

(448)(447)(447)(447)(448)

(26)(26)(27)(28)(28)

(282)(282)(283)(283)(284)(285)(284)(283)(282)

(101)(102)(102)(101)(101)(101)(101)(101)(101)

(407)(406)(405)

(37)(37)(37)

程序在Ubuntu中的配置:需要多加一個在vc中包含於iostream中的頭文件 #include<stdio.h>

在VC中用sprintf(filename, "..//..//..//data/doc_%d.txt", d);表征上層目錄。

Matlab 與 c++對txt 文檔的讀寫格式