reading the last char twice

I am not going to type in all my code, but when I run the program it reads the last char twice.How can I fix it thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void main()
{
	ifstream fin;
    ofstream fout;
	string temp;
	char* pch;
	char* array[100];
	int count= 0;
	fin.open("tvshows.dat");
    if(fin.fail())  {
        cout << "Could not open" << endl;
        exit(1);
    }
    while(!fin.eof())
    {
		fin>>temp;
		pch = (char *) malloc(sizeof(char) * temp.size()); 
		strcpy(pch, temp.c_str());
		array[count++] = pch;
	}
You are probably getting an EOF when you call fin >> temp; Therefore, your code plows ahead with what it read last time. Try also checking for eof after that line and then break if it's true.
1
2
3
4
5
6
7
8
while(!fin.eof())
    {
                break();
		fin>>temp;
		pch = (char *) malloc(sizeof(char) * temp.size()); 
		strcpy(pch, temp.c_str());
		array[count++] = pch;
	}


Did you mean this because this didnt work?
No, I meant:
1
2
3
4
5
6
7
8
9
10
11
12

while( ! fin.eof() )
    {
    fin >> temp;

    if( fin.eof() )
        break;

    /*   (The rest as you have it)    */

    }    /*    while( ! fin.eof() )    */
Thank you so much appreciate the help
You're welcome! Please mark this as fixed if this worked for you.
Topic archived. No new replies allowed.