Skipping specific characters in files in C

nimsalwayz's Avatar, Join Date: Dec 2009
Newbie Member
Hi,
I have been trying unsuccessfully to read in a text file and skip over specific characters - something like what C++ does with cin.ignore().
I need to skip over characters like ';' and '->'.

I hope someone can help me..
venami's Avatar, Join Date: Dec 2008
Ambitious contributor
What is the function that you use to read the text file?

Welcome to the forum
nimsalwayz's Avatar, Join Date: Dec 2009
Newbie Member
I am using the fscanf function.
An example of a line from the file I am reading from is 1->2;
And I want to read the 1 and 2 , ignoring the '->' and ';'

And thank you so much for the welcome.. I really hope you guys can help
venami's Avatar, Join Date: Dec 2008
Ambitious contributor
If you use getc(), then you can check the variable and omit from your manipulation like this:

Code:
#include <stdio.h>

int main()
{
  char c;
  FILE *fp;

  fp = fopen("sample.txt","r");
  while((c = getc(fp)) != EOF)
    {
      if(c == ';' || c == '-' || c == '>')
        continue;
      else
        printf("%c", c);
    }

  fclose(fp);

  return 0;
}
nimsalwayz's Avatar, Join Date: Dec 2009
Newbie Member
thank u! You are a lifesaver!
venami's Avatar, Join Date: Dec 2008
Ambitious contributor
Welcome.

In case your input file contains the '-' and '>' characters, they will also be omitted from reading along with the '->' symbol. I have given you an idea of how to do the skipping of characters, you should take care of the case that I mentioned here while omitting the characters.