File I/O questions
Using MS Visual C++ 6.0.
Command line project.
I'm trying to write a project (as part of an assignmet) that reads all text from a .txt document and breaks each word down into seperate string segments.
I'm stuck at the part where I'm reading the file. I'm using ifstream for the file input. I am reading the file just fine using filename_variable.get();
I'm using a while loop, and pushing the characters I get from the file to a vector.
The Problem: The problem I'm having is that I can't get the while loop to recognize eof on the file.
Can anyone give me some ideas here?
[657 byte] By [
jedispy] at [2007-11-20 4:34:25]

# 1 Re: File I/O questions
Normally, it would help to post your code ...
But here is one way to loop thru the file using get() ... (not really
the way I would recommend, but ...)
ifstream in("your_file");
char c;
while (in.get(c))
{
cout << c;
}
# 2 Re: File I/O questions
My mistake, I forgot to post my code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
const char dictionary[14] = { 'a', 's', 'c', 'i', 'i', '_', 'd', 'i', 'c', 't', '.', 't', 'x', 't' };
ifstream dictInput;
vector <char> dictVect(0);
char n;
int main()
{
dictInput.open(dictionary);
if (dictInput.is_open())
{
while ( !ios::eof() )
{
if (!ios::eof())
{
n = dictInput.get();
cout << n;
dictVect.push_back(n);
}
else
// Do nothing
}
cout << endl;
}
return (0);
}
As it is right now, this can't compile. I'm getting error:
project.cpp(207) : error C2352: 'std::ios_base::eof' : illegal call of non-static member function
The filename is ascii_dict.txt, and it reads it well. My main problem is that I don't know how to get it to find end of file on ascii_dict.txt.
# 3 Re: File I/O questions
Firs of all I'd recommend this (or using a string which I reckon is better)
#include <fstream>
const char dictionary[] = "ascii_dict.txt";
dictInput.open(dictionary);
char n;
while (dictInput.good()) // loop while extraction from file is possible
{
n = dictInput.get(); // get character from file
cout << n;
dictVect.push_back(n);
}
// dont forget to close file
dictInput.close();