Thread: ASCII Exceptions problem

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    28

    ASCII Exceptions problem

    As part of my project I am getting my program to counter all characters within a text file, and it is doing this with little trouble. But I want the counter not to increase when it encounters blanks and new line which have the corresponding ASCII vlaues of '32' and '10'

    This can be done in two ways

    1. Either get program to ignore all ASCII values of '10' and '32'

    2. Count all characters but when the program encounters '10' and '32' decrement the counter by 1

    Here is my code but I am having problems

    Code:
    while(!feof(stream))
          {
    	fscanf(stream, "%c", inchar != '10' || '32'); 
    	*string = inchar; 
    	totalchars++; 
          }
    Any help would be much appreciated thanx
    Cheers

    Bazza

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I thought I'd given you the answer?

    Please don't create multiple threads on the same subject.

    If you have questions on my sample code, just ask.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    Sorry Hammer I just wanted to try different techniques to se which proved to be the best

    thanx
    Cheers

    Bazza

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    [Quote]
    I thought I'd given you the answer?

    Please don't create multiple threads on the same subject.

    If you have questions on my sample code, just ask.

    [\quote]

    The reason im trying it this way is because the answer you tried to help me out with is wrong. This expels blanks but not newline characters

    All your help still appreciated though

    thanx hammer
    Cheers

    Bazza

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >The reason im trying it this way is because the answer you tried to help me out with is wrong. This expels blanks but not newline characters
    No, it's not wrong, it's incomplete, like I said:
    Hammer wrote...
    I won't write the whole thing for you, here's a snippet that will do as I have suggested.
    Here's another go:

    For reading the file in, you can use these methods:

    1. Use fread() to load the file into memory, either as whole or in blocks.
    2. Use fgetc() to get one character at a time.
    3. Use fgets() to grab a line at a time.

    For each byte read, say something like

    >if (c != ' ' && c != '\n' && c != '\r' && c != '\t') Counter++;

    Or use strchr(), like this:
    >#define IGNORE " \n\r\t"
    >if (strchr(IGNORE, c) == NULL) Counter ++

    Or, use isalnum() to determine if c is alphanumeric.
    Last edited by Hammer; 07-10-2002 at 07:59 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    Thanx hammer most appreciated your help has been great couldnt of asked for anymore. Cheers again if only I knoew some of the **** you did.

    P.s. Just out of interest if i wanted to block out certain ASCII values

    [code] if (c != ' ' && c!= '\n') totalchars++; [\code]

    what would i enter instead.

    Thanx again if you want a program that does a char count line count and word count you can have it. Even though you could probably make one in 5 mins

    cheers
    Cheers

    Bazza

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Thanx hammer most appreciated your help has been great couldnt of asked for anymore.
    No problem!

    >if only I knoew some of the **** you did.
    You will, just keep learning!

    >P.s. Just out of interest if i wanted to block out certain ASCII values what would i enter instead.
    You can represent each character as it's numeric value (like 0x20 for a space), but it's not recommended, because of portability issues. For example, if you used 0x20 to represent a space, it wouldn't work on an EBCDIC OS, 'cos a space is 0x40 on there.

    You almost got the code tags thing right, you just used the wrong slash (check my sig for an example).

    Have fun
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exceptions problem
    By sd03eee in forum C++ Programming
    Replies: 4
    Last Post: 11-02-2005, 06:12 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. array input/output problem
    By glowstick in forum C Programming
    Replies: 1
    Last Post: 09-10-2002, 07:56 PM