Jump to content

Reading from text file.

Go to solution Solved by WanderingFool,

Well this might help a bit

 

Reading files, one line at a time, until the end.

std::string lineData;std::ifstream rFile("text.txt");if(!rFile.is_open())	return "Error on read";while(std::getline(rFile, lineData)) {	//lineData now equals each line, and this will run until EOF}std::rFile.close();

Now for reading each item into an array....this is a bit more tricky.  C++ really doesn't have a tokenizer, although C does (but strtok is a destructive method).  So instead you could just read 1 element at a time using the << from istringstream

std::istringstream iss(lineData);int data;int temp[100]; //Assuming you have an upper limit...you could actually load the data straight into the final array if you		//really wanted though...it would be more efficient and likely cleanerint index = 0;while(index < 100){ //Keeps looping until there is an exit condition or 100 elements have been read	iss >> data;	if(iss.fail()) //Fail will mean data could not be read...thus end of data		break;	temp[index] = data;	index++; //I could have done index++ in the previous line, but it is easier to read like this}//Index will now equal the amount of elements that were in the lineData.  You can now copy them into the array

Let's say I have a file with the below data:

2

4 101 13 50 8

1 50

How would I read each lines separately in to an array like this arr[3][5], I know how to use fstream, but how do I know if i reached the end of a line, and how would I know how many numbers there are in a line.

(C++)

Link to comment
https://linustechtips.com/topic/70887-reading-from-text-file/
Share on other sites

Link to post
Share on other sites

You can use 

getline(isteam, string);

http://www.cplusplus.com/reference/string/string/getline/

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
https://linustechtips.com/topic/70887-reading-from-text-file/#findComment-971660
Share on other sites

Link to post
Share on other sites

Well this might help a bit

 

Reading files, one line at a time, until the end.

std::string lineData;std::ifstream rFile("text.txt");if(!rFile.is_open())	return "Error on read";while(std::getline(rFile, lineData)) {	//lineData now equals each line, and this will run until EOF}std::rFile.close();

Now for reading each item into an array....this is a bit more tricky.  C++ really doesn't have a tokenizer, although C does (but strtok is a destructive method).  So instead you could just read 1 element at a time using the << from istringstream

std::istringstream iss(lineData);int data;int temp[100]; //Assuming you have an upper limit...you could actually load the data straight into the final array if you		//really wanted though...it would be more efficient and likely cleanerint index = 0;while(index < 100){ //Keeps looping until there is an exit condition or 100 elements have been read	iss >> data;	if(iss.fail()) //Fail will mean data could not be read...thus end of data		break;	temp[index] = data;	index++; //I could have done index++ in the previous line, but it is easier to read like this}//Index will now equal the amount of elements that were in the lineData.  You can now copy them into the array

0b10111010 10101101 11110000 00001101

Link to comment
https://linustechtips.com/topic/70887-reading-from-text-file/#findComment-972073
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×