fstream到数组

扫码查看
本文介绍了fstream到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在使用数组实现内存分配方面遇到了一些问题。在这种情况下,我得到了一个存储在文本文件中的样本数据: tri1(1,7,1)(2,8,2)(3, 7,3) tri2(2,8,2)(5,7,5)(3,7,3) tri3(5,7,5) (3,5,2)(3,7,3) tri4(3,7,3)(3,5,2)(1,7,1) tri5(5,7,5)(5,5,7)(3,5,2) 它是一个三角测量表..所以后来我会绘制它是一个三角形.. i想要读取文本文件并将其传递给数组..包括node1,node2和node3 .. i就是这样开始的.. int i; float node1 [3]; float node2 [3]; float node3 [3]; std :: ifstream in(" sample.txt",std :: ios :: in | std :: ios :: binary); if(!in) { std :: cout<< ;无法打开文件<< std :: endl; 返回1; } 然后我没有想法传递价值... 所以,有人可以帮助我吗? p / s:无论如何..这不是学校作业..我只是想尝试图形编程 - openGL .. 解决方案 你可以将完整的文本行读入字符数组,然后使用字符串标记生成器来提取数字,例如 展开 | 选择 | Wrap | 行号 展开 | 选择 | 换行 | 行号 你有几个分隔符错了,试试 展开 | 选择 | Wrap | 行号 i got a little proble to implement memory allocation using array. Here''s the situation, i got a sample data stored in textfile:tri1 (1,7,1) (2,8,2) (3,7,3)tri2 (2,8,2) (5,7,5) (3,7,3)tri3 (5,7,5) (3,5,2) (3,7,3)tri4 (3,7,3) (3,5,2) (1,7,1)tri5 (5,7,5) (5,5,7) (3,5,2)it is a triangulation table.. so later i''ll plot it as a triangle..i would like to read the text file and pass it into array.. consisting node1, node2 and node3..i started like this..int i;float node1[3];float node2[3];float node3[3];std::ifstream in("sample.txt",std::ios::in |std::ios::binary);if(!in){std::cout<<"could not open file"<<std::endl;return 1;}then i ran out of idea to pass the value...so, can anyone help me out?p/s: anyway.. this is not a school assignment.. i just want to try graphic programming - openGL.. 解决方案 you could read a complete line of text into a character array and then use a string tokenizer to extract the numbers, e.g. Expand|Select|Wrap|Line Numbers Expand|Select|Wrap|Line Numbersyou had a couple of your delimiters wrong, try Expand|Select|Wrap|Line Numbers 这篇关于fstream到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-28 01:08
查看更多