Thread: reading and writing to file

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    38

    Exclamation reading and writing to file

    I have written a piece of code that is supposed to identify and record
    all the text in between tags

    (ie. <title> help please </title>

    code would only record
    <title> and </title>

    I compiled and the compiler did not catch any errors.
    I can open the file and create the output file, but then the console just hangs up
    I am sure it is how I have my if loop worded, any help would be appreciated even if it is a look at this line!

    this is the code that i am working with
    Code:
    	inFile.get( ch ); // this reads one character
    	// notice this does not skip whitespace
    
    	while( !inFile.eof() )
    	{
    		if (ch == '<')
                    {
    
    		//outFile << ch; // write this character to the output file
    		//cout << ch;  // write this character to the monitor
    		outFile.put( ch );
    		cout.put( ch );
    		inFile.get( ch ); // read the next character from the file
                    outFile.put( '>' );
                    cout.put( '>' );
                    }
    
    	}
    thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Look at your loop. What happens if the character is not '<'?

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Inside your loop, you only checked for occurences of '<' and didn't move the cursor to the next character if the '<' is not found. So the inFile won't get to eof because you didn't iterate to eof. So it will loop forever because the inFile's cursor will be the same every time it loops.

    I don't really understand what you want to do with the program. But I guess this is what you wanted:

    Code:
    	inFile.get( ch ); // this reads one character
    	// notice this does not skip whitespace
    
    	while( !inFile.eof() )
    	{
    		if (ch == '<')
                    {
    
    		        //outFile << ch; // write this character to the output file
    		        //cout << ch;  // write this character to the monitor
    
                           while (ch!='>')
                           {
    		              outFile.put( ch );
    		              cout.put( ch );
    		              inFile.get( ch ); // read the next character from the file
                           }
                           outFile.put( '>' );
                           cout.put( '>' );
                    }
                    inFile.get( ch );
    
    	}

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    thanks
    that was not exactly it but it helped me see where my mistakes were
    !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmetation fault (reading and writing file in c)
    By tasosa in forum C Programming
    Replies: 5
    Last Post: 04-13-2009, 06:04 AM
  2. Reading out of and writing into the same file
    By Wiretron in forum C Programming
    Replies: 8
    Last Post: 12-30-2006, 02:04 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 11:18 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM