Thread: Input from file instead of cin!!!

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    45

    Arrow Input from file instead of cin!!!

    Hello I'm here again for help.
    Is anyone able to tell me quite specifically what this code does??
    Any help is appreciated!!

    Code:
    void printMimeStructure(MimeEntity* pMe, int tabcount = 0)
    {
    	Header& h = pMe->header();                   
    	for(int c = tabcount; c > 0; --c)            
    		cout << "    ";							
    	cout << h.from() << endl;
    
    	MimeEntityList& parts = pMe->body().parts(); 
    
    	// cycle on sub entities list and print info of every item
    	MimeEntityList::iterator mbit = parts.begin(), meit = parts.end();
    	for(; mbit != meit; ++mbit)
    		printMimeStructure(*mbit, 1 + tabcount);
    }
    I think that it parses a mail file and print the FROM, am I right??
    Cause I'm trying to pass to this function a file, that I open in main(), using this:

    Code:
    //my posted code
            ifstream is;
    	is.open ("text.txt", ios::in );
    
    	is.seekg (0, ios::end);
    	len= is.tellg();
    	is.seekg (0, ios::beg);
    
    	buff= new char [len];
    
    	is.read (buff,len);
    
    	istreambuf_iterator<char> test1(is), test2;
    	int VarTest = imTestP | imTestE;
    	MimeEntity me(test1, test2, VarTest );
            printMimeStructure(&me);
    But it doesn't parse anything!
    In fact instead of printing the From field it prints '/'.
    Something wrong in my posted code??

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    First, you're wrong. Printing the from() field is probably part of what it does, but not everything: it also prints information on all the MIME parts of the message.

    Second, doing what you're doing with tellg is undefined behaviour on text files.

    Third, you're reading the entire file into a buffer, but then do not use the buffer anymore. Instead, you create the MimeEntity by passing a pair of input stream iterators. However, since the stream is at the end from the big read, this effectively passes an empty area to MimeEntity.

    Try removing everything related to your buffer.
    Code:
    //my posted code
            ifstream is;
    	is.open ("text.txt", ios::in );
    
    	istreambuf_iterator<char> test1(is), test2;
    	int VarTest = imTestP | imTestE;
    	MimeEntity me(test1, test2, VarTest );
            printMimeStructure(&me);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    I've done as you told me, but the funny things is:
    1) Start without Debugging;
    2) VS2k5 opens the DOS window;
    3) I can see only the flashing _ of DOS and nothing else;
    4) after some seconds the system is slowing down and it freezes, have to restart!!

    I think that the problem is here:
    Code:
    	MimeEntity me(bit,eit);							// parse and load message
            printMimeStructure(&me);						// print msg structure
    Infact if I comment out these lines, I can see the:
    "Press any key to continue . . ."
    nothing else but the program exits instead of doing an infinite loop!

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, since we don't have the code of MimeEntity, how should we know?

    Start with debugging, track your code into the entity and find out where it's taking so long.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Well, since we don't have the code of MimeEntity, how should we know?
    You are right, but I can't post here or ask you an entire source (not only one function).
    I thought that it could be easy fix with only some infos.

    Btw I'll try to degub, as you suggested. Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM