Sign in to follow this  
Alternate-E

Continue reading after set indicators? C FILE I/O

Recommended Posts

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.
Edited by iGoogleThis

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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;
    }
}

Share this post


Link to post
Share on other sites

 

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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this