strstream problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 78
Reputation: nanodano is an unknown quantity at this point 
Solved Threads: 2
nanodano's Avatar
nanodano nanodano is offline Offline
Junior Poster in Training

strstream problem

 
0
  #1
Oct 1st, 2006
Ok, I have a small problem. I'm just trying to get my program to read a single line from a file at a time, and put the results in a vector.

The problem is at the end of each vector. It repeats the last element twice and also adds a 0. I'm not sure whats going on. I don't think the 0 is a newline, because my strstream never receives a '\n'. I also don't know why it repeats the last element. I posted my program plus my input and output at the end. Any help or ideas would be appreciated Thank you.

- Daniel


#include <iostream>
#include <fstream>
#include <strstream>
#include <string>
#include <vector>

void main() {
// To determine if the the list is smaller or equal to 8, calculate how
// many times youd have to cut the list to reach less than/equal to 8 BEFORE
// you go into the mergesort loop.

ifstream in("in.dat");
vector<float>numbers;
float myfloat;
char ch[256];


while (in) {
in.getline(ch, 256, '\n');
numbers.clear();
strstream myss;
myss << ch;

while(myss) {
myss >> myfloat;
numbers.push_back(myfloat);

}
for (int a = 0; a <= numbers.size(); a++)
cout << numbers[a] << ", ";
cout << endl;
}
}


My in.dat file looks like this:

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30

My output looks like this:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 0,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 20, 0,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 30, 0,
30, 22,
30, 22,
Last edited by nanodano; Oct 1st, 2006 at 3:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: strstream problem

 
0
  #2
Oct 1st, 2006
the loop is formatted wrong. Crevat: atof() does not detect errors in the string, but it doesn't really matter if you know the strings are all formatted ok.
  1. while( in.getline(ch, 256, '\n'))
  2. {
  3. numbers.push_back(atof(ch));
  4. }

better yet, use a std::string instead of a char array
  1. std::string line;
  2.  
  3. while( getline(in, line) )
  4. {
  5. numbers.push_back(atof(line.c_str));
  6. }
Last edited by Ancient Dragon; Oct 1st, 2006 at 5:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: strstream problem

 
0
  #3
Oct 1st, 2006
Your code shouldn't even compile. You're missing a using declaration (I assume) and main should return 'int'

Also, you should use std::stringstream instead of strstream (I believe the latter is deprecated)
Last edited by GloriousEremite; Oct 1st, 2006 at 5:06 pm.
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: strstream problem

 
0
  #4
Oct 1st, 2006
Originally Posted by nanodano View Post
    for (int a = 0; a <= numbers.size(); a++)
   cout << numbers[a] << ", ";
I think you are in for a bit of surprise
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 78
Reputation: nanodano is an unknown quantity at this point 
Solved Threads: 2
nanodano's Avatar
nanodano nanodano is offline Offline
Junior Poster in Training

Re: strstream problem

 
0
  #5
Oct 1st, 2006
Thank all of you for your help,
I took all of your suggestions into account and made some changes in my code. Ancient Dragon, I took your advice on the string, which I think is more appropriate since I don't know how long the input may be. The thing is, I'm not trying to pushback the string. I use a string so I can work on one line at a time. Then, I had a thought. In the long run, I'm trying to use a mergesort to sort each line and output on its own line. I think what I'm going to try is something like this:
  1. psuedocode:
  2. vector<vector<float>>inputlines
  3. vector<float>floatvector
  4.  
  5. while (infile) {
  6.  
  7. extract one line into a string
  8. put the string into a strstream
  9. while (mystrstream) {
  10. mystrstream >> myfloat
  11. floatvector.pushback
  12. }
  13. inputlines.pushback(floatvector)
  14. }

Hopefully my psuedo code is psuedoreadable. I was thinking about creating a vector to hold each line of input, and gathering all the input at the beginning of the program. Then in the next procedure merge sort each input line and then output them.

Ok, so, with that unneccessary information, I did run into another problem even with my modified code. At the end of the run, each vector has one too many numbers still. The last number is always repeated. I also printed out the sizeof each vector, and it comes to 11, instead of the 10 inputs on each line. I'm not sure why the last number is being duplicated every time. Should I try a different kind of loop? Maybe a do-while and check the status of the stream at the end of the loop? My in.dat and my output are at the end of the post.
  1. #include <iostream>
  2. #include <fstream>
  3. #include <strstream>
  4. #include <string>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. void main() {
  9.  
  10.  
  11. ifstream in("in.dat");
  12. vector<float>numbers;
  13. float myfloat;
  14. string input;
  15.  
  16.  
  17. while (in) {
  18. getline(in, input);
  19. numbers.clear();
  20. strstream myss;
  21. myss << input;
  22.  
  23. while(myss) {
  24. myss >> myfloat;
  25. numbers.push_back(myfloat);
  26. }
  27.  
  28. for (int a = 0; a < numbers.size(); a++) // outputs one line
  29. cout << numbers[a] << ", ";
  30. cout << endl;
  31. cout << "sizeof numbers: " << numbers.size() << endl;
  32. }
  33. }

OUTPUT:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10,
sizeof numbers: 11
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 20,
sizeof numbers: 11
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 30,
sizeof numbers: 11
30,
sizeof numbers: 1

in.dat:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
Last edited by nanodano; Oct 1st, 2006 at 6:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: strstream problem

 
0
  #6
Oct 1st, 2006
>>I'm not trying to pushback the string. I
If you read my example carefully I didn't push_back the string either.

