i'm trying to use string stream to get data from an input file. the program works until it get's to adding the elements to the array.

i'm still not completely sure how to make use of stringstream yet or if i'm using it right.

ifstream ipf;
ipf.open(test.txt);
	
char c;
int size, FSize = 0;
unsigned char** UCarray = new unsigned char* [size];
stringstream str;

while(!ipf.eof())
{
	ipf.get(c);
	if ((c=='\n')||(c==' '))
		FSize++;			
}
size = FSize;
UCarray[size];
		
ipf.seekg (0, ios::beg);

for(int i = 0;i<size;i++)
{	
	str<<ipf;
	str >> UCarray[i];
}

any help will be greatly appreciated.

Recommended Answers

All 11 Replies

I would suggest making a demo program to make sure you understand how stringstream works before using it in your program with user input. If you hard code values and it works as you'd expect, then you know the problem is with the input itself.

I would suggest making a demo program to make sure you understand how stringstream works before using it in your program with user input. If you hard code values and it works as you'd expect, then you know the problem is with the input itself.

i've already done that.

here's how i think i should try to do it

for(int i = 0;i<size;i++)
{
 	str<<ipf;
        str >> temp;
        mpImg[i] = temp;
}

the only problem i'm having is i can't figure out what data type to use as temp

It should be the same type as the elements of mpImg. How is mpImg declared?

It should be the same type as the elements of mpImg. How is mpImg declared?

mpimg is a unsigned char so thanks i'll try that

Don't forget stringstream needs a constructor:
http://www.cplusplus.com/reference/iostream/stringstream/stringstream/
By the way, that site in general is a great API for C++ with examples as in that case.
By the way, note that with C's *scanf or C++'s stringstream, if you ever need to convert a number in scientific notation to a long, which is definitely possible now with 64 bit longs, you need to read in a temporary double variable and then assign it to your long variable. Not an issue with this case, but good to know sooner than later...

the main problem i'm having now is i can't seem to get the temp pointer to initialize.

null or 0 break at runtime and const chars just don't seem to be working

How are you declaring it? Give us some short, compilable code snippets to work with.

Following the link I posted above, here is an example of how to use it that does work:

// using stringstream constructors.
#include <iostream>
#include <sstream>
using namespace std;

int main () {

  int val;
  stringstream ss (stringstream::in | stringstream::out);

  ss << "120 42 377 6 5 2000";

  for (int n=0; n<6; n++)
  {
    ss >> val;
    cout << val*2 << endl;
  }

  return 0;
}

How are you declaring it? Give us some short, compilable code snippets to work with.

unsigned char* temp = 0;

it get's through the compilers check but when i call the function i get an error

Try

unsigned char temp[1]; // or however many characters you expect to get

Or better yet, why not use a std::string instead of a char array?

Try

unsigned char temp[1]; // or however many characters you expect to get

Or better yet, why not use a std::string instead of a char array?

that gave me alot of error code unfortuneatly

the unsigned char array was something i was asked to use so my hands are kinda tied. i just need to get this bit done and link it all together and it's done

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.