WillFerrellLuva
March 25th, 2010, 04:15 AM
my objective here is to read a file and store it in a string. I always seem to be getting an extra character in my string for some reason:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
fstream in;
char char_temp = ' ';
int int_temp = 0;
int int_count[10] = {0};
int char_count[26] = {0};
string contents = "";
in.open("test");
if(in.good())
{
while(!in.eof())
{
in>>char_temp;
contents += char_temp;
}
}
else
{
cout << "Error opening file." << endl;
}
in.close();
//contents.erase(contents.size()-1);
for(unsigned int i = 0; i < contents.size(); i++)
{
cout << contents[i] << endl;
}
I can just erase the last char but this solution bothers me. Anybody know what I am doing wrong here? Any help will be greatly appreciated.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
fstream in;
char char_temp = ' ';
int int_temp = 0;
int int_count[10] = {0};
int char_count[26] = {0};
string contents = "";
in.open("test");
if(in.good())
{
while(!in.eof())
{
in>>char_temp;
contents += char_temp;
}
}
else
{
cout << "Error opening file." << endl;
}
in.close();
//contents.erase(contents.size()-1);
for(unsigned int i = 0; i < contents.size(); i++)
{
cout << contents[i] << endl;
}
I can just erase the last char but this solution bothers me. Anybody know what I am doing wrong here? Any help will be greatly appreciated.