Thread: Deleting 4 lines from a file

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    3

    Deleting 4 lines from a file

    Hi, I'm pretty new to programming and I have a question. I've created a function for a program that is supposed to delete 4 lines from a file. The program is movie database and I want it to take in the year(in this case 1994) and delete the year and the 4 lines below it. I've gotten to the point where it deletes 1 line, 1994. Can anyone help me figure out what I should do to delete 4?

    Code:
    void deleteLines(struct MovieDB *pMovies, int count){    FILE *pFile;//pointing to buffer
        char line[50];
        char *buffer;//pointers are 4 bytes, better than creating a buffer array 
        char *ptr;
        printf("Enter a name you want deleted");
        buffer = (char *)malloc(1000*sizeof(char)); //passes the type and returns the size of that type
        //you want this buffer to initilized to all zeros ^
        memset(buffer, 0, 1000*sizeof(char));
    
    
        /*buffer is that start adress, the value you want to store in each element which is zero
         The number of bytes which is the same as above. when you allocate a buffer you have to zero it
        */
        ptr = buffer;
        //open the file
        pFile =fopen(FileName, "r");
    
    
        if(pFile != NULL)
        {
            while (!feof(pFile)){
                fgets(line, 50, pFile);
    
    
                if(strcmp(line, "1994\n") != 0)//line is equal to "1994"
                {
                    //put line in buffer
                    strcpy(ptr, line);//destination string, source string
                    ptr += strlen(line); //increments ptr by the length of the line. puts it back into ptr
                }
            }
            fclose(pFile);
            
            pFile = fopen(FileName, "w");
            fprintf(pFile, "%s", buffer);
            fclose(pFile);
        }
        else{
                printf("Could not open the file.\n");
        }
    
    
        getch();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    A really simple way would be to call fgets() just 3 times in a row and throw the result away.

    Oh, and make your initial while loop
    while ( fgets(line, 50, pFile) )
    See the FAQ for why using feof() to control a loop is bad.

    Are you assuming that the file will always fit in 1000 characters (the buffer you allocate)?

    You also need
    free(buffer);
    when you're done.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    3
    Thank You for responding . I'm just assuming for now that the file will always fit into the buffer. Also, do you literally mean write my fgets() line three times or something else?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    He means something like this:
    Code:
            while (fgets(line, 50, pFile))
            {
                if (strcmp(line, "1994\n") == 0)  // changed from != to ==
                {
                    // read and discard next 4 lines
                    fgets(line, 50, pFile);
                    fgets(line, 50, pFile);
                    fgets(line, 50, pFile);
                    fgets(line, 50, pFile);
                    continue;
                }
                strcpy(ptr, line);
                ptr += strlen(line);
            }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    3
    Ok, I understand. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with deleting completely blank lines
    By dnguyen1022 in forum C Programming
    Replies: 3
    Last Post: 12-07-2008, 11:51 AM
  2. deleting empty lines from a file
    By smoking81 in forum C Programming
    Replies: 4
    Last Post: 03-09-2008, 02:10 AM
  3. lines in a file
    By ozzy34 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2004, 01:51 AM
  4. # of lines in a file...
    By Yoshi in forum C++ Programming
    Replies: 10
    Last Post: 01-01-2003, 07:52 PM
  5. need help deleting a deleting a struct from file
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-20-2002, 05:38 AM