john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

file remove linefeed characters

/* John Pfeiffer, 10jan09 a quick prog for my wife to remove linefeeds (enters, /n) from the end of lines EXCEPT when there are 2 lines feeds (e.g. a paragraph break) */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])
{
    char c,d;
    char outputfile[128]="no-linefeeds-";
    FILE* ifp;
    FILE* ofp;

    ifp = fopen(argv[1],"r");
    strcat(outputfile, argv[1]);
    ofp = fopen(outputfile,"w");

    if( (ifp == NULL) || (ofp == NULL))
    {    printf("\nERROR opening file, try remove-linefeed-characters filename.txt\n");
        exit(1);
    }

    c = getc(ifp);
    while( c!= EOF)
    {
        if( c == '\n')
        {   
            d = getc(ifp);
            if( d == '\n')
            {
                putc('\n',ofp);
                putc('\n',ofp);
            }
            else
            {
                putc(' ',ofp);
                putc(d,ofp);
            }
        }
        else
        {   putc(c,ofp);
        }
        c = getc(ifp);
    }

    fclose(ifp);
    return 0;
}

  • « mem size
  • win msg box wince »

Published

Feb 6, 2010

Category

c

~93 words

Tags

  • c 95
  • characters 5
  • file 92
  • linefeed 1
  • remove 16