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

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 accountSign in
Already have an account? Sign in here.
Sign In Now