Hi all, i asked this question a few days ago bit its still unsolved.

I'm reading a text file of strings, line by line, and storing them into an array.

when i try and print them using this function:

template < typename T >
void printArray(T * const array, int size)
{
    for (int i=0; i < size; ++i)
    {
        cout << array[i] << ' ';
        cout.flush();
    }
    cout << endl;

}//printarray

it dosn't print the whole array.
when i use endl or '\n' instead of ' ' it works.
and it can't be the cout buffer because im using cout.flush().
Does anyone know what is happening.

Recommended Answers

All 4 Replies

You do not have to flush cout. Can you show us some more code, because this should work fine as far as I can see.

[edit]
I tested it just to be sure and it works fine for me:

template < typename T >
void printArray(T * const array, int size)
{
    for (int i=0; i < size; ++i)
    {
        cout << array[i] << ' ';
    }
    cout << endl;
}//printarray

int main()
{
    string str[3];
    str[0] = "abc";
    str[1] = "cde";
    str[2] = "def";
    printArray(str,3);
    return 0;
}

output: abc cde def

yeah i pretty sure its the reading from file, cause i tried your way as well. heres the reading from file code:

void processFile_strings(const char * fileName, int maxData, int &linecount, string *strptr)
{
     string str;//for reading each line
     linecount = 0;
     ifstream inFile(fileName);//open file

     if (inFile.fail()){
        cout << fileName << " " << strerror(errno) << endl;
        exit (1);
     }
     else
     {

          while(true)
	   {
                if(linecount > maxData)break;
                getline(inFile, str);
		if (!commentLine((char*)str.c_str()))
		{
                    *strptr = str;
                    linecount++;
                    *strptr++;
                }
                if(inFile.eof())break;
	    }
    }
    inFile.close();//close file
}//processFile_strings
bool commentLine(char *s)
{
   char *p=strchr(s,'#');

   if (p) *p = '\0'; // "omit" the comment
   for (p=s; isspace(*p); ++p) // skip over all whitespace
    ;

   return !*p; // OR MORE clearly as   *p != '\0'
}
Member Avatar for jencas

Have you ever stepped thru commentLine() in the debugger?? I think this crap does not do what you want! And by the way, what about passing your string by reference to commentLine() and then use iterators???

commented: >>"I think this crap does not do what you want!" Very well put :) +14

Yes I do need to fix commentline(), it was given to me by the teacher, so I assumed it worked. But its not causing the problem, because I got rid of it all together, and its still happening. any other ideas?

By the way, i think I've said this before, but it does work when i put a new line character (/n) at the end of each word.

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.