Is there a way to getline just part of a line? - c++

I couldn't come up with a better title, but let me explain.
I have a file like this
potions.txt potions
ingredients.txt ingredients
INSERT((Red Mountain Flower|0.1|2|Yes|No|Mountains,Forests),ingredients)
INSERT((Abecean Longfin|0.5|15|No|Yes|Rivers,Lakes),ingredients)
INSERT((48|Glibness|Fortify|+20 Speechcraft for 60 seconds.|96|None|None),potions)
UPDATE((Abecean Longfin|0.5|15|No|Yes|Rivers,Lakes,Swamps),ingredients)
UPDATE((205|Minor Healing|Health|Restore 25 points of Health.|17|Blue Mountain Flower|Charred Skeever Hide),potions)
UPDATE((206|Healing|Health|Restore 50 points of Health.|36|Blue Mountain Flower|Swamp Fungal Pod),potions)
SELECT((9|*|*|*|*|*|*),potions)
INSERT((Purple Mountain Flower|0.1|2|Yes|No|Mountains,Forests),ingredients)
I am trying to parse the file to store the proper things into the proper variable.
So, I tried writing
for(int i = 0; i < num_of_lines; i++)
{
getline(inputFile, insert, '(');
if(insert == "INSERT")
{
cout << insert << endl;
}
}
And I immediately know my problem. When the for loop continues, the order it will read things in is
(
Red Mountain Flower|0.1|2|Yes|No|Mountains,Forests),ingredients)INSERT(
(Abecean Longfin|0.5|15|No|Yes|Rivers,Lakes),ingredients)INSERT(
Meaning it will never get another "insert" to read in so i'll never have access to it in order to parse the file further.
Is there a way to getline just part of a line so that If the string matches, I can continue to parse the file? I've tried find functions, I've tried string compare functions, but I can't seem to get anything to work. Any suggestions on how i can solve this would be appreciated.

The input is obviously line-based, so read it line by line and then parse those lines:
for(int i = 0; i < num_of_lines; i++)
{
getline(inputFile, lineText);
std::istringstream line(lineText);
// Now, work with `line` as your stream
getline(line, insert, '(');
if(insert == "INSERT")
{
cout << insert << endl;
}
}

Related

How to extract specific substring from getline function in C++?

I'm fairly new to C++ so please forgive me if my terminology or methodology isn't correct.
I'm trying to write a simple program that:
Opens two input files ("infileicd" and "infilesel").
Opens a single output file "list.txt".
Compares "infilesel" to "infileicd" line by line.
If a line from "infilesel" is found in "infileicd", it writes that line from "infileicd" to "list.txt", effectively making a separate log file.
I am using the getline() function to do this but have run into trouble when trying to compare each file line. I think it might be easier if I could use only the substring of interest to use as a comparison.
The problem is that there are multiple words within the entire getline string and I am only really interested in the second one. Here are two examples:
"1529 nic1_mau_op_mode_3 "8664afm007-01" "1" OUTPUT 1 0 LOGICAL 4 4136"
"1523 pilot_mfd_only_sel "8664afm003-02" "1" OUTPUT 1 0 LOGICAL 4 4112"
"nic1_mau_op_mode_3" and "pilot_mfd_only_sel" are the only substrings of interest.
It would make it a lot easier if I could only use that second substring to compare but I don't know how to extract it specifically from the getline() function. I haven't found anything suggesting it is impossible to do this, but if it is impossible, what would be an alternative method for extracting that substring?
This is a personal project so I'm under no time contstraints.
Any assistance is greatly apprecated in advance. Here is my code (so far):
int main()
{
//Open the file to write the selected variables to.
ofstream writer("list.txt");
//Open the selected variabels file to be read.
ifstream infilesel;
infilesel.open("varsel.txt");
//Open the icd file to be read.
ifstream infileicd;
infileicd.open("aic_fdk_host.txt");
//Check icd file for errors.
if (infileicd.fail()){
cerr << "Error opening icd.\n" << endl;
return 1;
}
else {
cout << "The icd file has been opened.\n";
}
//Check selected variables file for errors.
if (infilesel.fail()){
cerr << "Error opening selection file.\n" << endl;
return 1;
}
else {
cout << "The selection file has been opened.\n";
}
//Read each infile and copy contents of icd file to the list file.
string namesel;
string nameicd;
while(!infileicd.eof()){
getline(infileicd, nameicd);
getline(infilesel, namesel);
if (nameicd != namesel){ //This is where I would like to extract and compare the two specific strings
infileicd; //Skip to next line if not the same
} else {
writer << nameicd << namesel << endl;
}
}
writer.close();
infilesel.close();
infileicd.close();
return 0;
}
So, based on what we discussed in the comments, you just need to toss the stuff you don't want. So try this:
string namesel;
string nameicd;
string junk;
while(!infileicd.eof()){
// Get the first section, which we'll ignore
getline(infileicd, junk, ' ');
getline(infilesel, junk, ' ');
// Get the real data
getline(infileicd, nameicd, ' ');
getline(infilesel, namesel, ' ');
// Get the rest of the line, which we'll ignore
getline(infileicd, junk);
getline(infilesel, junk);
Basically, getline takes a delimiter, which by default is a newline. By setting it as a space the first time, you get rid of the first junk section, using the same method, you get the part you want, and then the final portion goes to the end of the line, also ignoring it.

how to print X amount of lines after finding a string in a file using C++

I'm trying to implement a function in C++ that searches for a string in a file, then prints the the line containing the string AND X subsequent lines.
I have the following code that works for finding the string and printing the line, but I can't get it to work to print the lines under the line with the string.
void repturno(void){
system("cls");
string codemp, line,output;
bool found = false;
ifstream myfile ("Pacientes.csv");
//captures the string that i need to look for, in this case an employee code
cout<<"\nBienvenida enfermera en turno, por favor introduzca su codigo"<<endl;
cin.ignore();
getline(cin,codemp);
system("cls");
/*reads the file and searches for the string, then prints the whole line, then searches again for
the string and if finds it, will print the whole line again*/
while (getline(myfile, line)) {
if (line.find(codemp) != string::npos) {
cout<<line<<endl;
getline(myfile,line);
found = true;
}
}
//using this to check if the code was working, and verifying if the string was found or not :P
if( found == false){
cout <<"No se encontro la cadena"<< endl;
}
system("pause");
return menu();
}
Here are my problems:
I can't get the cursor to go down to the next line and print it (need to do this 4 times), everything just gets messy and I end up with the same string and its line printed all over the screen many times.
I can't save all the data pertinent to each employee in a single line (it would solve the problem in this function, but would create other problems); there's too much info for each employee, way more than the 80 characters allowed in the console and it messes up other parts of my program where I have to display the information on the screen and it looks really bad when i print it, even when trying to use gotoxy or '\t' for proper spacing. I had to save the data line by line, each category in a different line, so I can properly display it in other parts of my programs.
I came with 2 solutions in pseudo-code, but i don't know how to translate them into C++ sentences:
NOTE: the search loop is already defined in the code above, so won't break it down step by step in the pseudo-code, will refer to it as SEARCH LOOP
Pseudo-code #1
START SEARCH LOOP
SEARCH for the desired string(`codemp`) line by line
IF string is found
PRINTLINE containing the STRING
MOVE CURSOR one line below
PRINT the entire line
MOVE CURSOR one line below
PRINT the entire line
MOVE CURSOR one line below
PRINT the entire line
MOVE CURSOR one line below
PRINT the entire line
Move CURSOR one line below
RESTART SEARCH LOOP in the current line
IF NO matching string is found AND EOF
END IF
END SEARCH LOOP
END PROGRAM
Psuedo-code #2
START SEARCH LOOP
SEARCH for the desired string(codemp) line by line
IF string is found
PRINTLINE containing the string
PRINTING LOOP
MOVE CURSOR one line below
ASSIGN line to a temp_string
IF temp_string = "\n\n"
//each employee is separated by 2 whitelines after its info
DO NOTHING
ELSE
PRINTLINE temp_string
RESTART PRINTING LOOP
END IF
END PRINTING LOOP
RESTART SEARCH LOOP
IF NO string is found AND EOF
END SEARCH LOOP
END PROGRAM
I hope this is clear and detailed enough. That's what I want to do with my program, but I can't find a way to translate it into c++ sentences. Please let me know if there's a better, or more efficient way to do what I want to do.
I didn't test it but that should work :
while( getline(myfile,line) )
{
if( line.find( codemp ) != string::npos )
{
cout << line << endl;
for( int i = 0; i < nbLines && getline( myfile, line ); ++i )
cout << line << endl;
}
}
It iterate the file with a while and when it find the string it print "nbLines" other lines"
Since what you have is working for you, let's look at that first. Here I've xtracted just the relevant portion, with a small addition. All we need to do is print a four more lines in the case that the code was found, so:
while (getline(myfile, line)) {
if (line.find(codemp) != string::npos) {
cout<<line<<endl;
getline(myfile,line);
found = true;
for (int i=4; i; --i) { <== new code begins
cout << line << '\n';
getline(myfile, line);
} <== new code ends
}
}
Simple as that.

c++ while loops, using getline function

Ok so to start because people like to know, this is homework....
I am writing a C++ Program to convert a text file to XML (this is not where I need help)
I am having this issue.
I am writing a while loop to cycle through the companies in the txt file (up to 15) and at the end of each group it is marked with the words --END_MANAGER_DATA--. I am trying to write a while loop that will loop through the company's and finish looping when it reaches --END_MANAGER_DATA--. below is an example of the txt file
19936 WALKER KOLTON PORTLAND TN
HARMAN INTERNATIONAL INDUSTRIES INC
LUCENT TECHNOLOGIES INC
COMPUTER SCIENCES CORP
COMMUNICATIONS CORPORATION
--END_MANAGER_DATA--
this is the code I am trying to use.....
getline(inFile,company);
inFile.ignore();
while(company != "--END_MANAGER_DATA--"|| !inFile.eof())
{
outputfile <<"\t\t\t <company> "<<company << "</company>"<<endl;
getline(inFile,company);
inFile.ignore();
}
this is not working....it just stays in the loop.... can someone offer advice as to a route I can take. I am not asking for you to finish my homework....just need a nudge in the right direction
The idiomatic way to read data is to put the std::getline() into the while loop's test condition rather like this:
std::string line;
while(std::getline(inFile, line) && line != "--END_MANAGER_DATA--")
{
std::cout << "\t\t\t<company>" << line << "</company>" << '\n';
}
This works because the while will only proceed if there were no errors reading the line. Testing for eof() doesn't work because eof() doesn't happen until after the read has taken place and the read will only take place if no error condition already exists.
In your code you ignore() the first character of each line which is why, I think, you can never match "--END_MANAGER_DATA--".
Try this:
string company = "";
while(company.compare("--END_MANAGER_DATA--") != 0 && !inFile.eof()) {
getline(inFile, company);
outFile << "\t\t\t <company> "<< company << "</company>" << endl;
inFile.ignore();
}

Logic for reading rows and columns from a text file (textparser) C++

I'm really stuck with this problem I'm having for reading rows and columns from a text file. We're using text files that our prof gave us. I have the functionality running so when the user in puts "numrows (file)" the number of rows in that file prints out.
However, every time I enter the text files, it's giving me 19 for both. The first text file only has 4 rows and the other one has 7. I know my logic is wrong, but I have no idea how to fix it.
Here's what I have for the numrows function:
int numrows(string line) {
ifstream ifs;
int i;
int row = 0;
int array [10] = {0};
while (ifs.good()) {
while (getline(ifs, line)) {
istringstream stream(line);
row = 0;
while(stream >>i) {
array[row] = i;
row++;
}
}
}
}
and here's the numcols:
int numcols(string line) {
int col = 0;
int i;
int arrayA[10] = {0};
ifstream ifs;
while (ifs.good()) {
istringstream streamA(line);
col = 0;
while (streamA >>i){
arrayA[col] = i;
col++;
}
}
}
edit: #chris yes, I wasn't sure what value to return as well. Here's my main:
int main() {
string fname, line;
ifstream ifs;
cout << "---- Enter a file name : ";
while (getline(cin, fname)) { // Ctrl-Z/D to quit!
// tries to open the file whose name is in string fname
ifs.open(fname.c_str());
if(fname.substr(0,8)=="numrows ") {
line.clear();
for (int i = 8; i<fname.length(); i++) {
line = line+fname[i];
}
cout << numrows (line) << endl;
ifs.close();
}
}
return 0;
}
This problem can be more easily solved by opening the text file as an ifstream, and then using std::get to process your input.
You can try for comparison against '\n' as the end of line character, and implement a pair of counters, one for columns on a line, the other for lines.
If you have variable length columns, you might want to store the values of (numColumns in a line) in a std::vector<int>, using myVector.push_back(numColumns) or similar.
Both links are to the cplusplus.com/reference section, which can provide a large amount of information about C++ and the STL.
Edited-in overview of possible workflow
You want one program, which will take a filename, and an 'operation', in this case "numrows" or "numcols". As such, your first steps are to find out the filename, and operation.
Your current implementation of this (in your question, after editing) won't work. Using cin should however be fine. Place this earlier in your main(), before opening a file.
Use substr like you have, or alternatively, search for a space character. Assume that the input after this is your filename, and the input in the first section is your operation. Store these values.
After this, try to open your file. If the file opens successfully, continue. If it won't open, then complain to the user for a bad input, and go back to the beginning, and ask again.
Once you have your file successfully open, check which type of calculation you want to run. Counting a number of rows is fairly easy - you can go through the file one character at a time, and count the number that are equal to '\n', the line-end character. Some files might use carriage-returns, line-feeds, etc - these have different characters, but are both a) unlikely to be what you have and b) easily looked up!
A number of columns is more complicated, because your rows might not all have the same number of columns. If your input is 1 25 21 abs 3k, do you want the value to be 5? If so, you can count the number of space characters on the line and add one. If instead, you want a value of 14 (each character and each space), then just count the characters based on the number of times you call get() before reaching a '\n' character. The use of a vector as explained below to store these values might be of interest.
Having calculated these two values (or value and set of values), you can output based on the value of your 'operation' variable. For example,
if (storedOperationName == "numcols") {
cout<< "The number of values in each column is " << numColsVal << endl;
}
If you have a vector of column values, you could output all of them, using
for (int pos = 0; pos < numColsVal.size(); pos++) {
cout<< numColsVal[pos] << " ";
}
Following all of this, you can return a value from your main() of 0, or you can just end the program (C++ now considers no return value from main to a be a return of 0), or you can ask for another filename, and repeat until some other method is used to end the program.
Further details
std::get() with no arguments will return the next character of an ifstream, using the example code format
std::ifstream myFileStream;
myFileStream.open("myFileName.txt");
nextCharacter = myFileStream.get(); // You should, before this, implement a loop.
// A possible loop condition might include something like `while myFileStream.good()`
// See the linked page on std::get()
if (nextCharacter == '\n')
{ // You have a line break here }
You could use this type of structure, along with a pair of counters as described earlier, to count the number of characters on a line, and the number of lines before the EOF (end of file).
If you want to store the number of characters on a line, for each line, you could use
std::vector<int> charPerLine;
int numberOfCharactersOnThisLine = 0;
while (...)
{
numberOfCharactersOnThisLine = 0
// Other parts of the loop here, including a numberOfCharactersOnThisLine++; statement
if (endOfLineCondition)
{
charPerLine.push_back(numberOfCharactersOnThisLine); // This stores the value in the vector
}
}
You should #include <vector> and either specific std:: before, or use a using namespace std; statement near the top. People will advise against using namespaces like this, but it can be convenient (which is also a good reason to avoid it, sort of!)

Reading BSDF data format

I have been required to write a function that reads the BSDF data format defined by Zemax
An example of such file can be found at the following page: BSDF file example
I would like to use, if possible, only standard ifstream functions.
I have already prepared all the necessary datamembers inside a dedicated class.
I am now trying to write the function that reads the data from the file.
Problems:
how do I exclude comment lines? as documented, they start with an hash # I was going for something like
void ReadBSDFFile(myclass &object)
{
ifstream infile;
infile.open(object.BRDFfilename);
char c;
infile.get(c);
while (c == "#") // Problem, apparently I cannot compare in this way. How should I do it?
{
getline(infile, line);
infile.get(c);
}
// at this point I would like to go back one character (because I do not want to lose the non-hash character that ended up in *c*)
infile.seekg(-1, ios_base::cur);
// Do all the rest
infile.close();
}
in a similar way, I would like to verify that I am at the correct line later on (e.g. the "AngleOfIncidence" line). Could I do it in this way?
string AngleInc;
infile >> AngleInc;
if (AngleInc != "AngleOfIncidence")
{
//error
}
Thanks to anyone who will comment/help. Constructive criticism is welcomed.
Federico
EDIT:
Thanks to Joachim Pileborg below, I managed to proceed up to the data blocks part of the file.
Now I have the following problem. When reaching the datablocks, I wrote the following piece of code, but at the second iteration (i = 1) i receive the error message for the TIS line.
Could someone help me understand why this does not work?
Thanks
Note: blocks is the number on the AngleOfIncidence line, rows the one on the ScatterAzimuth line and columns the one on the ScatterRadial. I tested and verified that this part of the function works as desired.
// now reading the data blocks.
for (int i=0; i<blocks; i++)
{
// TIS line
getline(infile, line);
if (line.find("TIS") == string::npos)
{
// if not, error message
}
// Data block
for (int j=0; j<rows; j++)
{
for (int k=0; k<columns; k++)
{
infile >> object.BRDFData[i][j][k];
}
}
}
EDIT 2:
solved adding infile.seekg(+2, ios_base::cur); as a last line of the i loop.
The reading loop could be simplified like this:
std::string line;
while (getline(infile, line))
{
if (line[0] != '#')
{
// Not a comment, do something with the line
if (line.find("AngleOfIncidence") != std::string::npos)
{
// On the AngleOfIncidence line, do special things here
}
}
}
It's might not be optimal, just something written at the top of my head, but should work.
From the description of the format you provided:
Any line that starts with the # symbol is ignored as a comment line.
So what you need to do is the following
Read the file line by line
If the line starts with # ignore it
Otherwise process the line.
The while you have used is wrong. Use the getLine function instead and compare its first character with the #.