-
keep printing lst char
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char * argv[])
{
if(argc<2)
{
cout<<"The correct format is file <string>"<<endl;
return 0;
}
cout<<"correct number of parimeters."<<endl;
cout<<"You have "<<argc<<" parimeter"<<endl;
for(int x=0;x<argc;x++)
{
cout<<argv[x]<<endl;
}
if(argc>2)
{
if(argv[1]="open")
{
ifstream a_file;
a_file.open(argv[2]);
char x;
a_file.get(x);
while(x!=EOF)
{
cout<<x;
a_file.get(x);
}
a_file.close();
}
}
return 0;
}
all of the code works, apart from the fact that it keeps printing out the last character in the text file.
-
AFAIK ifstream::get() never retrieves the EOF character. Use ifstream::eof() to test for the end-of-file condition instead.
Code:
a_file.open(argv[2]);
char x;
a_file.get(x);
while(!a_file.eof())
{
cout<<x;
a_file.get(x);
}
a_file.close();
-
This might eat the last character of the file, maybe you have to reorder the code.
-
thanks its working great
btw, it didnt eat the last character.
Thanks :)