I'm trying to bind the following grid to a symbol
(def grid [08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48])
This yields Exception in thread "main" java.lang.NumberFormatException: Invalid number: 08 (11.clj:1). Why can't I do this in Clojure? Are there any workarounds?
Clarification
All I want to do is paste this grid somewhere and have it act as if there were no leading zeros, even if it takes a little coercion. I don't want to have to drop all of the zeros in my editor, I'd just like to paste it in there and have each number behave as if there were no leading zeros.
One other strange detail
The REPL seems to allow zero-padded numbers, but executing a .clj file with java -cp clojure.jar -i some_file.clj will throw the error.
Leading zeros imply an octal number, so 08 is not valid. Many programming languages use this convention, starting with C.
SPOILER ALERT:
Since you're solving a Project Euler problem, you might not want to read this, even though it's only about the "how to read in the data?" part of it...
The reason this happens is as explained in the other answers. The correct solution would be to embed the input in your code as a string -- with linebreaks! -- and use something like the following:
(->> the-string
(.split #"\n")
(map #(.split #"\s+" %))
(map (partial drop-while empty?))
;; this just doesn't care about the leading 0
(mapcat (partial map #(Integer/parseInt %)))
vec)
This should produce a vector of your numbers. For a two-dimentional vector, you could replace the mapcat with a regular map and put in an extra (map vec) before the final vec.
If you prefer to put the input in a separate file and have Clojure read it from there, replace the-string and (.split #"\n") with a call to line-seq on a reader on your file.x
numbers with a leading 0 are read as if they where in base 8 so any charcter not between 0-7 will not work. to fix this you can append 10r08 to explicity specify the base.
user> 10r08
8
user> 08
; Evaluation aborted.
This messes up your nice formatting though :( sorry about that. you could write a little macro to change this for a block if you want to preserve your nicely formatted code.
Regular expressions will remove leading zeros
(re-seq #"[1-9]+[0-9]*|0{2}" the-string)
The regex phrase breaks down as follows:
[1-9]+ ;; one or more repetitions of 1-9 (i.e. must start with 1-9)
[0-9]* ;; zeros are ok after the first non-zero number has been found
|0{2} ;; or if the above can't be found, just look for two zeros
A more general expression is
#"[1-9]+[0-9]*|(?<=\s)0+(?=\s)"
which does the same thing but in the 'or' portion it uses positive lookahead and lookbehind assertions to look for a sequence of one or more zeros preceded and followed by whitespace.
With the leading zeros stripped (map read-string (re-seq ....)) works just fine
Since it only took about 3 minutes to remove all leading zeroes, I'll just paste the above vector with the zeroes removed in case anyone else wants to copy/paste the euler problem.
(def grid [ 8 2 22 97 38 15 0 40 0 75 4 5 7 78 52 12 50 77 91 8
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 4 56 62 0
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 3 49 13 36 65
52 70 95 23 4 60 11 42 69 24 68 56 1 32 56 71 37 2 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 3 45 2 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 8 40 91 66 49 94 21
24 55 58 5 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 9 75 0 76 44 20 45 35 14 0 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 3 80 4 62 16 14 9 53 56 92
16 39 5 42 96 35 31 47 55 58 88 24 0 17 54 24 36 29 85 57
86 56 0 48 35 71 89 7 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 5 94 47 69 28 73 92 13 86 52 17 77 4 89 55 40
4 52 8 83 97 35 99 16 7 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 3 46 33 67 46 55 12 32 63 93 53 69
4 42 16 73 38 25 39 11 24 94 72 18 8 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 4 36 16
20 73 35 29 78 31 90 1 74 31 49 71 48 86 81 16 23 57 5 54
1 70 54 71 83 51 54 69 16 92 33 48 61 43 52 1 89 19 67 48])
Related
I don't know why it is giving the wrong output(appending all remaining numbers in the string at last)can any pls help me out!!
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{string str="08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00\
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65\
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91\
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80\
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50\
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70\
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21\
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72\
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95\
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92\
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57\
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58\
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40\
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66\
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69\
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36\
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16\
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54\
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48";
int cnt=0;
for(int i=0;str[i];i++)
{
if(str[i]!=' ')
{
str[cnt++]=str[i];
}
}
str[cnt] = '\0';
cout<<str<<endl;
}
This is how it is displaying as output
Thank you
Strings can be split (with any whitespace in between) "like this" -> "like" " " "this".
I.e. (guessing a little a the desired outcome):
string str=
"08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 "
"49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 "
"81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 "
"52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 "
"22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 "
"24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 "
"32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 "
"67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 "
"24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 "
"21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 "
"78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 "
"16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 "
"86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 "
"19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 "
"04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 "
"88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 "
"04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 "
"20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 "
"20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 "
"01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 ";
No need for the line continuation here \<newline>, more of an obstacle.
Concerning the unexpected output of the rest of the string at the end:
Overwriting a letter with the '\0' does not have the terminating effect in a c++-string as it has in most character sequence handling functions in C.
As Botje mentions in the comments:
You overwrite 2/3 of the string, but what do you do with the remaining 1/3? You should truncate the string by either taking a substring or calling resize.
I.e. that is the way to change the effective length of a C++ string.
Not sure why you're trying to change str variable in place. Why don't you use a temporary string variable to do that?
Try this:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
string str="08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 \
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00\
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65\
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91\
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80\
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50\
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70\
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21\
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72\
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95\
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92\
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57\
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58\
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40\
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66\
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69\
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36\
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16\
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54\
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48";
string tmp = "";
for(char& c : str) if(c != ' ') tmp += c;
str = tmp;
cout << str << endl;
}
I am trying to read a .txt file line by line:
vector<vector<int>> iVecGrid;
vector<int> iVecRow;
ifstream text;
text.open("grid.txt", ios::in);
if (!text)
{
cout << "Error!\n";
return EXIT_FAILURE;
}
istringstream isLine;
string sLine;
while (getline(text, sLine))
{
isLine.str(sLine);
int iNumber;
while (isLine >> iNumber)
{
cout << iNumber << " ";
iVecRow.push_back(iNumber);
}
cout << endl;
iVecGrid.push_back(iVecRow);
iVecRow.clear();
}
When I put isLine inside the while (getline(text, sLine)) loop, it works fine.
But when I put it outside, it bugged out for some reason.
.txt file:
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
When you are done with the following loop
while (isLine >> iNumber)
{
cout << iNumber << " ";
iVecRow.push_back(iNumber);
}
isLine is in a state of error. The error state needs to cleared. Add the following line after the loop.
isLine.clear();
Also, the position of isLine needs to be reset to point to the start of the string. You can use the following line for that.
isLine.seekg(0);
Both of these problems are avoided by moving the scope of isLine to inside the while block.
The following should work.
while (getline(text, sLine))
{
isLine.str(sLine);
isLine.seekg(0); //ADD. Start reading from position 0.
int iNumber;
while (isLine >> iNumber)
{
cout << iNumber << " ";
iVecRow.push_back(iNumber);
}
isLine.clear(); //ADD. Clear the error state.
cout << endl;
iVecGrid.push_back(iVecRow);
iVecRow.clear();
}
However, I would recommend sticking to your first approach. It removes the unnecessary clutter from your code.
while (getline(text, sLine))
{
std::istringstream isLine(sLine);
int iNumber;
while (isLine >> iNumber)
{
cout << iNumber << " ";
iVecRow.push_back(iNumber);
}
cout << endl;
iVecGrid.push_back(iVecRow);
iVecRow.clear();
}
When you do
while (isLine >> iNumber)
{
cout << iNumber << " ";
iVecRow.push_back(iNumber);
}
the loop runs until isLine enters a failed state. Once it enters that failed state, you can no longer read from it until you call the clear member function to clear those errors. This means that when isLine is declare outside of the while loop, the first iteration of the loop puts it into an error state, and it stays that way for each subsequent iteration since you do not manually clear the errors.
On the other hand, when isLine is declared inside the while loop, it is destroyed at the end of the loop and created again at the start of the next iteration. This process gives you a new stream that is not in a error state so you can use it as expected.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
im trying to fill an array with random 200 numbers that can very from 0-100. I get it populated except the last couple number are very odd.
here my code.
for (int i = 0; i < NUM_LIST_ELEMENTS; i++)
{
int j = rand() % 100;
list[i] = j;
}
my output comes out at follows
Original Arrays:
41 67 34 0 69 24 78 58 62 64 5 45 81 27 61 91 95 42 27 36 91 4 2 53 92 82 21 16 18
95 47 26 71 38 69 12 67 99 35 94 3 11 22 33 73 64 41 11 53 68 47 44 62 57 37 59 23 41
29 78 16 35 90 42 88 6 40 42 64 48 46 5 90 29 70 50 6 1 93 48 29 23 84 54 56 40 66
76 31 8 44 39 26 23 37 38 18 82 29 41 33 15 39 58 4 30 77 6 73 86 21 45 24 72 70 29
77 73 97 12 86 90 61 36 55 67 55 74 31 52 50 50 41 24 66 30 7 91 7 37 57 87 53 83 45
9 9 58 21 88 22 46 6 30 13 68 0 91 62 55 10 59 24 37 48 83 95 41 2 50 91 36 74 20
96 21 48 99 68 84 81 34 53 99 18 38 0 88 27 67 28 93 48 83 7 21 10 17 13 14-858993460
9 16 35 51 0 49 19 56 98 3 24 8 44 9 89 2 95 85 93 43 23 87 14 3 48 0 58 18 80
96 98 81 89 98 9 57 72 22 38 92 38 79 90 57 58 91 15 88 56 11 2 34 72 55 28 46 62 86
75 33 69 42 44 16 81 98 22 51 21 99 57 76 92 89 75 12 0 10 3 69 61 88 1 89 55 23 2
85 82 85 88 26 17 57 32 32 69 54 21 89 76 29 68 92 25 55 34 49 41 12 45 60 18 53 39 23
79 96 87 29 49 37 66 49 93 95 97 16 86 5 88 82 55 34 14 1 16 71 86 63 13 55 85 53 12
8 32 45 13 56 21 58 46 82 81 44 96 22 29 61 35 50 73 66 44 59 92 39 53 24 54 10 45 49
86 13 74 22 68 18 87 5 58 91 2 25 77 14 14 24 34 74 72 59 33 70 87 97 18 77-33686019
notice that last number in each array is really weird. Is there anything I can do to avoid this? btw this is two different arrays.
Thanks everyone that posted! I got it working!
You are reading one beyond the end of the array.
e.g. if you populate an array with 200 elements, you should write to and read from 0 to 199 not 0 to 200 or 1 to 200.
By the way - rand() % 100 will not make numbers from 0 to 100. It will make numbers from 0 to 99 only.
Also, as Randy Howard says (thanks), you can get a more even random generation by following the advice at http://www.azillionmonkeys.com/qed/random.html .
This is probably because there is something wrong with your code that prints the result. You might be looping from index 0 to 200, which has 201 items.
I counted your outputs and found there is 201 items, if the last 77-33686019 are actually 2 separate numbers.
If it's not that, you might have some printf/cout somewhere further down your code that actually prints some other value. To confirm this you can probably try printf ("\n"); right after your loop that outputs the array. If your negative number ends up on a different line, you'll know it's some other printf further down your code.
You might want to use int j = rand() % 101; instead so that you get 0 to 100. Your original code gives you the random range from 0 to 99.
I was wondering if there was a way to construct a grid or 2D array with predetermined numbers. Specifically, problem 11 of project Euler presents the following 20x20 grid:
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
And I'm trying to recreate it in code.
Thank you!
Sure:
int a[3][3] = { { 1, 2, 3},
{ 4, 5, 6},
{ 7, 8, 9} };
You're actually allowed to flatten the brace initializer out into a single run of numbers, but it's nice to be clear. In C99 and C++11 you can also have extraneous trailing commas.
This is the text of my program:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
int main(){
string line;
ifstream inf("grid.txt");
while(!inf.eof()){
getline(inf, line);
cout << line;
}
return 0;
}
(I'll be using sstream later)
This is the contents of grid.txt:
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
I'm compiling this under Cygwin and g++ and here's what I get:
$ g++ program.cpp
$ ./a.exe
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
To save you looking - that's the last line of the file. If I replace the loop with:
getline(inf, line);
cout << line;
getline(inf, line);
cout << line;
it will display not the first, but the second line of the file. It's been a while since I've last programmed in C++ but I'm 90% sure it's supposed to display more than one line there...
Try adding a std::endl, which will automatically append a newline and flush the buffer.
You can also use the istream& getline ( istream& is, string& str, char delim ); signature to specify another delimiter than the default which is newline in case your file doesn't have any.
What file format is your grid.txt? It's likely that your lines end with a carriage return \r
Just as I finished asking my question I tried:
cout << "hi";
before changing it to the two couts. When I got two extra characters, that's when I realized I was getting the \r character. (Can you tell I've ran into problems with the carriage return before? :P )
You're not emitting a newline at the end of the lines, so they're overwriting each other.
Change the output line to
cout << line << endl;
You're reading binary data using getline. Try using read() instead.