dear guru's,

I had a problem in C, and i seems i can't figure out why this piece of codes seems not working properly, kindly help my with my little problem?

/* My problem here is, how can i copy the file into other file name, i just wanted to copy them eliminate the leading spaces leaving only one space each and every after word. Kindly help me figure out what type of condition can i use here?

ex:
from: have' '' '' 'a' '' 'nice' '' '' '' 'day.
to: have a nice day.
*/

================================================== ==
code starts here:
================================================== ==

/* Copying a file and eliminate white spaces and tab. */
#include <stdio.h>
#include <stdlib.h>

int file_copy( char *oldname, char *newname );

main()
{
char source[80], destination[80];

/* Get the source and destination names. */

printf("\nEnter source file: ");
gets(source);
printf("\nEnter destination file: ");
gets(destination);

if ( file_copy( source, destination ) == 0 )
puts("Copy operation successful");
else
fprintf(stderr, "Error during copy operation");
return(0);
}
int file_copy( char *oldname, char *newname )
{
FILE *fold, *fnew;
int c,b,col;

/* Open the source file for reading in binary mode. */

if ( ( fold = fopen( oldname, "r" ) ) == NULL )
return -1;

/* Open the destination file for writing in binary mode. */

if ( ( fnew = fopen( newname, "w" ) ) == NULL )
{
fclose ( fold );
return -1;
}

/* Read one byte at a time from the source; if end of file */
/* has not been reached, write the byte to the */
/* destination. */

while (1)

{
c = fgetc( fold );

/* My problem here is, how can i copy the file into other file name, i just wanted to copy them eliminate the leading spaces leaving only one space each and every after word. Kindly help me figure out what type of condition can i use here?

ex:
from: have' '' '' 'a' '' 'nice' '' '' '' 'day.
to: have a nice day.
*/
if ( !feof( fold ))

fputc(c,fnew );
}

fclose ( fnew );
fclose ( fold );

return 0;
}
================================================== ==
code ends here:
================================================== ==

thank you,
j

Recommended Answers

All 2 Replies

you are trying to do it the hard way. fgets() and strtok() functions that make it very simple. fgets() reads a whole line, and strtok() splits it into its individual parts based on a token, such as a space, that you give it.

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

int main(int argc, char* argv[])
{
	char buf[255];
	FILE* in = fopen("infile.txt","r");
	if(in == NULL)
	{
		printf("file not found\n");
		return -1;
	}
	FILE* out = fopen("outfile.txt","w");
	while( fgets(buf,sizeof(buf),in) != 0 )
	{
		char *ptr = strtok(buf, " ");
		while(ptr != 0)
		{
			fprintf(out,"%s ", ptr);
			ptr = strtok(0," ");
		}
	}
	fclose(in);
	fclose(out);
	
	return 0;
}

Im just following the problem requirements definitely we are not allowed to use system define functions, like strtok.

thanks
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.