Continue reading after set indicators? C FILE I/O

Started by
3 comments, last by Alternate-E 10 years, 9 months ago

I am running Linux and what I am trying to do here is make a program that will start and stop after reaching specific indicators such as '}' would be an indicator for closing a statement in some programming languages. So for example, if I have:


void main (void)
{
    FILE *fp;
    fp = fopen("readthis.txt", "r");

    int ch = fgetc(fp)

    while (ch != EOF)
   {
        fprintf(fp, "%c", ch);
   }
}

    

How would I pick and choose what does get read and what doesn't? I didn't want to just throw in strcmp's and cross my fingers. Maybe the solution is simple, thanks in advance. Let's assume I want to start reading after every asterisk (*) and read until the end-of-file. Also, lets say the file contained:


This doesn't get read *

This gets read.

Editor // Joy-Toilet.com

Anything But Shitty Entertainment!

Advertisement

I don't really understand what the problem is.

You can read byte by byte and check for the asterisk, and continue reading and storing data.
If you want faster performance, you could read the file in chunks or the entire file at once.

It's only when you look at ants with a magnifying glass on a sunny day, that you realise just how often they burst into flames.

I don't really understand what the problem is.

You can read byte by byte and check for the asterisk, and continue reading and storing data.
If you want faster performance, you could read the file in chunks or the entire file at once.

Perhap my understanding of the fgets function is abit slim however if you could elaborate or maybe give me an example that would be badass. The problem is that I don't know how to check for an asterisk, safe to say I'm not used to doing elaborate file I/O routines. I have ideas but a well explained solution would be much more helpful.

Editor // Joy-Toilet.com

Anything But Shitty Entertainment!

Well something like this should work:


bool useDataFromNowOn = false;

while(!feof(fp))
{
    int ch = fgetc(fp);

    if(useDataFromNowOn)
    {
        ... do something with ch
    }

    if(ch == '*')
    {
        useDataFromNowOn = true;
    }
}

It's only when you look at ants with a magnifying glass on a sunny day, that you realise just how often they burst into flames.

Well something like this should work:


bool useDataFromNowOn = false;

while(!feof(fp))
{
    int ch = fgetc(fp);

    if(useDataFromNowOn)
    {
        ... do something with ch
    }

    if(ch == '*')
    {
        useDataFromNowOn = true;
    }
}

You are mighty, thanks!

Editor // Joy-Toilet.com

Anything But Shitty Entertainment!

This topic is closed to new replies.

Advertisement