Avatar of rebelcowgirl_73
rebelcowgirl_73

asked on 

Add contents of file to buffer, reverse the buffer and overwrite the current file

I have gotten options 1, 2, and 3 to work.  Here are my new delima's...
When user chooses option 4 the contents of the file should be read einto memory, all in one buffer, and the buffer is then reversed and the file is overwritten.  I can seem to figure out what I am doing wrong.  The max size for simplicity is 20000.  

If I can ever figure this out I still have to provide a error is option 2, 3, or 4 is chosen before option 1.  

Help ME PLEASE
#include <stdio.h>
 
main()
{
	int option;					//users choice for menu
	int result;					//used to rename file
	int length;				//used when reversing file
    char filename[50] = "";		//original file used in program
    char newfilename[50] = "";	//new name of original file when renamed
	char line[255];				//max length for displaying contents of file
	char *buffer;
	FILE *fp;					//file pointer to file being manipulated
 
 
	printf("					CMIS415 Project 1\n\n");
	printf("				     Created by ALI Balboa\n\n");
	printf("			~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");
	printf("			Welcome to the File Manipulation Program\n\n");
    printf("			~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");
	printf("\n");	
 
    do
    {
 
		printf("\n");
		printf("Here are the options available:\n");
		printf("0 - Exit Program\n");
		printf("1 - Select File to manipulate\n");
		printf("2 - Display contents of file\n");
		printf("3 - Rename file selected\n");
		printf("4 - Reverse contents of file\n");
		printf("_________________________\n");
		printf("Please choose the option you wish to perform: ");
		scanf("%d", &option);
		printf("\n");
     
		if(option == 1)//user will select file to manipulate
		{
			printf("Name of File to be Manipulated: ");
			scanf("%s", filename); 
		}
 
		else if(option == 2)//user wants file selected displayed on the screen
		{
			printf("Here are the contents of the file, %s", filename);
			printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
			//read the file in read mode
			fp = fopen(filename, "r");
			if((fp==NULL))
			{
				printf("Can't open file. Or No file was selected!  Program Terminated!\n");
				exit(1);
		    }
			//read from the file up to the end of the file
			while(!feof(fp))
			{
				if(fgets(line, 255, fp))
				printf("%s", line);
			}
			printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
			printf("End of File:  File has closed!\n");
			//close open file
			fclose(fp);
		}
		else if(option == 3)//user wants file selected renamed
		{
			printf("Current Name of File: %s", filename);
			printf("\nRename file as: ");
			scanf("%s", newfilename);
			//rename the unix file
			result = rename(filename , newfilename);
		}
		else if(option == 4)//user wants file selected displayed in reverse
		{
			fp = fopen(filename, "r");
			if((fp==NULL))
			{
				printf("Can't open file. Or No file was selected!  Program Terminated!\n");
				exit(1);
		    }
			//get file length
			fseek(fp, 0, SEEK_END);
			length = ftell(fp);
			fseek(fp, 0, SEEK_SET);
			
			//allocate memory
			buffer = (char *)malloc(length+1);
			
			//read file contents into buffer
			fread(buffer, length, 1, fp);
 
			fclose(fp);
			
		}
	} 
	while(option != 0);
	
	printf("		~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("			Thanks for using my File Manipulation Program!\n");
	printf("					Have a great day!\n");
	printf("					  Good Bye!\n");
 
    return 0;
}

Open in new window

C

Avatar of undefined
Last Comment
rebelcowgirl_73

8/22/2022 - Mon