Thread: Help with code

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    20

    Help with code

    Hello
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #define DATA_FILE "data3.bin"
    
    int main(int argc, char *argv[])
    {{
    
            FILE *fp;
            char* buff;
            if( ( fp=fopen(DATA_FILE, "wb") )== NULL)
            {
                printf("Klaida sukuriant  faila %s\n",DATA_FILE);
                exit(1);
            }
            printf("Iveskite teksta (Ivedimo pabaiga ctrl+z):\n");
            scanf("%c", &buff);
            while(!feof(stdin))
            {
                scanf("%c", &buff);
                fwrite(&buff,sizeof(char),1,fp);
            }
            fclose(fp);
        }
        FILE *fp;
        if ( ( fp=fopen(DATA_FILE, "rb+") )== NULL)
        {
            printf("Klaida atidarant faila %s\n",DATA_FILE);
            exit (1);
        }
        char* buff;
    	buff = (char*) malloc (sizeof(char));
        while ( fread(buff,sizeof(char),1,fp) == 1)
        {
    		if (*buff >= 'a' && *buff <= 'z')
    		{
    			*buff-=32;
                fseek(fp,-1,SEEK_CUR);
                fwrite(buff,sizeof(char),1,fp);
    			fflush(fp);
    		}
        }
        fclose (fp);
    
        return 0;
    }
    here is code is changing all small letters to big one, how to make that this code changes large letters to small ones, i tried to use strlwr function but it's don't work.
    thank you for help
    forgot to say that it's read all from bin file
    Last edited by alionas; 03-13-2011 at 01:54 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Find the part of the code that turns them from small to big, and switch it so it changes from big to small. You should probably get rid of the -=32 piece and just use tolower or toupper from ctype.h instead.


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

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you used tolower() and toupper() as quzah suggests your code could be this simple...

    Code:
    char buff;
        while ( fread(&buff,sizeof(char),1,fp) == 1)
        { buff = toupper(buff)
           fseek(fp,-1,SEEK_CUR);
           fwrite(buff,sizeof(char),1,fp);
           fflush(fp); }
    However; you should know that reading seeking and writing inside the same sequential file like you're doing is some serious bad Ju-Ju... One error and your input file is trashed. Just consider the consequences of that on the customer list for a major corporation...

    You should always open a second temp file to write to. It's perfectly legal to have multiple files open at the same time. If you want, after the process terminates successfully, you can delete the original file and rename the copy... and all is good.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    admins please delet this thread thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Illegal Instruction at perfectly fine Code
    By m00ni in forum C Programming
    Replies: 24
    Last Post: 02-14-2011, 02:56 AM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM