title: Blank Space Removal- C Programming
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/8113938/consecutive-blank-space-removal-in-c">Consecutive Blank space Removal in C</a>  </p>
  </blockquote>
  
  
  
  <p>Prompt:
  A text file contains a bunch of characters. There are no tab characters within the file. Write a program that replaces two or more consecutive blanks by a single blank. The input from this program should come from a file whose name has been supplied via argv[1]. The output from this program should go to standard output.</p>
  
  <p>Question: How to remove consecutive blank spaces in a text file?</p>
  
  <p>I have started my code and it compiles but then doesn't do anything. Im not sure where I went wrong with the code. I want the code to basically simulated a FMS which says that if the text read in is equal to a blank space two times in a row then place the text from the file there but I am having trouble getting it to print out correctly.</p>
  
  <p>Input:</p>
  
  <pre><code>Let’s   go  to  the movies.
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Let’s go to the movies.
  </code></pre>
  
  <p>My written Code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(int argc, char* argv[]){
      int i;
      char c;
      FILE* fin;
  
      fin=fopen("textfile38", "r");
      fscanf(fin,"%c", &amp;c);
      while((i=getchar()) !=EOF)
              putchar(c);
      if(i ==' ')
      {
              putchar(i);
      }
      else
      {
              putchar(' ');
      }
      printf("%c \n", c);
      return 0;
  }
  </code></pre>
  
  <p>I get it to return L. Am I not reading in all of the characters? Any help would be appreciated thanks.</p>
  
  <p><strong>UPDATED CODE:</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(int argc, char* argv[]){
      char c;
      FILE* fin;
  
      fin=fopen("textfile38", "r");
      while(fscanf(fin,"%c", &amp;c) !=EOF){
              if(c ==' ')
              {
                      putchar(c);
              }
              else
              {
                      putchar(' ');
              }
              printf("%c", c);
      }
      return 0;
  }
  </code></pre>
  
  <p>Only problem is that the spaces are still there from the input, also its printing vertically not horizontally, and i don't know why. </p>