Thread: A file parser

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    10

    A file parser

    Hello!!

    I'm trying to write a file parser. I have just began and I stuck.

    Let's assume that I have a file called 'profile.txt'. This file contains :

    "


    hello


    "

    2 new lines, the word "hello" and then 3 new lines (I opened the text file and pressed 2 times the enter key, then I wrote "hello" and then 3 times the enter key).

    I wrote this program to count and confirm that the file contains 4 new lines. The line which contains the word hello, needs not to be counted.

    So I wrote this :


    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    
    ifstream file("profile.txt");
    string tmp;
    
    	while(!file.eof())
    	{
    		if(file.peek()=='\n')
    		{
    			cout<<"ignored"<<endl;
    			file.ignore(1,'\n');
    		}
    		getline(file,tmp,'\n');
    	}
    
    file.close();
    
    return 0;
    
    }

    This code however doesn't seems to be working. Any ideas where my code is wrong?

    Thank you
    Last edited by Crashgr; 11-16-2004 at 08:06 PM.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Step 1: Fix the compile errors (I assume code got losted when you moved it to your post).
    Step 2: You should only call getline if the if statement fails, so add an else before the getline. You could also remove the ignore and let the getline read in the '\n' instead.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    10
    Thank you jlou for your answer!

    I edited the code and now it is correct. You guest right!! Code got lost when I moved it to my post!

    I tried removing the ignore and let the getline read the '\n', but it doesn't seem to work again.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by Crashgr
    I tried removing the ignore and let the getline read the '\n', but it doesn't seem to work again.
    Post your new code. When I commented out the line with the ignore, it worked just fine. Try copying and pasting your code so it is exact (you are still missing the #include <string> in the above post).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM