1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Skipping specific characters in files in C

Discussion in 'C' started by nimsalwayz, Dec 18, 2009.

  1. nimsalwayz

    nimsalwayz New Member

    Joined:
    Dec 18, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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:
     
  2. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    What is the function that you use to read the text file?

    Welcome to the forum :)
     
  3. nimsalwayz

    nimsalwayz New Member

    Joined:
    Dec 18, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  4. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    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;
    }
    
     
  5. nimsalwayz

    nimsalwayz New Member

    Joined:
    Dec 18, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    thank u! You are a lifesaver! :D :happy:
     
  6. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    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.
     

Share This Page