Simply loading a text file

I’m trying to get a simple text file to load and display in a simple window for the last day and i’m at a total loss. I’ve been using ofToDataPath in all sorts of ways but nothing.
I have to confess i’m a real noob at this.
I’m coding on the latest XCode on leopard.

i hope you guys can help me.

there are a few ways to do this, one that i’ve used, is if you are trying to get data line by line from a text file

in your h file

  
  
#include <fstream>  
  

in your cpp:

  
  
ifstream fin; //declare a file stream  
fin.open( ofToDataPath("file.txt").c_str() ); //open your text file  
  
vector<string> data; //declare a vector of strings to store data  
  
while(fin!=NULL) //as long as theres still text to be read  
{  
	string str; //declare a string for storage  
	getline(fin, str); //get a line from the file, put it in the string  
	data.push_back(str); //push the string onto a vector of strings  
}  
  

stefanix covered another way to load variables from a text file in this-thread

2 Likes

hey rio!

zach’s example would work well.
just put the vector data; into the testApp.h class so that you can access it in both setup(); and draw();

to then draw it out you could put this in your testApp::draw() in testApp.cpp

ofSetColor(255, 255, 0);
for(int i = 0; i < data.size(); i++){
ofDrawBitmapString(data[i], 10, 10 + i * 14); //draw each lines of text 14 pixels bellow the other
}

For nice looking type you could always switch the text drawing to the ofTrueTypeFont class.

Hope that helps!
Theo