Language/C & C++

[C] Read a file and remove the spaces between words

Rogue 2022. 10. 23. 18:07
반응형
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

/* Read a file and remove the spaces between words */
int main(void)
{
	FILE* filePtr;
	char fileBuff[255];
	int fileChar;

	//open file
	filePtr = fopen("C:\\Users\\hrmk\\OneDrive\\바탕 화면\\fox.map", "rt");

	if (filePtr)
	{
		// Read and print file content
		fgets(fileBuff, 255, filePtr);
		printf("The contet of the file is : \n%s", fileBuff);

		// Move file cursor to the start of the file
		fseek(filePtr, 0, SEEK_SET);

		printf("\n\nAfter removing the spaces the content is : \n");
		
		do
		{
			// Read a character from the file
			fileChar = fgetc(filePtr);

			// Display if character is a graphic character
			if (isgraph(fileChar))
			{
				putchar(fileChar);
			}
		} 
		while (fileChar != EOF);

		// Close the file
		fclose(filePtr);
	}

	else
	{
		printf("\nFile cannot be opened");
	}


	return 0;
}
반응형