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.