Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Skipping specific characters in files in C (http://www.go4expert.com/forums/skipping-specific-characters-files-c-t20430/)

nimsalwayz 18Dec2009 14:15

Skipping specific characters in files in C
 
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.. :nonod:

venami 18Dec2009 16:16

Re: Skipping specific characters in files in C
 
What is the function that you use to read the text file?

Welcome to the forum :)

nimsalwayz 18Dec2009 18:22

Re: Skipping specific characters in files in C
 
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 :D

venami 18Dec2009 20:54

Re: Skipping specific characters in files in C
 
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 18Dec2009 22:29

Re: Skipping specific characters in files in C
 
thank u! You are a lifesaver! :D :happy:

venami 19Dec2009 07:01

Re: Skipping specific characters in files in C
 
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.


All times are GMT +5.5. The time now is 03:44.