0

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.

3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by Danny_501
0

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

0

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'
}
1

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???

Votes + Comments
>>"I think this crap does not do what you want!" Very well put :)
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.