>>At the end of the run, each vector has one too many numbers still
re-read my previous post -- I already told you that your loop is incorrect and I showed you the correct loop
Last edited by Ancient Dragon; Oct 1st, 2006 at 6:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 78
Reputation: nanodano is an unknown quantity at this point 
Solved Threads: 2
nanodano's Avatar
nanodano nanodano is offline Offline
Junior Poster in Training

Re: strstream problem

 
0
  #7
Oct 1st, 2006
Sorry for misunderstanding it. I got the problems all worked out now. Thank you all. =)
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 78
Reputation: nanodano is an unknown quantity at this point 
Solved Threads: 2
nanodano's Avatar
nanodano nanodano is offline Offline
Junior Poster in Training

Re: strstream problem

 
0
  #8
Oct 1st, 2006
Ok, for anyone interested, this is the entire mergesort program I was working on. It is finished and works(w/ g++). The stringstream issues I had were in the "GetInput" function. You will see how I finally decided to solve the problem.

  1. /* mergesort.cpp | John Daniel Leon | johndleon@gmail.com | 10/01/06
  2.  This program sorts numbers using a merge sort algorithm
  3.  Merge sort: O(nlog(n)) - Divides unsorted list in half and sorts each sublist
  4.  Recieves input from file in.dat - NOTE : End of file does not contain a new line. */
  5.  
  6. #include <iostream>
  7. #include <fstream>
  8. #include <sstream>
  9. #include <string>
  10. #include <vector>
  11.  
  12. using namespace std;
  13.  
  14. void GetInput(vector<float> &numbers, vector< vector<float> > &inputlines);
  15. void mergesort(vector<float> &myvec);
  16. void mergesort(vector<float> & a, vector<float> & tmp, int left, int right);
  17. void merge(vector<float> & vec1, vector<float> & vec2, int leftpos, int rightpos, int rightend);
  18.  
  19. void main()
  20. {
  21. vector<float>numbers;
  22. vector< vector<float> >inputlines;
  23.  
  24. GetInput(numbers, inputlines); // Uses "in.dat" - no new line at EOF
  25.  
  26. for (int q = 0; q < inputlines.size(); q++)
  27. mergesort(inputlines[q]);
  28.  
  29. for (int x = 0; x < inputlines.size(); x++){
  30. for (int a = 0; a < inputlines[x].size(); a++)
  31. cout << inputlines[x][a]; if(a != inputlines[x].size() - 1) cout << ", ";
  32. cout << endl;
  33. }
  34. }
  35.  
  36.  
  37. void GetInput(vector<float> & numbers, vector< vector<float> > & inputlines)
  38. // This function reads "in.dat" file and stores each float number on a line into a float
  39. // vector. Then the float vectors are stored in a vector called inputlines.
  40. // This waay I can gather all the data in one procedure and then close the file
  41. // and move on with the rest of the program.
  42. // NOTE: End of "in.dat" should not contain a newline.
  43. {
  44. ifstream in("in.dat");
  45. if(!in)
  46. cout << "Error reading file: in.dat" << endl;
  47. string input;
  48. float myfloat;
  49.  
  50. while (getline(in, input)) {
  51. numbers.clear();
  52. stringstream myss;
  53. myss << input;
  54. while(myss >> myfloat) {
  55. numbers.push_back( myfloat );
  56. }
  57. inputlines.push_back(numbers);
  58. }
  59. in.close();
  60. }
  61.  
  62.  
  63. void mergesort ( vector<float> &unsortedvec )
  64. // Calls mergesort on the unsorted vector and a temporary
  65. // vector the same size as unsortedvec.
  66. {
  67. vector<float> temp(unsortedvec.size());
  68. mergesort(unsortedvec, temp, 0, unsortedvec.size() - 1);
  69. }
  70.  
  71.  
  72. void mergesort(vector<float> & a, vector<float> & tmp, int left, int right)
  73. // If the size of the vectors is not 1, then it recurses on itself and
  74. // then calls merge.
  75. {
  76. if (left < right)
  77. {
  78. int center = (left + right) / 2;
  79. mergesort(a, tmp, left, center);
  80. mergesort(a, tmp, (center + 1), right);
  81. merge(a, tmp, left, (center + 1), right);
  82. }
  83. }
  84.  
  85.  
  86. void merge(vector<float> & vec1, vector<float> & vec2, int leftpos, int rightpos, int rightend)
  87. // This is the merge routine. It compares the elements in each vector and manipulates the elements
  88. // so that vec1 ends up with the final merged(sorted) values.
  89. {
  90. int leftend = rightpos - 1;
  91. int tmppos = leftpos;
  92. int numelements = (rightend - leftpos + 1);
  93.  
  94. while(leftpos <= leftend && rightpos <= rightend)
  95. if (vec1[leftpos] <= vec1[rightpos])
  96. vec2[tmppos++] = vec1[leftpos++];
  97. else
  98. vec2[tmppos++] = vec1[rightpos++];
  99.  
  100. while(leftpos <= leftend) // Copy rest of first half
  101. vec2[tmppos++] = vec1[leftpos++];
  102.  
  103. while(rightpos <= rightend) // Copy rest of right half
  104. vec2[tmppos++] = vec1[rightpos++];
  105.  
  106. for(int i = 0; i < numelements; i++, rightend--) //Copy vec2 back
  107. vec1[rightend] = vec2[rightend];
  108. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC