0

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

AlexK
  • 336
  • 8
  • 21
  • 41
  • What do you do with the part of the string you want to keep? read it in character by character? – woolstar Dec 06 '13 at 22:17
  • 1
    Is there a reason not to use `fgets` to read the entire line at a time? – woolstar Dec 06 '13 at 22:18
  • With the text I keep I want to ideally read it in string by string (ie, character array seperated by whitespace). fgets can read the entire line up until a specified line length, but I am looking to read the characters up until ";" – AlexK Dec 06 '13 at 22:21

3 Answers3

1

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.

woolstar
  • 5,063
  • 20
  • 31
  • the problem with doing this is that I need to use fgetc. I can only use fgetc on a file, as there is no "sgetc" in c, if I'm not mistaken? Thanks! – AlexK Dec 06 '13 at 22:31
  • That's because you don't need `sgetc`, I will expand my example. – woolstar Dec 06 '13 at 22:32
0

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • this works but the problem is in order to use it I would need to make a new file to reprint everything minus the ;->. I might do this. – AlexK Dec 06 '13 at 22:58
  • If you're not constrained to use `fgetc()`, which you aren't because you've accepted an answer that doesn't use `fgetc()`, then you don't need this code. The trouble is, your question was incomplete; you did not state what you needed to do with the data before the semicolon. Echoing to output is a simple option; stashing into an array that can be processed by some other function requires some modification of the code above. Incomplete specifications get incomplete answers; they're also apt to irritate people who try to help you. (I'm not too fussed; it didn't take me long to write that!) – Jonathan Leffler Dec 06 '13 at 23:15
0
//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;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70