fgetc to skip from point to new line

2.6k views Asked by At

I am trying to get fgetc to read through a file and skip from a certain indicator until a new line. This seems like a simple question, but I can't find any documentation on it.

Here is an example of my question:

read this in ; skip from semicolon on to new line

My best guess at a solution would be to read in the entire file, and for each line use strtok to skip from ; to the end of the line. Obviously this is horrible inefficient. Any ideas?

*I need to use fgetc or something like fgetc that will parse the file character by character

3

There are 3 answers

2
woolstar On BEST ANSWER

Easiest thing to do is read the entire line in, then truncate if there a ;.

char buffer[1024], * p ;

if ( fgets(buffer, sizeof(buffer), fin) )
{
  if (( p= strchr( buffer, ';' ))) { *p = '\0' ; }  // chop off ; and anything after
  for ( p= buffer ; ( * p ) ; ++ p )
  {
    char c= * p ;
    // do what you want with each character c here.
  }
}

When you do the read, buffer will initially contain:

"read this in ; skip from semicolon on to new line\n\0"

After you find the ; in the line and stick a '\0' there, the buffer looks like:

"read this in \0 skip from semicolon on to new line\n\0"

So the for loop starts at r and stops at the first \0.

2
Jonathan Leffler On

Given a requirement to use fgetc(), then you are probably supposed to echo everything up to the first semicolon on the line, and suppress everything from the semicolon to the end of the line. I note in passing that getc() is functionally equivalent to fgetc() and since this code is about to read from standard input and write to standard output, it would be reasonable to use getchar() and putchar(). But rules are rules...

#include <stdio.h>
#include <stdbool.h>

int main(void)
{
    int c;
    bool read_semicolon = false;

    while ((c = fgetc(stdin)) != EOF)
    {
        if (c == '\n')
        {
            putchar(c);
            read_semicolon = false;
        }
        else if (c == ';')
            read_semicolon = true;
        else if (read_semicolon == false)
            putchar(c);
        /* else suppressed because read_semicolon is true */
    }
    return 0;
}

If you don't have C99 and <stdbool.h>, you can use int, 0 and 1 in place of bool, false and true respectively. You can use else if (!read_semi_colon) if you prefer.

0
BLUEPIXY On
//Function of compatible fgets to read up to the character specified by a delimiter.
//However file stream keep going until to newline.
//s : buffer, n : buffer size
char *fgets_delim(char *s, int n, FILE *fp, char delimiter){
    int i, ch=fgetc(fp);

    if(EOF==ch)return NULL;
    for(i=0;i<n-1;++i, ch=fgetc(fp)){
        s[i] = ch;
        if(ch == '\n'){
            s[i+1]='\0';
            break;
        }
        if(ch == EOF){
            s[i]='\0';
            break;
        }
        if(ch == delimiter){
            s[i]='\0';//s[i]='\n';s[i+1]='\0'
            while('\n'!=(ch = fgetc(fp)) && EOF !=ch);//skip
            break;
        }
    }
    if(i==n-1)
        s[i] = '\0';
    return s;
}