i have made a program on text file to remove extra blank spaces in a file but it is not working correctly. when i run the program just "enter text" appears and after entering the text the screen just remains as it was. Nothing else appears. I cant find where the problem lies.

char  a[150];
  char ch;
  int count=0;
  cout<<"Enter text"<<endl;
  gets(a);
  ofstream file("EXAMPLE.txt");
  for(int i=0; a[i]!='\0'; i++)
  {
    file.put(a[i]);
  }
  file.close();
  ifstream fin("EXAMPLE.txt");
  ofstream fout("NEW.txt");
  while(fin)
  {
    ch=fin.get();
    while(ch==' ')
    {
       if(count==0)
          fout.put(ch);
       ch=fin.get();
       count++;
     }
     fout.put(ch);
     count=0;
  }
  fin.close();
  fout.close();

  ifstream fil("NEW.txt");

  while(fil)
  {
     ch=fil.get();
     cout<<ch;
  }

thanx in advance...

Never mix C and C++ I/O unless you know what you are getting in to. (Yes, the C++ standard says you can mix on the same target stream, but that is still somewhat vague and you mileage may vary with different compilers. Just don't mix unless you are forced to.)

That is the only thing I can see that can be messing you up...

Now, just get rid of every line with count on it and it should work just fine...

Never mix C and C++ I/O unless you know what you are getting in to. (Yes, the C++ standard says you can mix on the same target stream, but that is still somewhat vague and you mileage may vary with different compilers. Just don't mix unless you are forced to.)

That is the only thing I can see that can be messing you up...

Now, just get rid of every line with count on it and it should work just fine...

sorry i cant understand you...hope you dont mind explainig a bit further...
tell from my program where i am gettong wrong and what should i do to solve the problem...

Nothing else is output to the screen. What do you expect to happen? If you think there's a problem, add a few cout statements to follow what's happening and pinpoint the problem.

And isn't this a C++ program? Why are you using the extremely dangerous C function gets() when there are perfectly good C++ input functions available?

Your compiler might be choking because you are mixing C and C++ I/O. While this isn't supposed to be bad, it sometimes is --particularly with older compilers.

Change your first few lines to:

string a;
char ch;
cout << "Enter a line of text:" << endl;
getline( cin, a );
ofstream file( "EXAMPLE.txt" );
file << a;
file.close();

In general, when coding C++ use the C++ way of doing file I/O, and don't use the C way of doing file I/O.

Also, every line that has the word count on it needs to be deleted. It is preventing those spaces from disappearing.

Hope that makes better sense.

[EDIT]
WaltP you missed the fil ifstream at the bottom... so he actually should see more output on the screen...

Also, every line that has the word count on it needs to be deleted. It is preventing those spaces from disappearing.

count is used to delete extra spaces. Removing them will prevent spaces from being deleted.

[EDIT]
WaltP you missed the fil ifstream at the bottom... so he actually should see more output on the screen...

No, I missed the cout at the end. But miss something I did...

WaltP, I've compiled and tested the code. If count is zero then the spaces are written to the output file, and count is always reset to zero. It has no other effect on the code.

Nothing else is output to the screen. What do you expect to happen? If you think there's a problem, add a few cout statements to follow what's happening and pinpoint the problem.

i did this while(fin) loop is running endlessly...

And isn't this a C++ program? Why are you using the extremely dangerous C function gets() when there are perfectly good C++ input functions available?

and i have to use that perfectly dangerous C function because this is an assignment and we have not been taught another method to get a character array from user...

and DUOAS sir i cant use string a;
we have not been taught this yet...

If count is 0, the space is written. count is then incremented. If another space is immediately read, it is not written because count is no longer 0. He has effectively removed extra spaces as his original post describes:

i have made a program on text file to remove extra blank spaces in a file but it is not working correctly.

/Me feels stupid

Perhaps I should get some sleep.

@tracethepath
To read a string as a character array then use cin.getline( a, 150 ); G'night all...

sir as told by you i also changed that while(fin) loop to this...

while(fin)
  {
    ch=fin.get();
    fout.put(ch);
    
    while(ch==' ')
       ch=fin.get();

  }

but still its running infinitely..

while(fin)
  {
    ch=fin.get();
    fout.put(ch);
    
    while(ch==' ')
       ch=fin.get(); //*****

  }

If the last character in the file is a space, your inner loop will run forever because ch is never changed but the fin.get() has returned an error which you never test for.

Go back to your other code with count . It should work. This code will skip each character that follows a space. And make sure you always test for an error from all of your your inputs.


Go back to your other code with count . It should work. This code will skip each character that follows a space. And make sure you always test for an error from all of your your inputs.

ohk going back to my code with count...
in that too while(fin) is running endlessly...can you tell me the reason why?...
thanx in advance...

ohk going back to my code with count...
in that too while(fin) is running endlessly...can you tell me the reason why?...
thanx in advance...

I just did.

> while(fin) is running endlessly ..
in general, when reading an entire file, it is a good idea to embed the input statement inside the while loop's condition. this would eliminate a lot of subtle errors. and the loop will exit once eof is reached. eg.

#include <fstream>
int main()
{
  std::ifstream in( "in.txt" ) ;
  std::ofstream out( "out.txt" ) ;
  char ch ; 
  bool last_was_space = false ;
  while( in.get(ch ) )
  {
    if( ch != ' ' )
    {
      out.put(ch) ;
      last_was_space = false ;
    }
    else if( !last_was_space )
    {
       last_was_space = true ;
       out.put(ch) ;
    }
  }
}

note: this will remove extra spaces; but will not take care of tab characters.

If the last character in the file is a space, your inner loop will run forever because ch is never changed but the fin.get() has returned an error which you never test for.

oh yeah got your point. Thanx...

> while(fin) is running endlessly ..
in general, when reading an entire file, it is a good idea to embed the input statement inside the while loop's condition. this would eliminate a lot of subtle errors. and the loop will exit once eof is reached.

i did that and now my loop is working fine.thanx...
but can you illustrate why while(fin) was not working correctly and while(fin.get(ch)) did?

your original code was fine *except* for that it did not take care of end of file correctly.

while(fin)
  {
    ch=fin.get(); // this may fail; if it does
    // ch will get the coerced value of char_traits<char>::eof()  
    while(ch==' ') 
    {
       if(count==0)
          fout.put(ch);
       ch=fin.get(); // this too may fail
       // ch will again get the coerced value of 
       // char_traits<char>::eof() which may or may not be == ' '
       count++;
     }
     fout.put(ch);
     count=0;
  }

I was surfing around, looking for the same solutions. But few hours ago, I've managed to solve it. This is my code & please, comment my codings:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main(){
    
    int count = 0;
    char a;
    
    ifstream fin;
    fin.open("yeehaa.txt");
    
    ofstream fout;
    fout.open("yeehuu.txt");
    
    while(!fin.eof())
    {
          fin.get(a);
          
          if(isspace(a))
              count++;
          
          if(isspace(a) && count >= 2)
          {
              cout<<"";
              count = 0;
          }

          else
          {
              cout<<a;
              fout<<a;
          }
          
          
    }
    
    fin.close();
    fout.close();
    
    
    system("pause");
}

Here is my full code , which accepts input text file and output textfile as commandline parameters and removes spaces from given text file.

/* Program to remove all white spaces from a text file
Author:	udayasankar
date:	30-08-08					*/

#include<iostream.h>
#include<ctype.h>
#include<string.h>
#include<fstream.h>
#define LINESIZE 2500
int main(int argc, char * argv[])
{
	char   chline[LINESIZE+1];
	const char * self=argv[0];
	const char * ipfile=argv[1];
	const char * opfile=argv[2];
	if ( ! (argc==3))
	  {
		if (argc<3)
		   cout<<"\n\nerror:  too few arguments"<<endl;
		if (argc>3)
		   cout<<"\n\nerror: too many parameters"<<endl;
		cout<<"\n\n\nTo remove white spaces from a given file and \nkeep the remaining in an output file.\n";
		cout<<"\n\n\nusage  :\n\n\t"<<argv[0]<<" <input.txt> <output.txt> \n\n\n\n";
		return 1;
	}
	ifstream a_file (ipfile,ios::in|ios::nocreate);
	if (!a_file) {
	   cout<<"\n\nThe file(s)   could not be opened.\nThe specific error is:\n";
	   cout<<"Cannot open file: "<<ipfile<<" to read as input (:-"<<endl;
	   cout<<"Make sure that the file is present"<<endl;
	   cout<<"\nCalled from "<<self<<endl;
	   return 1;
	}
	ofstream b_file (opfile,ios::out);
	if (!b_file){
	   cout<<"\n\nThe file(s)   could not be opened.\nThe specific error is:\n";
	   cout<<"Cannot open file: "<<opfile<<" to write output (:-\n";
	   cout<<"Make sure that the file is not write protected nor read only innor you have sufficient priviliges to write in the specified directory.";
	   cout<<"\ncalled from :"<<self<<endl;
	   return 1;
	}

	cout<<"\n\n\n\nPlease wait while removing spaces:";
	while (a_file) {
	      a_file.getline(chline,LINESIZE,'\n');
	      for (int i=0; i<strlen(chline);i++){
		if (! isspace(chline[i]))
		   b_file.put(chline[i]);
	      }
	      b_file<<endl;
	}
	a_file.close();
	b_file.close();
	cout<<"\nresult is in "<<opfile<<"\n\n\n\n";
	return 0;
}

PS:-

This code is Borland Turbo c++ version 3 compliant.
Will not function correctly with VC++ 6.0 (verified)

Request: when given a non-text file as an input, it is failing.
How to detect the input whether it is a plain text or not?
Please help me.
For plain text files, it is working fine and performing the desired operation well.

This question has already been answered. Start a new discussion instead.