follow up with char I/O and line, word, and char counting

This is a discussion on follow up with char I/O and line, word, and char counting within the C Programming forums, part of the General Programming Boards category; When i compile my program only the character counts seems to be returning the proper value, but line and word ...

  1. #1
    Registered User Led Zeppelin's Avatar
    Join Date
    Mar 2002
    Posts
    17

    follow up with char I/O and line, word, and char counting

    When i compile my program only the character counts seems to be returning the proper value, but line and word count return 0 and 1 respectivley. From my previous post I understand that the program didnt see the hard returns, so my question is this:

    I typed the gettysburg address in notepad and saved it as a text file on the a:\drive. Is notepad not capable of displayed the 'hard return' or is c unable to read 'hard returns' from note and it is better to use another editor like 'wordpad'?

    Here is a copy of my code;



    [code]

    #include <stdio.h>
    #include <conio.h>

    #define WHT_SPC (symbol == " " || symbol == '\n' || symbol == '\t')



    main()
    {


    char symbol;
    int preSymbol;
    int lineCount = 0;
    int symbolCount = 0;
    int wordCount = 0;
    char word = 'O';
    FILE *gettyadd;

    clrscr();

    if (!(gettyadd = fopen("A:\\gettyadd.txt", "r")))
    }
    printf("Unable to open file for reading");
    getch();
    return (1);
    }

    while ((symbol = fgetc(gettyadd)) != EOF)
    {
    if (symbol != '\n')
    symbolCount++;
    else
    lineCount++;
    }

    if (preSymbol != '\n')
    lineCount++;

    while ((symbol = fgetc(gettyadd)) != EOF)
    {
    if (WHT_SPC)
    word = 'O';
    else
    if (word == 'O')
    {
    wordCount++;
    word = 'I';
    }
    }

    printf("The number of characters is: %d\n", symbolCount);
    printf("The number of lines is: %d\n", lineCount);
    printf("The number of words is: %d\n", wordCount);
    fclose(gettyadd);

    getch();
    return 0;

    }
    [\code]

    Thanks for your time.

  2. #2
    Nit
    Nit is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    42
    correct me if I'm wrong but doesn't DOS and UNIX treat the hard return as different values?

    I'm not exactly sure what the value would be for DOS, but I'm sure you could search for it easily if I am indeed correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game and strcmp
    By crazygopedder in forum C Programming
    Replies: 12
    Last Post: 11-23-2008, 05:13 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  5. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21