Thread: Changing filename to all capitals

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    13

    Changing filename to all capitals

    Good Evening, I have an assignment that is asking me to:

    Have a program prompt the user for a filename to open. Change every alphabetic character in the file to a capital letter. Numbers and special characters should not be changed. Print the output to the screen.

    I am having a little trouble conceptualizing the code I need. I am assuming that I will have to pass the file name into an string and then from there use the string to update all of the characters. I am not sure where to start. Any help will be greatly appreciated, Thanks in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should get a filename to open.
    Open that file.
    Process the characters in the file, changing to uppercase as appropriate.
    Write the new characters to the screen, either as they are processed or all at the end, as you desire.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Pass the file name into a string? What are you talking about? This is ridiculously easy.

    First write a program to do this:

    1. Ask the user for the filename.
    2. Print the filename back to the user and confirm you did it all right.

    Hint: Use fgets(), not gets() if you don't want to get virtually beat up by angry programmers.

    Secondly:

    1. Add the ability to open the file and print the contents to the screen.

    Hint: Don't overcomplicate this. Write basic code.

    Thirdly:

    1. Modify your print code to change each character that it needs to change to uppercase.

    Hint: There is a function to do this kind of thing.

    This should get you started.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    So far I have gotten the program to open the selected file and display the contents but I am unable to convert the contents to capital letters, I get an array of strange character and number combinations. Any pointers or guidance on what I need to add to this code to get it to function properly.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
        FILE *inFile;
        FILE *outFile;
        char fileName[13],line[256], descrip[10];
        
        printf("\nEnter a file name:");
        gets(fileName);
        inFile=fopen(fileName, "w+");
        outFile=fopen(fileName, "w+");
        
        if (inFile==NULL)
        {
                         printf("\nThe file &#37;s was not successfully opened.", fileName);
                         printf("\nPlease check that the file curretly exists.\n");
                         exit(1);
                         }
        printf("\nThe file has been sucecessfully opened for reading.\n");
        while (fgets(line,256,inFile) !=NULL)
    toupper(line);    
    fputs(line, inFile);
         printf("%s", line);
         fclose(inFile);
    printf("\nPress any key to exit.....");
    getchar();
        return 0;
    }
    Last edited by cosmiccomputing; 06-16-2008 at 02:08 AM.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Can you offer any guidance to assist me?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    How about treating us seriously? Shall I begin to list why your code sucks?

    • You use gets() like I told you not to.
    • You open the input file for writing.
    • You write to the input file.
    • You only close one file.
    • You didn't even read the documentation on toupper().
    • Your indentation sucks.


    That might be a good starting place for you to start fixing things. Your code shouldn't even compile in its current form.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MacGyver View Post
    That might be a good starting place for you to start fixing things. Your code shouldn't even compile in its current form.
    Actually, it probbaly does, but with warnings enabled it would give quite a few warnings. I suggest you enable warnings on the compiler you are using:
    • gcc: -Wall
    • MS visual C: -W3

    For other compilers, refer to the relevant manual.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're right. MinGW compiles it, but only because he forgot the header file for toupper(). If he bothered to include it, it would choke even with no warnings.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Code:
    gets(fileName);
    Use fgets. It's safer. Just pass stdin into the FILE* argument.
    http://www.cplusplus.com/reference/c...dio/fgets.html

    Code:
    inFile=fopen(fileName, "w+");
    To read a file you need to use the mode "r". According to http://www.cplusplus.com/reference/c...dio/fopen.html "w+" means you can both read and write to the file, but when you open it, it's contents are erased...making it pointless to read an existing file.

    Code:
    if (inFile==NULL)
        {
                         printf("\nThe file &#37;s was not successfully opened.", fileName);
                         printf("\nPlease check that the file curretly exists.\n");
                         exit(1);
                         }
    Why use exit(1) when you can use return?

    Code:
    while (fgets(line,256,inFile) !=NULL)
        toupper(line);
    Unfortunately it's not going to be that simple. toupper() does not convert a whole line to uppercase. It takes ONE character, and returns the uppercase for that character. You're going to need two loops. The first loop, which you already have will read each line, the next loop should be INSIDE the previous loop, and should iterate through each character in the line and convert it to uppercase like this:

    line[i] = toupper(line[i]);

    Where i is the index of the character.

    Code:
    fputs(line, inFile);
    Let's say some how your code worked and it could convert each line to uppercase. It still wouldnt output every line. It would only output the final line because you have not used {} for your while loop, which means only the first statement after the loop "toupper(line);" would be executed each iteration. fputs would only be executed once, after the loop has finished. To execute multiple statements inside a loop you need to use curly braces:

    Code:
    while(...)
    {
      ...
      ...
    }
    Last edited by 39ster; 06-16-2008 at 02:45 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. Pass Filename to another function
    By awesmesk8er in forum C Programming
    Replies: 9
    Last Post: 10-24-2008, 01:43 PM
  4. Changing filename extension
    By slippy in forum C Programming
    Replies: 3
    Last Post: 05-03-2007, 11:04 AM
  5. Replies: 3
    Last Post: 01-25-2006, 10:30 PM