I have been searching online for a long time and there is no correct answer as far as I can find. Now the most common answer looks like below:
int main() {
int number_of_lines = 0;
std::string line;
std::ifstream myfile("textexample.txt");
while (std::getline(myfile, line))
++number_of_lines;
std::cout << "Number of lines in text file: " << number_of_lines;
return 0;
}
If the textexample.txt file actually has two empty lines at the end, this program will only count one of them, I'm guessing the first one. Such as below:
1
2
3
4
5
6
The above 6 numbers and 3 empty lines are 9 lines in total, but the program above will return 8.
I don't know why, but it seems std::getline() only loops 8 times.
The file in your example has 10 lines, of which 3 are empty. And if I run your code (with the missing includes...) it tells me there are 10 lines. So either you're running different code, or you're mis-quoting the file. Or you C++ standard library is broken somehow...
If I remove the line with "end", I get 9, not 8, lines.
Related
Is there any difference between the terms 'line of code' and 'statement' in programming languages?
Yes, check following:
int a =0;
while(a<100)
{
cout<<
"Is it ok"<<
a <<
"is the current value of 'a'"<<endl;
a++;
}
Above code snippet is having:
Line of code: 9
Simple Statements: 3
Compound Statements: 1
Line of code is basically how many end points you are using. A Statement is the group of code that you produce to create an expected output.
For example in a conditional statement using if else, you can right that in multiple line of core or using only one line via Ternary method.
Sample:
if($var == 0) {
echo "this is zero";
} else {
echo "not a zero";
}
that basically created 5 line of code. In ternary you can go like this
$var == 0 ? echo "this is zero" : echo "not a zero";
As you see the result is the same only it is created in 1 line of code.
Hope this helps you moving forward
Lines of code presumably refers to lines of content in a source file, i.e. the number of lines in a file. On the other hand, a given statement of code can, and often does, exceed a single line. For example, consider the following Java statement from the Javadoc for streams:
int sum = widgets.stream()
.filter(b -> b.getColor() == RED)
.mapToInt(b -> b.getWeight())
.sum();
One statement spans four physical lines in the editor. However, we could inline the whole thing into a single line.
I have a question related to AMPL. I'm trying to construct a matrix of sets, named A in the following code (part of the .mod file). This gives an error message of "A is already defined."
Please note that S, T are parameters and B is a set in .dat file. (They have already been read by the previous part of the .mod file that I excluded in the following code.)
set A{s in 1..S, t in 1..T} default {};
for {s in 1..S} {
for {t in 1..T} {
/*set A{s,t} default {};*/
for {sprime in 1..S: sprime != s}{
if B[sprime,t] = B[s,t] then {
let A[s,t] := A[s,t] union {sprime};
}
}
}
}
I tried commenting out the first line and uncommenting the 4th line; however, it did not help.
In short, what I'm trying to do is to have an empty A matrix sized SxT and then fill/update each element of that matrix with nested for loops. So, every element of the matrix will contain a set. The sizes of these elements/sets can be different.
I tested the following code and it seems to do what you wanted, without error messages:
reset;
param S := 5;
param T := 3;
model;
param B{1..S,1..T};
data;
param B: 1 2 3 :=
1 1 2 3
2 2 3 2
3 0 0 3
4 1 1 1
5 3 2 3
;
model;
set A{s in 1..S, t in 1..T} default {};
for {s in 1..S}{
for {t in 1..T}{
for {sprime in 1..S: sprime != s}{
if B[sprime,t] = B[s,t] then {
let A[s,t] := A[s,t] union {sprime}};
}
}
}
I haven't significantly modified the part that you posted, just added some definitions so it's self-contained. However, I do have a "reset" at the beginning of the script.
Is it possible that you forgot to clear definitions of A in between runs? If so, then you would get an "A is already defined" error, not because of the LET statements but because of the "set A" statement at the start of your code snippet.
my problem is the following. I have a BIG file with many rows containing ordered numbers (repetitions are possible)
1
1.5
3
3.5
6
6
...
1504054
1504056
I would like to print all the pair of row numbers such that their difference is smaller than a given threshold thr. Let us say for instance thr=2.01, I want
0 1
0 2
1 2
1 3
2 3
4 5
...
N-1 N
I wrote a thing in python but the file is huge and I think I need a smart way to do this in bash.
Actually, in the complete data structure there exists also a second column containing a string:
1 s0
1.5 s1
3 s2
3.5 s3
6 s4
6 s5
...
1504054 sN-1
1504056 sN
and, if easy to do, I would like to write in each row the pair of linked strings, possibly separated by "|":
s0|s1
s0|s2
s1|s2
s1|s3
s2|s3
s4|s5
...
sN-1|sN
Thanks for your help, I am not too familiar with bash
In any language you can white a program implementing this pseudo code:
while read line:
row = line.split(sep)
new_kept_rows = []
for kr in kept_rows :
if abs(kr[0], row[0])<=thr:
print "".join(kr[1:]) "|" "".join(row[1:])
new_kept_rows.append(kr)
kept_rows = new_kept_rows
This program only keep the few lines which could match the condition. All other are freed from memory. So the memory footprint should remain small even for big files.
I would use awk language because I'm comfortable with. But python would fit too (the pseudo code I give is very close to be python).
I am a newbie to SonarQube and trying to use the tool for measuring my product quality.
In some cases, I found that the duplicated lines is reported incorrectly by SonarQube . The number of lines of code is less than the duplicated lines. How can that be ? Either the count of lines of code is incorrect or the count of duplicated lines is incorrect.
Assuming it could be a problem with my code alone, I visited the demo page of Sonarqube https://sonarqube.com/component_measures/domain/Duplications?id=com.adobe%3Aas3corelib
There as well , I found that one of the cases the lines of code is less than duplicated lines.
Where is the issue ? How do I address it ?
What you're seeing is the difference between Lines and Lines of Code. For instance, how many of each are below:
public void foo() {
int i = 0;
for (int j=0; j < 10; j++)
doTheThing(j);
}
I'd say that's 4 LoC (maybe 5. Don't remember if the '}' counts) but 9 Lines.
I have files that can be 19GB or greater, they will be huge but sorted. Can I use the fact that they are sorted to my advantage when searching to see if a certain string exists?
I looked at something called sgrep but not sure if its what I'm looking for. An example is I will have a 19GB text file with millions of rows of
ABCDEFG,1234,Jan 21,stackoverflow
and I want to search just the first column of these millions of row to see if ABCDEFG exists in this huge text file.
Is there a more efficient way then just greping this file for the string and seeing if a result comes. I don't even need the line, I just need almost a boolean, true/false if it is inside this file
Actually sgrep is what I was looking for. The reason I got confused was because structured grep has the same name as sorted grep and I was installing the wrong package. sgrep is amazing
I don't know if there are any utilities that would help you out if the box, but it would be pretty straight forward to write an application specific to your problem. A binary search would work well, and should yield your result within 20-30 queries against the file.
Let's say your lines are never more than 100 characters, and the file is B bytes long.
Do something like this in your favorite language:
sub file_has_line(file, target) {
a = 0
z = file.length
while (a < z) {
m = (a+z)/2
chunk = file.read(m, 200)
// That is, read 200 bytes, starting at m.
line = chunk.split(/\n/)[2]
// split the line on newlines, and keep only the second line.
if line < target
z = m - 1
else
a = m + 1
}
return (line == target)
}
If you're only doing a single lookup, this will dramatically speed up your program. Instead of reading ~20 GB, you'll be reading ~20 KB of data.
You could try to optimize this a bit by extrapolating that "Xerox" is going to be at 98% of the file and starting the midpoint there...but unless your need for optimization is quite extreme, you really won't see much difference. The binary search will get you that close within 4 or 5 passes, anyway.
If you're doing lots of lookups (I just saw your comment that you will be), I would look to pump all that data into a database where you can query at will.
So if you're doing 100,000 lookups, but this is a one-and-done process where having it in a database has no ongoing value, you could take another approach...
Sort your list of targets, to match the sort order of the log file. Then walk through each in parallel. You'll still end up reading the entire 20 GB file, but you'll only have to do it once and then you'll have all your answers. Something like this:
sub file_has_lines(file, target_array) {
target_array = target_array.sort
target = ''
hits = []
do {
if line < target
line = file.readln()
elsif line > target
target = target_array.pop()
elseif line == target
hits.push(line)
line = file.readln()
} while not file.eof()
return hits
}