Thread: having trouble opening file and converting all lowercase letters to uppercase letters

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    1

    having trouble opening file and converting all lowercase letters to uppercase letters

    I'm writing a program that asks user to enter a file to be opened and then convert all lower case letters to uppercase letters and then display the file. the way i have my program write now, it doesnt seem to go go into the toUpper function and when it displays the file, my program crashes. Please help, Thank you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        FILE *inFile;
        char fileName[30];
        char toUpper(char);
        
        printf("\nPlease enter the file to be opened: ");
        gets(fileName);
        inFile = fopen(fileName, "r");
        
        
        if (inFile == NULL)
        {
                   printf("\nThe file %s was not successfully opened.", fileName);
                   printf("\nPlease check that you have typed in the correct file.\n");              
                   system("PAUSE");
                   exit(1);
        }
        
        printf("\nThe file has been opened\n");
        system("PAUSE");
        
        void convert(char fileName[])
        {
             int i=0;
             
             while (fileName[i] != '\0');
             {
                   fileName[i] = toUpper(fileName[i]);
                   i++;
             }
        }
        
        while (fscanf(inFile,"%s", fileName) != NULL)
        printf("%s", fileName);
       
      
      system("PAUSE");    
      return 0;
    }
    
    
    char toUpper(char letter)
    {
         if( letter>= 'a' && letter <= 'z')
             return (letter - 'a' +'A');
         else
             return (letter);
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    How are you even getting a build? You must not be compiling according to any standard:
    Code:
    C:\Users\Josh2>gcc -Wall -ansi -pedantic jeff.c
    jeff.c: In function 'main':
    jeff.c:27:5: warning: ISO C forbids nested functions [-pedantic]
    jeff.c:27:5: warning: ISO C90 forbids mixed declarations and code [-pedantic]
    jeff.c:38:42: warning: comparison between pointer and integer [enabled by defaul
    t]
    Move the convert function out of main()'s scope.
    Then call convert in main() instead.
    Stop using filename to do everything; that makes it really confusing to read the code.

    Unless you have to make your own toUpper, I would just use the one in the ctype.h library.

    Edit: Also, be careful of mistakes like this.
    Code:
      while (fileName[i] != '\0');
    Last edited by whiteflags; 09-11-2013 at 10:50 PM.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-04-2013, 12:22 PM
  2. converting a string array into uppercase letters from file
    By thesyntaxdr in forum C Programming
    Replies: 6
    Last Post: 04-20-2013, 08:37 AM
  3. Counting lowercase letters in string
    By askinne2 in forum C Programming
    Replies: 14
    Last Post: 09-29-2010, 02:58 PM
  4. Find word with uppercase and lowercase letters
    By doia in forum C Programming
    Replies: 9
    Last Post: 07-15-2010, 08:51 PM
  5. Counting uppercase and lowercase letters in a text
    By Tintu in forum C Programming
    Replies: 2
    Last Post: 02-06-2008, 10:15 PM