Thread: FILE * fp, remove one line from text file and duplicate it

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    FILE * fp, remove one line from text file and duplicate it

    Hey all...this is my grocery inventory program, also I aware to not use while (!feof(fp) however I haven't yet sorted through my code to find the correct statement. Also, i am wondering why my temp file is not being created (tmpfile, temp.txt). Thanks for help.
    Code:
    ...
    ...
    void remove_entry(void)
    {
    	FILE *fp;
    	FILE *fp2;
    	FILE *fp3;
    	FILE *tmpfile;
    	
    	char str[MAXCHAR];
    	char str2[MAX];
    
    	fp = fopen("REFRIGERATOR.TXT", "r+");
    	fp2 = fopen("FREEZER.TXT", "r+");
    	fp3 = fopen("DRY_STOCK.TXT", "r+");
    	tmpfile = fopen("temp.txt", "w");		// open temporary file in write mode
    
    	int ln, number, ctr;
    	ctr = 0;
    
    	if (!tmpfile)
    	{
    		printf("Unable to open a temporary file to write!\n");
    		exit(0);
    	}
    
    	puts("\n1. for Fridge\n2. for Freezer\n3. for dry shelve\n");
    	scanf("%d", &number);
    	
    
    	if(number == 1)
    	{
    		// Print REFRIGERATOR.TXT to screen
    
    		while(fgets(str, MAXCHAR, fp) != NULL)
    		{
    			printf("%s", str);
    		}
    		
    		puts("\n");
    
    		printf("Input the line you want to remove: ");
    		scanf("%d", &ln);
    		ln++;
    
    		// copy all contents to the temporary file except the specific line
    	
    		while (!feof(fp))
    		{
    			strcpy(str2, "\0");
    			fgets(str2, MAX, fp);
    			if (!feof(fp))
    			{
    				ctr++;
    				/* skip the line at given line number */
    				if (ctr != ln)
    				{
    					fprintf(tmpfile, "%s", str2);
    				}
    			}
    		}
    
    		menu();
    
    		fclose (fp);
    		fclose (tmpfile);
    	}
    }

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Please check your Standard Library for the tmpfile() function!!!

    Or check online: man tmpfile

    Try choosing a different name. Hint! Why is tmpfile in red along with other Standard Library functions??? ;^)

    BTW: Are your warning levels turned up to the highest level? If not, turn on, and turn UP!
    Last edited by rstanley; 01-06-2019 at 08:12 PM.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    My apologies. This was not the problem, although you should avoid using a variable name with the same name as a Standard Library function.

    Without seeing the entire program, I do not have an answer.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should fix what rstanley highlighted anyway.

    How do you know that tmp.txt is not being created? For example, is there the error message that you set to be printed?

    For testing purposes, you could try writing something to tmp.txt immediately after it is opened.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
            // Print REFRIGERATOR.TXT to screen
     
            while(fgets(str, MAXCHAR, fp) != NULL)
            {
                printf("%s", str);
            }
    
            // ....
    
            // copy all contents to the temporary file except the specific line
            while (!feof(fp))
    So, having read to the end of the file using the first while loop, do you still expect the 2nd while loop to do anything at all?

    There's no reason to make a completely different style of file reading for the second pass.

    Perhaps
    Code:
            while(fgets(str, MAXCHAR, fp) != NULL)
            {
                printf("%s", str);
            }
    // ...
            rewind(fp);
            while(fgets(str, MAXCHAR, fp) != NULL)
            {
                    if (ctr != ln)
                    {
                        fprintf(tmpfile, "%s", str);
                    }
            }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    Thanks Salem, that printed what I was looking for to the textfile

  7. #7
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    This is what I came up with, still need to add a scanf to accept two words, or else just use an underscore.
    Code:
    /* Grocery Inventory
     *
     * Author: deadend, 1/7/19
     *
     * 1 for Fridge
     * 2 for Freezer
     * 3 for dry goods
     *
     * save to text file */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXCHAR 1000
    #define MAX 256
    
    void refrigerator_func(void);
    void freezer_func(void);
    void dry_func(void);
    void write_func(void);
    void menu(void);
    void add_entry(void);
    void remove_entry(void);
    
    int main(void)
    {
    	menu();
    }
    
    void menu(void)
    {
    
    	int number = 0;
    
    	/*
    	 * number = 1 // Refrierator
    	 * number = 2 // Freezer
    	 * number = 3; // Dry storage
    	 */
    
    	while(1)
    	{
    		int reply;
    
    		puts("\n1. for Fridge\n2. for Freezer\n3. for dry shelve\n4. Add/Remove Entry\n5. Exit Program\n");
    		scanf("%d", &reply);
    
    		switch (reply)
    		{
    			case 1:
    				{
    					number = 1;
    					refrigerator_func();
    					break;
    				}
    
    			case 2:
    				{
    					number = 2;
    					freezer_func();
    					break;
    				}
    
    			case 3:
    				{
    					number = 3;
    					dry_func();
    					break;
    				}
    	
    			case 4:
    				{
    					write_func();
    					break;
    				}
    		
    			case 5:
    				{
    					exit(0);
    				}
    
    
    			default:
    				{
    					puts("Error!\n");
    					exit(1);
    				}
    		}
    	}
    }
    
    void refrigerator_func(void)
    {
    	FILE *refrig;
    	char str[MAXCHAR];
    
    	refrig = fopen("REFRIGERATOR.TXT", "r+");
    
    	if (refrig == NULL)
    	{
    		printf("Could not open file REFRIGERATOR.TXT\n");
    		exit(0);
    
    	}
    
    	puts("\n");
    
    	while(fgets(str, MAXCHAR, refrig) != NULL)
    		printf("%s", str);
    
    	fclose(refrig);
    }
    
    void freezer_func(void)
    {
    	FILE *freezer;
    	char str[MAXCHAR];
    
    	freezer = fopen("FREEZER.TXT", "r+");
    
    	if (freezer == NULL)
    	{
    		printf("Could not open file FREEZER.TXT\n");
    		exit(0);
    	}
    
    	puts("\n");
    
    	while(fgets(str, MAXCHAR, freezer) != NULL)
    		printf("%s", str);
    	fclose(freezer);
    }
    
    void dry_func(void)
    {
    	FILE *dry;
    	char str[MAXCHAR];
    
    	dry = fopen("DRY_STOCK.TXT", "r+");
    
    	if (dry == NULL)
    	{
    		printf("Could not open file DRY.TXT\n");
    		exit(0);
    	}
    
    	puts("\n");
    
    	while(fgets(str, MAXCHAR, dry) != NULL)
    		printf("%s", str);
    
    	fclose(dry);
    }
    
    void write_func(void)
    {
    	for (;;)
    	{
    		puts("\n");
    		printf("a. Add Entry\n");
    		printf("b. Remove Entry\n");
    
    		char choice;
    
    		scanf(" %c", &choice);
    
    		if (choice == 'a')
    			add_entry();
    		else if (choice == 'b')
    			remove_entry();
    		else
    			printf("Invalid choice.\n");
    	}
    }
    
    void add_entry(void)
    {
    	FILE *fp;
    	FILE *fp2;
    	FILE *fp3;
    
    	fp = fopen("REFRIGERATOR.TXT", "a+");
    	fp2 = fopen("FREEZER.TXT", "a+");
    	fp3 = fopen("DRY_STOCK.TXT", "a+");
    
    	int number;
    	char fridge_string[20];
    	char freezer_string[20];
    	char dry_string[20];
    
    	puts("\n1. for Fridge\n2. for Freezer\n3. for dry shelve\n");
    	scanf("%d",  &number);
    
    	if (number == 1)
    	{
    		printf("Enter item to add (%d): ", number);
    		scanf("%s", fridge_string);
    
    		fprintf(fp, "%s\n", fridge_string);
    
    		fclose(fp);
    		printf("\nADDED: %s\n", fridge_string);
    
    		menu();
    	}
    
    	else if (number == 2)
    	{
    		printf("Enter item to add (%d): ", number);
    		scanf("%s", freezer_string);
    
    		fprintf(fp2, "%s\n", freezer_string);
    
    		fclose(fp2);
    
    		printf("\nADDED: %s\n", freezer_string);
    
    		menu();
    	}
    
    	else if (number == 3)
    	{
    		printf("Enter item to add (%d): ", number);
    		scanf("%s", dry_string);
    
    		fprintf(fp3, "%s\n", dry_string);
    
    		fclose(fp3);
    
    		printf("\nADDED: %s\n", dry_string);
    
    		menu();
    	}
    
    	else
    	{
    		printf("Incorrect Entry\n");
    		add_entry();
    	}
    }
    
    void remove_entry(void)
    {
    	FILE *fp;
    	FILE *fp2;
    	FILE *fp3;
    	FILE *fp4;
    	
    	char str[MAXCHAR];
    	char str2[MAX];
    	char ch;
    
    	fp = fopen("REFRIGERATOR.TXT", "a+");
    	fp2 = fopen("FREEZER.TXT", "a+");
    	fp3 = fopen("DRY_STOCK.TXT", "a+");
    	fp4 = fopen("temp.txt", "a+");		// open temporary file in write mode
    
    	int ln, number, ctr;
    	ctr = 0;
    
    	if (!fp4)
    	{
    		printf("Unable to open a temporary file to write!\n");
    		exit(0);
    	}
    
    	puts("\n1. for Fridge\n2. for Freezer\n3. for dry shelve\n");
    	scanf("%d", &number);
    	
    
    	if(number == 1)
    	{
    		// Print REFRIGERATOR.TXT to screen
    
    		while(fgets(str, MAXCHAR, fp) != NULL)
    		{
    			printf("%s", str);
    		}
    		
    		puts("\n");
    
    		printf("Input the line you want to remove: ");
    		scanf("%d", &ln);
    
    		while(fgets(str2, MAX, fp) != NULL)
    		{
    			printf("%s", str2);
    		}
    
    		rewind(fp);
    		while(fgets(str2, MAX, fp) != NULL)
    		{
    			ctr++;
    			if (ctr != ln)
    			{
    				fprintf(fp4, "%s", str2);
    			}
    		}
    
    
    		fclose(fp);
    		fclose(fp4);
    
    		// remove REFRIGERATOR.TXT
    		remove("REFRIGERATOR.TXT");
    		
    		// copy fp4 ("temp.txt") to REFRIGERATOR.TXT
    		rename("temp.txt", "REFRIGERATOR.TXT");
    
    		// now output buffer fp4 to screen
    
    		fp=fopen("REFRIGERATOR.TXT", "r");
    		ch = fgetc(fp);
    
    		printf("Now the contents of the file REFIRGERATOR.TXT is: \n\n");
    		while(ch != EOF)
    		{
    			printf("%c", ch);
    			ch = fgetc(fp);
    		}
    
    		fclose(fp);
    
    		menu();
    	}
    
    	else if (number == 2)
    	{
    		// Freezer (2) option (FREEZER.TXT)
    
    		while(fgets(str, MAXCHAR, fp) != NULL)
    		{
    			printf("%s", str);
    		}
    
    		puts("\n");
    		
    		printf("Input the line you want to remove: ");
    		scanf("%d", &ln);
    
    		while(fgets(str2, MAX, fp2) != NULL)
    		{
    			printf("%s", str2);
    		}
    
    		rewind(fp2);
    		while(fgets(str2, MAX, fp2) != NULL)
    		{
    			ctr++;
    			if (ctr != ln)
    			{
    				fprintf(fp4, "%s", str2);
    			}
    		}
    		
    		fclose(fp2);
    		fclose(fp4);
    
    		remove("FREEZER.TXT");
    
    		rename("temp.txt", "FREEZER.TXT");
    
    		fp = fopen("FREEZER.TXT", "r");
    		ch = fgetc(fp2);
    
    		printf("Now the contents of the file FREEZER.TXT is: \n\n");
    		while(ch != EOF)
    		{
    			printf("%c", ch);
    			ch = fgetc(fp2);
    		}
    		
    		fclose(fp2);
    
    		menu();
    	}
    
    	else if (number == 3)
    	{
    		// print DRY_STOCK.TXT to screen
    
    		while(fgets(str, MAXCHAR, fp3) != NULL)
    		{
    			printf("%s", str);
    		}
    
    		puts("\n");
    		
    		printf("Input the line you want to remove: ");
    		scanf("%d", &ln);
    
    		while(fgets(str2, MAX, fp3) !=NULL)
    		{
    			printf("%s", str2);
    		}
    
    		rewind(fp3);
    		while(fgets(str2, MAX, fp3) != NULL)
    		{
    			ctr++;
    			if (ctr != ln)
    			{
    				fprintf(fp4, "%s", str2);
    			}
    		}
    
    		fclose(fp3);
    		fclose(fp4);
    
    		// remove DRY_STOCK.TXT
    		remove("DRY_STOCK.TXT");
    
    		rename("temp.txt", "DRY_STOCK.TXT");
    
    		// now outbut buffer fp3 to screen
    
    		fp = fopen("DRY_STOCK.TXT", "r");
    		ch = fgetc(fp3);
    
    		printf("Now the contents of the file DRY_STOCK.TXT is: \n\n");
    		while (ch != EOF)
    		{
    			printf("%c", ch);
    			ch = fgetc(fp3);
    		}
    
    		fclose(fp3);
    
    		menu();
    	}
    }

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    I have some ideas for "improvements" (obviously in the eye of the beholder ). Let me know if you're interested. (I don't want to work on it for 20 minutes and find you don't care!)
    A little inaccuracy saves tons of explanation. - H.H. Munro

  9. #9
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    john.c:

    right now i'm working through these Exercises.. C programming Exercises, Practice, Solution - w3resource (the array section)

    I finished a C programming book and the code above was what I wrote to the last exercise in the book, write a program with what I have learned in all 22 lessons.
    Last edited by _jamie; 01-08-2019 at 12:16 PM. Reason: link formatting

  10. #10
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    john.c:

    how about what to look for if I was running the program through gdb on freebsd...or a debugger....or anyone else. I am familar with "gcc -g -o grocery grocery_inventory.c" to enable debugging support and what is mentioned in the 2.6. Debugging

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-11-2011, 10:32 PM
  2. Replies: 2
    Last Post: 07-23-2011, 10:26 PM
  3. Remove line from a file
    By Winkle in forum C Programming
    Replies: 0
    Last Post: 11-22-2010, 06:41 PM
  4. How to check duplicate line in text file?
    By ooosawaddee3 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2002, 06:35 PM
  5. How to erase duplicate lines in a text file?
    By miketv in forum C Programming
    Replies: 0
    Last Post: 02-25-2002, 11:18 PM

Tags for this Thread