c++ - Advanced File and String Operations -


so working on model loader in directx 11 program, , ran think unique issue. spent bit of time looking solution this, failed so. problem in file has texture path , list of vertices, want able pick out parts, , remove aswell. below example file texture-less triangle:

t:0$ (0, 5, 0) (5, 0, 0) (-5, 0, 0) 

^ old, @ edit below ^

let me explain what's happening here. first, "t:___" file path texture. have set "0" because not using texture. "$" after "t:0" program's mark end of file path , beginning of vertices.

now, here need program do.

1. read file until character "$" has been reached. erase first 2 characters(the "t:") , "$" if has been added well. put remaining text string called texturedata. p.s. don't erase "t:" file, string(the file needs stay untouched).

2. put remaining text(vertices) temporary string called vertexdata, maybe remove parenthesis..? know how this, maybe not use @ moment.

i hope made myself , issue clear enough.

thanks in advance.

--- important edit ---

i have changed format little bit, looked @ .obj file , decided that easier do. texture , vertex file looks this:

t:0$ v 0 5 0 v 5 0 0 v -5 0 0 

--- end of edit ---

this code here have basis:

model loading function:

bool loadtvf(string fp) {     ifstream tvfreader;     tvfreader.open(fp);     if (tvfreader.is_open())     {         readline(1);    // function not working, need improve         // load vertices , texture strings         tvfreader.close();         return true;     }     else     {         return false;     } } 

readline function(made organize code , jump line , retrieve lines data , put string, cutting , modifying of string needs in main function):

string readline(int character) {     return linedata;    // know doesn't work, don't know return? } 

honestly readline function, have no idea i'm doing. making kind of frame show how prefer code organized.

once again, thank you.

this how it:

bool loadtvf(string fp) {     ifstream tvfreader;     tvfreader.open(fp);     if (tvfreader.is_open()) {         stringstream buffer;         buffer << tvfreader.rdbuf();         string texture_string = buffer.str();         texture_string = texture_string.substr(2, texture_string.length());         // removes t:         texture_string.erase(texture_string.begin() + texture_string.find("$"));         // removes first occurrence of $         std::cout << texture_string;         /* print out:          * 0          * v 0 5 0          * v 5 0 0          * v -5 0 0          */         tvfreader.close();         return true;     }     else     {         return false;     } } 

this doesn't use readline function because loads entire string @ once. since i'm manipulating string @ beginning, rather have parsing , pruning logic happen in 1 place.

if must iterate line-by-line, there's way using getline:

bool loadtvf(string fp) {     ifstream tvfreader;     tvfreader.open(fp);     if (tvfreader.is_open()) {         string line;         while(getline(tvfreader, line))         {             // stuff each line         }         tvfreader.close();         return true;     }     else     {         return false;     } } 

note these lines don't have end-of-line characters in them.

edit: per op's comments, here's how split parsed file separate strings:

bool loadtvf(string fp) {     ifstream tvfreader;     tvfreader.open(fp);     if (tvfreader.is_open()) {         string header;         getline(tvfreader, header);         header = header.substr(2, header.length());         // removes t:         header.erase(header.begin() + header.find("$"));         // removes first occurrence of $         std::cout << header << std::endl;         // should print 0, string between t: , $          stringstream buffer;         string line;         while (getline(tvfreader, line))         {             line = line.substr(2, line.length());             // removes starting "v "             buffer << line << std::endl;             // need end-of-line here because been stripped         }         string texture_string = buffer.str();         std::cout << texture_string;         /* print out:          * 0 5 0          * 5 0 0          * -5 0 0          */         tvfreader.close();         return true;     }     else     {         return false;     } } 

a few notes i'd make here. if you're in control of way file structured, shouldn't need $ character indicate end of line. fact there's newline should indication enough of that. additionally, since every line after represents vector in matrix, don't think v necessary either. finally, t: may unnecessary if files named appropriately, "test.texture". way know file you're reading texture.

this reduce complexity of parsing these files, , bonus, reduces storage sizes these well.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -