View Single Post
Old 09-26-2005, 04:10 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
Take a look at this small program
Code:
#include <stdio.h>

int main(int argc, char* argv[])
{
  FILE *fin, *fout;
  char c;
  unsigned short int space = 0;
  if(argc != 3)
    {
      printf("Usage: %s <in_file> <out_file>\n", argv[0]);
      return -1;
    }
  if(!(fin = fopen(argv[1], "r")))
    {
      printf("Error opening input file %s\n", argv[1]);
      return -1;
    }
  if(!(fout = fopen(argv[2], "w")))
    {
      printf("Error opening output file %s\n", argv[2]);
      return -1;
    }
  while( (c=fgetc(fin)) && !feof(fin))
    {/* read everything in the file char by char */
      if(c == ' ' || c == '\t')
	{
	  space = 1;
	  continue; /* eliminate all whitespaces */
	}
      /* everything else write it, and add a space infront of a 
       * word, if we've just come from a line of spaces 
       */
      if(!fprintf(fout, "%s%c", space?" ":"", c))
	{
	  printf("Error writing to output file %s\n", argv[2]);
	  fclose(fin);
	  fclose(fout);
	  return -1;
	}
      space = 0;
    }
  fclose(fin);
  fclose(fout);
  return 0;
}
At first I wasn't sure you were talking about text files, and was going for a rather advanced solution for any form of binary file..
Anyway feel free to ask, if you encounter more problems, and welcome here at Code Newbie.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote