Jump to content

  • Log In with Google      Sign In   
  • Create Account


Continue reading after set indicators? C FILE I/O


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 iGoogleThis   Members   -  Reputation: 205

Like
0Likes
Like

Posted 14 July 2013 - 12:08 PM

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, 14 July 2013 - 12:13 PM.

Chris LeJohn

Build Engineer (RE)
Gnovahex Computing


Sponsor:

#2 Bleys   Members   -  Reputation: 171

Like
0Likes
Like

Posted 14 July 2013 - 01:33 PM

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.


#3 iGoogleThis   Members   -  Reputation: 205

Like
0Likes
Like

Posted 14 July 2013 - 02:33 PM

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.


Chris LeJohn

Build Engineer (RE)
Gnovahex Computing


#4 Bleys   Members   -  Reputation: 171

Like
0Likes
Like

Posted 14 July 2013 - 02:38 PM

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.


#5 iGoogleThis   Members   -  Reputation: 205

Like
0Likes
Like

Posted 14 July 2013 - 05:11 PM

 

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!


Chris LeJohn

Build Engineer (RE)
Gnovahex Computing





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS