currently I'm reading from file line by line from line 1 to line 10.
Now I need to read in every iteration a specific line too which is 15.
So for example:
Iteration 1: read line 1 ... read line 15
Iteration 2: read line 2 ... read line 15
.
.
Iteration 10: read line 10 ... read line 15
END OF LOOP, OPERATION END.
I don't understand how to read that specific line from the same file(I don't care even reading from a different file but I don't think it's possible to read from multiple files)
Use !DATASOURCE_LINE
SET !DATASOURCE_LINE {{!LOOP}}
PROMPT {{!DATASOURCE_LINE}}
SET !DATASOURCE_LINE 15
PROMPT {{!DATASOURCE_LINE}}
To learn more, see example
Related
I have this file below, which is 3 recipes. Each recipe contains a fixed number of header lines and a variable number of ingredient lines (composition).
The last line of the header gives the number of the ingredients in the composition. This line is always the 16th line of the header (in the linked screenshot these are lines 16, 38, 58).
Then comes the composition with the number of lines as defined in line 16 of the header.
Then the last line of each recipe is always 0,
I need to separate the recipes, so I need to find the last lines, which are line 22, 42 68 in the example.
The number of lines for each recipe is:
16 lines of header + the number of the ingredients (integer defined in line 16 of the header + 1 line (the final 0,)
screenshot of the text
I only got this far:
(?:.*?^0\,){1}
https://regex101.com/r/js3leQ/1
But this only finds all the lines with 0,.
Actually it would be good to find once the third 0, line match.
That would already give me the solution I need (since the third 0, is always the last line of a recipe).
To identify the ending row (3rd zero), the following may work, if you were able to only have regex g flag (without m)
0,(?=\n"REC|$)
https://regex101.com/r/ufXYFU/1
Update. According to the comment that "1", always precedes the 0,, the following should probably work (it would work even if m flag was present):
(?<="1",\n)0,
https://regex101.com/r/aJk5zM/1
I have to make a program which checks two txt files and concatenates the lines which start with the same ID. The results must be in a new file The files are in the following format:
ID STRING_UP_TO_30_CHARS
Error checking is not necessary but I have been stressing over this for quite a while now. I'm pretty new to programming in general as I am a freshman in University.
Code that I have right now:
int main()
{
temp();
fstream file1;
fstream file2;
ofstream result;
string line1, line2;
int pos1, pos2;
string id1, id2;
file1.open("first.txt");
file2.open("second.txt");
result.open("resultat.txt");
while(getline(file1, line1))
{
pos1 = line1.find(" "); //id and word are seperated by a space character, so I find the position of this space character
id1 = line1;
id1.erase(id1.begin()+pos1, id1.end()); //id is now a digit but string,
while(getline(file2,line2))
{
pos2 = line2.find(" ");
id2 = line2;
id2.erase(id2.begin()+pos2, id2.end()); //id is digit now
if(id2 == id1)
{
result<<line1<<" "<<line2<<endl;
// line2.erase(line2.begin(), line2.end());
break;
}
}
}
}
I made a function to make a temp file of the 2nd file since I tried deleting lines from the second document to make the code work. Simply ignore the temp() for now.
The problem is that it only creates 2 lines in my result.txt file. I also have to add all the lines which are not paired with any lines from the other txt file as seperate lines in the final file but this is not a problem for me at the moment.
Txt files I'm testing are probably important:
21 para
23 dyta
27 treta
11 katert
12 pest
13 fundit
14 jojo
41 vecant
46 vecanti
12 fifth
21 first
23 second
29 third
11 fourth
13 last
14 nono
91 special
104 specialty
The result i get is:
21 para 21 first
23 dyta 23 second
I have seen people suggesting vectors but I have not learned them at school and I am also not permitted to use any other libraries for my application.
while(getline(file2,line2))
After this inner loop finds the line from the 2nd file that matches the 1st line in the first file, the outer loop runs again to read the 2nd line from the first file.
However, afterwards, this inner loop simply continues reading from the 2nd file from the point where it stopped reading on the previous iteration of the outer loop. It found the first line, and the while loop terminated. Now, it again continues to getline from file2, so it simply continues reading the rest of it.
The Golden Rule Of Computer Programming states: "a computer always does exactly what you tell it to do instead of what you want it to do". Its corollary states: "a computer never does what you never tell it to do".
In this case you did not tell your computer to start at the beginning of the 2nd file, to begin searching it again for the line that matches the 2nd line from the first file. So, your computer never did that.
Your C++ textbook should explain how to use seekg to seek the position where your fstream reads the file contents from. You simply need to explicitly seekg back to the beginning of file2 before the inner while loop, so that it always start reading from the very beginning of the 2nd file.
My text file look something like this:
1 2 3 test//test
4 5 6 dummy//dummy
How can I read this file and only process the numbers present in each line?
Two ways:
By ignoring the rest of the line.
By reading the complete line into a string, put the string in an output string stream, and read only the leading three numbers from the output string stream.
I am wondering if it is possible to put a line back in a file if it has been read in.
Here is a sample of what I am doing:
string templine;
while(templine.at(0)!=">"){
getline(file,templine);
//do some stuff with string
}
So basically I want to keep reading in the file until the first character of the next line is ">". The amount of lines between each ">" is inconsistent. The way I am reading it in now it will read the next line with a ">" but I dont want to actually do anything with that line yet and want to "put it back" in the file so I can read it again later. I am working in C++ VS10
My files look something like
">"2013 11 24 //The date, had to put > in " "
1 2 3 4
5 6 7 8
">"2013 11 25
1 2 3 4
">"2013 11 26 etc...
You can do it like this:
while(file.peek() != '>'){
std::getline(file,templine);
//Do work
}
where file is your ifstream. That way, you will not read the line with > until you need it.
I am writing a program that take a command redirected file as the standard input, so instead of cin getting input from the keyboard, I run my program with ./teamBalancer < inputFile.txt > outputFile.txt.
The problem I'm having is that getline in the while loop reads the first line perfectly and outputs fine, but then it just reads the rest of the file in one line. I have tried everything from stringstreams to just getline and have read so many forums. Why is this happening?
Here is my code:
//USER INPUT--------------------------------------------------------------
while (cin) {
string attackString, defenseString;
getline(cin, attackString, ',');
getline(cin, defenseString);
int attack, defense;
attack = atoi(attackString.c_str());
defense = atoi(defenseString.c_str());
cout<<attack<<" "<<defense<<endl;
}
and the sample input is this:
5,1
9,1
7,4
1,3
1,3
10,4
4,5
7,4
and the corresponding output is...
5 1
9 1
0 0
Both of these are in txt files and the 0 0 for the last output is because the last line is empty? I don't understand why getline can't continue reading the file line by line like it does with the first one.
Thank you so much to anyone who helps and if you need any clarification please let me know! Thank you :)
the Code you presented looks good. I tested it under Windows without any changes and got the following result:
5 1
9 1
7 4
1 3
1 3
10 4
4 5
7 4
0 0
So it looks like your input file encoding is messed up.
Can you please check the line endings of your input txt. Are they all the same and are the EOL chars the default one for your system?
The 0 0 in the last row is that cin still contains something at the end (the EOF char) but it is not able to translate it to the wished numbers, to avoid this you can use this:
while (!cin.eof())
Now you are checking if you are on the end of a file.
Sorry currently i can not write comments.
br,
zinnjonas