Ok, I've asked something similar before, but I still have not been able to understand this... so I just want to have a txt file with math operations in it. Basically, it's just integers multiplies and added. Nothing else.
1*5+10
10+12
Each line can contain any multiplication or addition. The code below is the one I want to use to read it from the text file and process it. As you can see, it's stored in the variable "line". Obviously, I have to get the data out of line in order to do whatever with it - to solve it, check for errors.. etc. How do I do this? From reading, I get the impression I can use a global variable, call it calculation or whatever, and then look through each line and is gonna calculate this for me? I think you gotta have it look for the * and the + and then tell the code how to solve it... ? I am not understanding how to extract the data from "line". The global variable idea was just something someone told me I could do. I don't know.
I will need something like this in the future.. read from text file and append result to a text file, so I need to fully understand it. I also looked into tokens.. but I think there are better ways. I like the way it's being done here with getline.. so I do prefer going this route and just extract the input from line.
/* Open the input file & check that it opened OK */
std::ifstream inFile("file1.txt", std::ios::in);
if ( ! inFile.is_open() ){
std::cerr << "Error: unable to open file for input" << std::endl;
return 1;
}
/* Open the output file & check that it opened OK */
std::ofstream outFile( "file2.txt", std::ios::app ); // Open with append flag
if ( ! outFile.is_open() ){
std::cerr << "Error, unable to open file for output" << std::endl;
return 2;
}
/* Read from the input and append to the output file */
while ( inFile.good() ){
std::string line;
getline( inFile, line
/*I THINK THE LOOP ETC GOES HERE. PROCESSING TAKES PLACE HERE */
outFile << line << std::endl; // Write the file to the output file
}
(by ravenous)