Thread: An older post

  1. #1
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95

    An older post

    I am trying to understand the code posted earlier; I am working on the same problem. And I wonder if I can ask for a high level explanation of it; such as what ismpty flag does and high level explanation of what is going on in the program please. How are empty lines detected? The code is from deleting empty lines from a file
    Here it is:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define BUFFSIZE 1024
     
    void filecopy(FILE *from, FILE *to);
     
    int main ()
    {
        FILE *f;
        char buffer[BUFFSIZE]={0};
        if (f=fopen("changeme.txt","r+"))
        {   
            char c; int i=0,isempty=1;
            FILE *tmp;
            if (tmp=tmpfile())
            {
                while ((c=getc(f))!=EOF)
                {
                     
                    buffer[i++]=c;
                    if (c!=' '&&c!='\n'&&c!='\t')
                    {   
                        isempty=0;
                    }
                    else
                    {
                        if (c=='\n'&&isempty==1)
                        {
                            buffer[i]='\0';
                            i=0;
                            line=0;
                            isempty=1;
                        }
                        else
                            if (c=='\n'&& isempty==0)
                            {   
                                buffer[i]='\0';
                                fprintf(tmp,"%s",buffer);
                                i=0;
                                isempty=1;
                            }
                    }
                     
                 
                }
                            filecopy(tmp,f);
                            fclose(tmp);
            }
                    else
                    {
                            printf("Unable to create tmp file\n");
                     }
     
             
            fclose(f);
             
     
        }
        else
        {
            printf("Unable to open file\n");
        }
     
    }
     
     
    void filecopy(FILE *from, FILE *to)
    {
        int c;
        rewind(from);
        rewind(to);
        while ((c=getc(from))!=EOF)
        {   
            putc(c,to);
        }
        while(!feof(to))
        {
            fprintf(to," ");
        }
     
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    Basically, it reads the file character by character and compares it to a space (' '), tab ('\t') and newline ('\n'). There is also a flag called isempty that pretty much controls what lines are copied into a buffer that is written to a temp file, and then copied to a persistent file.

    For example, these lines are considered blank.

    Nullam eu \n
    \t \t\n
    enim id nunc\n
    \n
    mattis vehicula quis et urna.\n
    \n
    \n

    By the way the code isn't really that good and it has mistakes. Now that you know what it does, write your own, and don't use feof() like this code does.
    Last edited by whiteflags; 05-30-2016 at 07:50 PM.

  3. #3
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Thank you so much. Yes I will write my own code. I think I'll understand how to do it a lot better that way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. older posts
    By eloy203 in forum General Discussions
    Replies: 2
    Last Post: 01-31-2015, 10:59 AM
  2. Older versions of linux
    By überfuzz in forum Linux Programming
    Replies: 2
    Last Post: 01-27-2014, 06:50 AM
  3. One-time pad older than thought
    By hk_mp5kpdw in forum General Discussions
    Replies: 2
    Last Post: 07-30-2011, 06:02 PM
  4. does the mind change as you get older?
    By Anddos in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 12-12-2005, 12:10 PM
  5. Faster directx on older pcs
    By Josh Kasten in forum Game Programming
    Replies: 15
    Last Post: 01-25-2003, 08:32 AM

Tags for this Thread