Results 1 to 4 of 4

Thread: keep printing lst char

  1. #1

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    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.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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();
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    This might eat the last character of the file, maybe you have to reorder the code.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    thanks its working great

    btw, it didnt eat the last character.

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width