Thread: stl fstream error (yes... file problems again)

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490

    stl fstream error (yes... file problems again)

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string>
    #include <fstream>
    int main()
    {
          string filename, line;
          cout << "Input filename:";
          cin >> filename;
          ifstream in(filename.c_str());
          filename+=".out";
          ofstream out(filename.c_str());
          while (in) {
            getline(in,line);
            while (line[0]==' ') { line.erase(0,1);  cerr << line << endl;}
            out  << line << endl;
            cerr << line << endl;
          }
          in.close();
          out.close();
    
    
          return 0;
    }
    i own a ti-89 graphing calculator. i want to read the massachusetts drivers manual on there. unfortunately, all text readers only use text (duh...), and don't support pdf files. i've converted the files to txt, but they need to be... pruned... before transferring to my graphing calculator. in particular, there's a lot of leading space. this program is designed to input a text file and output another text file, with another extension, with all of its leading spaces cleaned up. ie:
    Code:
          This
          is
          my
          manual
          now
    Code:
    This
    is
    how
    i
    want
    it
    to
    be
    here's the problem: in the loop to remove spaces, the program gets stuck in an infinite loop. i don't know why. it trips on every file i've tried, but the place it trips varies. i've cut and pasted a section of that manual and posted it here. it trips up on this file too, so it's probably some hidden characters i've overlooked. any ideas? thanks in advance

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You were only erasing a single character, not all the leading spaces. This works with leading spaces, but will screw up with tabs as whitespace (it keeps them), but it shouldn't be too hard to change if ya want to.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
          string filename, line;
          cout << "Input filename:";
          cin >> filename;
          ifstream in(filename.c_str());
          filename+=".out";
          ofstream out(filename.c_str());
          while (in) {
            getline(in,line);
    		size_t i=0;
            for (i; i < line.size(); i++) {
    			if (line[i] != ' ') break;
    		}
    		line.erase(0,i);
            out  << line << endl;
            cerr << line << endl;
          }
          in.close();
          out.close();
    
    
          return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i've changed the file to what you have but it still trips up under the text file i have posted.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It worked for me under Linux with gcc 2.96 and 3.0.4 (both your test, and my own test that I made). What compiler are you using? Are you sure you are opening the right files?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Maybe you could read in the entire file then parse through it? If I understand correctly what you wanted something like this perhaps will work.
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main()
    {
          string filename, filename2;
    	  int length = 0;
    
    	  cout << "Input filename:";
          cin >> filename;
    
    	  filename2 = filename;
    	  filename += ".txt";
    
          ifstream in( filename.c_str() );
    	  if( !in )
    		  return -1;
    
    	  filename2 += ".out";
          ofstream out(filename2.c_str());
    	  if( !out )
    		  return -1;
    
    	  // Find out length of file
    	  in.seekg( 0, ios::end );
    	  length = in.tellg();
    	  in.seekg( 0, ios::beg );
    	  cout << length << endl;
    
    	  // Allocate room for contents
    	  char *pBuffer = new char[ length + 1 ];
    	  
    	  // Read in entire file
    	  in.read( pBuffer, length );
    	  	  
    	  // Parse buffer
    	  for( register int i = 0; i <= length; i++ )
    	  {
    		  if( *(pBuffer + i ) != ' ' )
    			  out << *(pBuffer + i);
    		  else
    			  out << "\n";
    	  }
    	
    	  // Close files
          in.close();
          out.close();
    
    	  // Free memory
    	  delete [] pBuffer;
    
          return 0;
    }
    There is some garbage at the bottom but I kind of quickly through this together. Maybe it will help you.

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    huh... it does work with gcc in linux. i was using dev-c++ in windows. can anyone with a windows compiler replicate the problem, or is it just my computer?

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Lemme try...hold on a sec..
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It works fine on mine (dev-c++/Windows)...strange.

    This takes care of extra newlines:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main()
    {
          string filename, filename2;
    	  int length = 0;
    
    	  cout << "Input filename:";
          cin >> filename;
    
    	  filename2 = filename;
    	  filename += ".txt";
    
          ifstream in( filename.c_str() );
    	  if( !in )
    		  return -1;
    
    	  filename2 += ".out";
          ofstream out(filename2.c_str());
    	  if( !out )
    		  return -1;
    
    	  // Find out length of file
    	  in.seekg( 0, ios::end );
    	  length = in.tellg();
    	  in.seekg( 0, ios::beg );
    	  cout << length << endl;
    
    	  // Allocate room for contents
    	  char *pBuffer = new char[ length + 1 ];
    	  
    	  // Read in entire file
    	  in.read( pBuffer, length );
    
          bool ignore = true;
    	  // Parse buffer
    	  for( register int i = 0; i <= length; i++ )
    	  {
    
                  if( *(pBuffer + i ) != ' ' &&  *(pBuffer + i ) != '\n')
                   {
                    out << *(pBuffer + i);
                    ignore = false;
                   }
                  else
                  {
                    if(!ignore)
                    {
                     out << "\n";
                     ignore = true;
                    }
                  }
          }
    	
    	  // Close files
          in.close();
          out.close();
    
    	  // Free memory
    	  delete [] pBuffer;
    
          return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM