Apr 20, 2010 at 12:37am UTC
basically im trying to read characters from a file and store them in my linked list...but my code doesnt read white spaces...when i print the list theres only characters and no spaces
heres the code...
#include <iostream>
#include <iomanip>
#include <fstream>
#include <list>
#include <stdlib.h>
#include <string>
using namespace std;
void readFile(list<char> &tempList,const string &file,int &count)
{
ifstream fin;
char temp;
list<char>::iterator itr= tempList.begin();
fin.open(file.c_str());
if(!fin){
cout << "Can't open" << file.c_str() << "\n";
exit(0);
}
while(!fin.eof()){
fin >> temp;
if(fin.good()){
tempList.push_back(temp);
if(temp == ' ')
count++;
}
else if(!fin.eof()){
cout << "Can't read from" << file.c_str() << "\n";
exit(0);
}
}
fin.close();
}
void printFile(list<char> &templist)
{
list<char>::iterator itr= templist.begin();
while(itr != templist.end()){
cout << *itr;
itr++;
}
}
int main(int argc,char* argv[])
{
list<char> maze1;
int count=0;
if (argc != 2) {
cout << "Doesn't take 1 argument";
exit(0);
}
readFile(maze1,argv[1],count);
printFile(maze1);
cout << "\n" << count;
return 0;
}
thanks
Raza
Apr 20, 2010 at 1:01am UTC
Please use [ code] tags.
The skipws flag is on by default. You need to turn it off.
fin >> noskipws;
Hope this helps.
Last edited on Apr 20, 2010 at 1:02am UTC