Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,395 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,123 people online right now. Registration is fast and FREE... Join Now!




Removing Spaces from Strings

 
Reply to this topicStart new topic

Removing Spaces from Strings, File I/O

chris.tkd
27 Sep, 2006 - 11:35 AM
Post #1

D.I.C Head
Group Icon

Joined: 26 Sep, 2006
Posts: 60


Dream Kudos: 25
My Contributions
Hey can anyone help me with this problem. Ive to open a file, create a copy of it, and remove all the spaces. I just cant figure out how to remove the spaces. My code is below. Thanks in advance

CODE

#include <stdio.h>
#include <ctype.h>


main()
{
      /*create files and char*/
      FILE *original;
      FILE *copy;
      char ch;
      
      /*open input and create output*/
      original = fopen("input.txt", "r");
      copy = fopen("output.txt","w");
      
      /*while there is data in the file
        check if its a space, if it is
        dont copy it, else copy it to
        the output file*/
      while(ch!=-1)
       {
            ch = fgetc(original);
            if(isspace(ch))
            {
                 //Do not print out <--- THIS IS WHERE IM STUCK  
            }
            fputc(ch,copy);
       }
      
       /*close open files*/
       fclose(original);
       fclose(copy);
       printf("The copy operation has been complete");
       getch();
}

User is offlineProfile CardPM
+Quote Post

janotte
RE: Removing Spaces From Strings
28 Sep, 2006 - 02:14 AM
Post #2

D.I.C Head
**

Joined: 28 Sep, 2006
Posts: 241



Thanked: 21 times
My Contributions
QUOTE(chris.tkd @ 27 Sep, 2006 - 12:35 PM) *

Hey can anyone help me with this problem. Ive to open a file, create a copy of it, and remove all the spaces. I just cant figure out how to remove the spaces. My code is below. Thanks in advance

CODE

#include <stdio.h>
#include <ctype.h>


main()
{
      /*create files and char*/
      FILE *original;
      FILE *copy;
      char ch;
      
      /*open input and create output*/
      original = fopen("input.txt", "r");
      copy = fopen("output.txt","w");
      
      /*while there is data in the file
        check if its a space, if it is
        dont copy it, else copy it to
        the output file*/
      while(ch!=-1)
       {
            ch = fgetc(original);
            if(isspace(ch))
            {
                 //Do not print out <--- THIS IS WHERE IM STUCK  
            }
            fputc(ch,copy);
       }
      
       /*close open files*/
       fclose(original);
       fclose(copy);
       printf("The copy operation has been complete");
       getch();
}



I'm a total beginner so this may be silly.

If you inverted the logic and did this does it work? (I haven't tested it). If you only 'put' when it's NOT a space does it solve your problem?

Apologies if this is a waste of time.

CODE

while(ch!=-1)
       {
            ch = fgetc(original);
            if(!isspace(ch))
            {
                fputc(ch,copy);
            }
            
       }


This post has been edited by janotte: 28 Sep, 2006 - 02:17 AM
User is online!Profile CardPM
+Quote Post

chris.tkd
RE: Removing Spaces From Strings
28 Sep, 2006 - 03:03 AM
Post #3

D.I.C Head
Group Icon

Joined: 26 Sep, 2006
Posts: 60


Dream Kudos: 25
My Contributions
yeah i worked it out another way i used
CODE

if(isspace(ch))
{
       ch=NULL;  
}
else
{
        fputc(ch,copy);
}

but i like your way better cos its shorter. cheers for the help.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:44AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month