Capitalize first letter of every word in .txt file

This is a discussion on Capitalize first letter of every word in .txt file within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <ctype.h> int main(void) { FILE* fin; FILE* fout; char inputfname[256]; char outputfname[256]; char currentchar; char previouschar ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    25

    Capitalize first letter of every word in .txt file

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
        FILE* fin;
        FILE* fout;
        char inputfname[256];
        char outputfname[256];
        char currentchar;
        char previouschar = ' ';
    
        printf("Enter the input filename\n");
        scanf("%s", inputfname);
        printf("Enter the output filename\n");
        scanf("%s", outputfname);
    
        fin = fopen(inputfname, "r");
        fout = fopen(outputfname, "w");
    
        while(!feof(fin))
        {
            currentchar = fgetc(fin);
            if(!isalpha(previouschar) && isalpha(currentchar))
            {
                currentchar = toupper(currentchar);
                fputc(currentchar, fout);           
            }
            currentchar = previouschar; 
        }
    
        system("PAUSE");
        return 0;
    }
    As the title says. I can't get this to work. I know I'm making a stupid mistake somewhere and I can't find it. And yea I know system("PAUSE"); shouldn't be used but it's what my prof. insists on.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    How does it not work?

    Incidentally, you should #include <stdlib.h> for system() and feof() should not be used to control a loop - use the result of fgetc() instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    It capitalizes every letter in the .txt file.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    You probably want to use fputc() on every char that is read, not just those that are capitalised.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    And you also want
    Code:
    previouschar = currentchar;
    instead of
    Code:
    currentchar = previouschar;
    I hate real numbers.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    Well I forgot to close the files ;/ but that was never the problem anyway. *Sigh* I know what you mean about fputc for every char, I just can't figure out how to implement it.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    I know what you mean about fputc for every char, I just can't figure out how to implement it.
    Move that line to after the if block.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Additionally, read the FAQ on how to use feof() correctly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    Thanks guys I figured it out now. Sometimes the simplest problems can be a real pain lol.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Last week a "hello world" question ended up being a 100+ post thread. So don't feel too bad.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

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