Where am I going wrong (file open,changecase,write)

Discussion in 'C Programming' started by barnetod@gmail.com, Jan 9, 2007.

  1. Guest

    I am trying to open a text file designated by the user.

    Then I want to change all lower case values to capital letters.

    Then write file.

    I am stuck and can not change the characters or am not writing
    correctly.
    Please let me know what I can do to fix this.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>

    #define MAXCHAR 250
    int main(void)
    {
    int c; /* for return value of fgetc */
    FILE *fp;
    char filename[MAXCHAR];

    printf("Enter the name of the file to open\n");
    fgets(filename, MAXCHAR, stdin);
    filename[strlen(filename) - 1] = '\0';

    if (!(fp = fopen(filename, "r")))
    {
    printf("Unable to open %s\n", filename);
    exit(EXIT_FAILURE);
    }

    while((c = fgetc(fp)) != EOF)
    {
    if(islower((unsigned char)c))
    {
    c=toupper(c);
    }
    fclose(fp);
    }


    printf("The file has been converted to all Capital letters.\n\n\n");
    system("Pause");
    return 0;

    }
    , Jan 9, 2007
    #1
    1. Advertising

  2. In article <>,
    <> wrote:
    > while((c = fgetc(fp)) != EOF)
    > {
    > if(islower((unsigned char)c))
    > {
    > c=toupper(c);
    > }
    > fclose(fp);
    > }


    Wouldn't it be best to write the characters out again? And wait until
    after reading all the characters before closing the file?

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Jan 10, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    282
    wahaha
    Jan 10, 2007
  2. barnetod
    Replies:
    2
    Views:
    243
    CBFalconer
    Jan 10, 2007
  3. scad
    Replies:
    4
    Views:
    942
    James Kanze
    May 28, 2009
  4. Replies:
    4
    Views:
    123
  5. Iulian Ilea
    Replies:
    1
    Views:
    293
    pcx99
    Dec 21, 2006
Loading...

Share This Page