Opening File Changing Case and Closing HW Help Code Included.

This is a discussion on Opening File Changing Case and Closing HW Help Code Included. within the C Programming forums, part of the General Programming Boards category; Thanks again ahead of time. I am looking to write a program that opens a text file, takes the text ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    28

    Opening File Changing Case and Closing HW Help Code Included.

    Thanks again ahead of time.

    I am looking to write a program that opens a text file, takes the text and converts all characters (excluding special and numbers) to capital letters.

    I am unsure where I am going wrong for some reason I am using d, and cannot get it to code correctly keep getting char to char error. Please let me know what I have messed up as I am a little confused.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAXCHAR 250
    int main(void)
    {
       FILE *fp;
       char filename[MAXCHAR];
       char d;
      
       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);
       }
    
      else
      {
           fgets(d,MAXCHAR,fp);
            if (fp > 'a' && fp < 'z')
            {
              fp = tolower(fp);
            }
            fputs(fp);
            }
    
       system("Pause");
       return 0;
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What exactly are you trying to fputs? Even if that were correct, fgets has already moved the file pointer. So even if it were writing, it would be writing past the point you want it to. If you plan on doing this with one file, in one shot, you should:
    Code:
    read one character
        test it
            if it's to be replaced
                seek back one character
                write out the new character
    Something like that. Also fp is a pointer to the file handle. You can't test it to see if it is a character. You simply read from and write to it. You don't test it directly usually for anything but NULL to see if it's failed to open or not.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    28
    Is this any better?

    I am still getting an error at my if statement. I was unfortunately given the worst textbook on earth which provide no examples which relate anything to our assignments both graded and practice.

    I am having to read wikipedia and here to learn to program in small segments.

    i appreciate the help. I am just somewhat lost on this topic i guess the concept of counting within a file is confusing the heck out of me.

    any examples would be great.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAXCHAR 250
    int main(void)
    {
       FILE *fp;
       char filename[MAXCHAR];
       char d;
      
       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);
       }
    
      else
      {
           fgets(d,MAXCHAR,filename);
            if (d > 'a' && d< 'z')
            {
              d = tolower(d);
            }
            }
    
       system("Pause");
       return 0;
    
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you're trying to read the entire file, you'll need some form of loop. Try writing a program that simply reads a file one character at a time until it has reached the end of file.

    Once you're able to do this, try something like just printing out the lower case letters as you come across them, ignoring the rest. Then consider opening a file, doing the above, but instead of ignoring them all, writing the letters out to a new file as you reach tme.

    Take small steps.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    Erm, so why not just open an output file and just do read from one file and write to another?

    Try that first, before trying in-place changes to a file.
    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.

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Wouldn't you want toupper() since you want caps ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    28
    yes i was being dumb with that.

    I program in the AML language for robotics and doing that at work all day and trying to switch to C for school sucks.

    Luckily this assignment isn't graded but i still need to complete tonight

    I am hoping i can get it done we shal see.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    28
    okay here is an update

    i guess my loop writing is very week.

    Code:
    #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 */
       int inword = 0;
       FILE *fp;
       char filename[MAXCHAR];
       unsigned int count = 0;
    
       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))
          {
             toupper(c);
          }
          }
       fclose(fp);
    
       printf("The file has been converted to all Capital letters.\n\n\n");
       system("Pause");
       return 0;
    
    }
    let me know what i am missing. I am hoping I got it to atleast open the file and close it. I am just having a problem scanning through and changing. maybe i am missing something small let me know.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    - open input file for reading
    - open output file for writing
    - while((c = fgetc(fp)) != EOF)
    -- if ( islower(c) ) c = toupper(c)
    -- write to output file
    - end while
    - close input
    - close output
    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.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    28
    here is an update I am at least writing to file now. except my output is MAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA and so on.

    Code:
    #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, *fp2;
       char filename[MAXCHAR];
       
       printf("Enter the name of the file to open\n");
       fgets(filename, MAXCHAR, stdin);
       
       filename[strlen(filename) - 1] = '\0';
       fopen(filename,"r+");
       
    
       if  (!(fp = fopen(filename, "r+")))
       {
          printf("Unable to open %s\n", filename);
          exit(EXIT_FAILURE);
       }
    
       while((c = fgetc(fp)) != EOF)
       {
          c=toupper(c);
          fseek(fp,-1L,SEEK_SET);
          fputc(c,fp);
          }
          fclose(fp);    
    
       printf("\n\nThe file has been converted to all Capital letters.\n\n\n");
       system("Pause");
       return 0;
    
    }

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I avoid update mode because its a PITA. Can you read/write to separate files? Or use a temp file and rename?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    Note changes
    Code:
    #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';
    
       /*!! fopen(filename,"r+"); */
    
       if  (!(fp = fopen(filename, "r+")))
       {
          printf("Unable to open %s\n", filename);
          exit(EXIT_FAILURE);
       }
    
       while((c = fgetc(fp)) != EOF)
       {
          c=toupper(c);
          fseek(fp,-1L,SEEK_CUR);/* back one char, not the beginning */
          fputc(c,fp);
          fflush(fp);
       }
       fclose(fp);    
    
       printf("\n\nThe file has been converted to all Capital letters.\n\n\n");
       return 0;
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Open file name object
    By JJFMJR in forum Windows Programming
    Replies: 3
    Last Post: 09-14-2007, 05:44 PM
  3. Which style of if loops is most common?
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 08-25-2005, 03:18 PM
  4. file existance checking /w code
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 02-08-2002, 01:40 PM
  5. returning to a spot in the code
    By Shadow in forum C Programming
    Replies: 4
    Last Post: 09-22-2001, 05:24 AM

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