help with fgetc

This is a discussion on help with fgetc within the C Programming forums, part of the General Programming Boards category; Is it possible to make fgetc skip to a new line once it encounters a certain character? Here is my ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    67

    help with fgetc

    Is it possible to make fgetc skip to a new line once it encounters a certain character? Here is my code so far:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    FILE *gfopen (char *fn, char *mode)
    {
        FILE *fp;
        
        if ((fp = fopen (fn, mode)) == NULL){
            fprintf (stderr, "Cannot open %s - bye\n", fn);
            exit (1);
        }
        return fp;
    }
    
    int main (int argc, char **argv)
    {
        int c;
        FILE *fp, *tmp_fp;
        char array[50];
        
        fp = gfopen (argv[1], "r+");
        tmp_fp = tmpfile();
        while ((c = getc(fp)) != EOF)
            if ( c != ' '){
                putc (c, tmp_fp);
            }
        rewind (tmp_fp);
        while ((c = getc (tmp_fp)) != EOF)
            printf("%c", c);
        printf("\n");
        return 0;
    }
    Whilst copying a text file into a temporary binary file I am removing all the spaces so if the text file contains:
    Code:
    Line 1: Example Example Example 
    
    Line 3: Example Example Example
    it comes out like this:
    Code:
    Line1:ExampleExampleExample
    
    Line3:ExampleExampleExample
    which is what I want, but if I were to read from the binary file again, how can I make it read Line 1 and then skip straight to Line 3, if it sees that Line 2 is empty?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Binary files, by definition, are not text-files, and as such do not have lines. You can of course "define" a meaning to lines in a binary file - but your question is a bit strange in my opinion.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    Ok then, suppose instead of a binary file I simply copy characters from one text file into another, whilst removing spaces between them (but blank lines still present).

    My problem is that if the text file contains a complete blank line, then if I were to copy that line into an array(using fgetc), it would end up being filled with whitespaces, and writing a function that compares each array element to see if all of them contain whitespaces is, in my opinion a bit tedious. I tried various approaches but none were very successful. At the end of the day my goal is simple - to read in a text file whilst ignoring blank lines. I need some tips and advice on how to do that.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, essentially what you want to do is that you skip a newline if that was the last thing written out? or read in, perhaps?

    something like this:
    Code:
    ...
    char prev = 0;
    ...
        while ((c = getc(fp)) != EOF)
            if ( c != ' ' && (c != '\n' || prev != '\n'){
                putc (c, tmp_fp);
                prev = c;
            }
    ...
    This may not do EXACTLY what you wanted, but I think it will give you the general idea.

    I would probably, actually, use fgets() to read a line at a time, strip out the spaces, and then see if the length of the string is 1 (only newline), and skip those single line strings on output. It may not be simpler, but if you want to deal with lines, then deal with lines.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. manipulating fgetc while reading a file
    By agentsmith in forum C Programming
    Replies: 1
    Last Post: 04-10-2008, 01:52 PM
  2. character input using fgetc() code???
    By lesrhac03 in forum C Programming
    Replies: 3
    Last Post: 03-27-2008, 10:55 PM
  3. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:33 PM
  4. About getc and fgetc. Please help.
    By Antigloss in forum C Programming
    Replies: 4
    Last Post: 09-28-2005, 04:00 AM
  5. fgetc
    By linuxdude in forum C Programming
    Replies: 3
    Last Post: 03-03-2004, 08:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21