Thread: Reading three digits at a time as one number in a long string of numbers

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    9

    Reading three digits at a time as one number in a long string of numbers

    How can I read a file that contains numbers only, but read it by three digits at a time? I have a long string of numbers and every three digits corresponds to a particular number in itself. i.e. a string of 064045154 would need to be read as '064' '045' and '154'. I need to then subtract one from each of these numbers and the new values I need to convert into their ASCII characters and place these in a new file. This is what I have (focusing on the 'Decrypt' function) but all it does is in the new file place a string of the same character repeated over and over a total number of times equal to the number of integers in the numbers file.
    Code:
    #include "stdio.h"
    #include "stdlib.h"
    #include "string.h"
    #include "limits.h"
    
    
    int Encrypt(char * FILENAME)
    {
        char NEW_FILENAME[strlen(FILENAME)+4];
        FILE *inFile;   //Declare inFile
        FILE *outFile;  //Declare outFile
        int dpos;
        char *ptr= strrchr(FILENAME, '.');
    
        dpos=(ptr-FILENAME);
        strncpy(NEW_FILENAME, FILENAME, dpos);
        NEW_FILENAME[dpos] = '\0';
        strcat(NEW_FILENAME, ".enc");
    
        int Byte;
        char newByte;
    
    inFile = fopen(FILENAME,"rb");
    outFile = fopen(NEW_FILENAME, "w");
    if(inFile == NULL || outFile == NULL)
        {
            printf("Error in opening file");
            return 1;
        }
    else
        {
        printf("\nFile Opened, Encrypting");
    
        while ( !feof( inFile ) )
        {
            Byte=fgetc(inFile);
            newByte=Byte+1;
            if(newByte<100)
            {
                fprintf(outFile,"0%d",newByte);
            }
            else
            {
                fprintf(outFile,"%d",newByte);
            }
        }
    
            fclose(inFile);
            fclose(outFile);
        }
    return 0;
    }
    
    int Decrypt(char * FILENAME)
    {
        char NEW_FILENAME[strlen(FILENAME)];
        FILE *inFile;   //Declare inFile
        FILE *outFile;  //Declare outFile
        int dpos;
        char *ptr=strrchr(FILENAME, '.');
    
        dpos=(ptr-FILENAME);
        strncpy(NEW_FILENAME, FILENAME, dpos);
        NEW_FILENAME[dpos] = '\0';
        strcat(NEW_FILENAME, ".dec");
    
    inFile = fopen(FILENAME,"rb");
    outFile = fopen(NEW_FILENAME, "w");
    
            int newByte;
            int Byte;
    
        if(inFile == NULL || outFile == NULL)
    {
            printf("Error in opening file");
            return 1;
    }
        else
    {
            printf("\nFile Opened, Decrypting");
    }
        while (Byte=fgetc(inFile) != EOF)
    {
            char Count[4];
    
            int i;
    
               while (i=0, i<3, i++)
    {
            Count[i]=fgetc(inFile);
    }
            int b=atoi(Count);
    
            newByte=b-1;
    
    
            fprintf(outFile,"%c", newByte);
    }
    
            fclose(inFile);
            fclose(outFile);
    
    return 0;
    }
    
    int main(int argc, char*argv[])
    {
        if(strcmp(argv[1], "-e") == 0)
        {
            char *file_to_be_enc=argv[2];
            Encrypt(file_to_be_enc);
        }
        else if(strcmp(argv[1], "-d") == 0)
        {
            char *file_to_be_dec=argv[2];
            Decrypt(file_to_be_dec);
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    All sorts of problems.

    Firstly, "while (Byte=fgetc(inFile) != EOF)" does not store the result of fgetc() in Byte, and then compare that with EOF. It computes "fgetc(infile) != EOF" and stores the result of THAT operation in Byte.

    Second, atoi() assumes a zero-terminated string. For example, to get 123, you would need a set of four bytes '1', '2', '3', and '\0' (no, the backslash in the end is not accidental). Your code, however, is not adding the zero terminator - it is leaving Count[3] uninitialised. That causes your code to exhibit undefined behaviour.

    There are plenty of other problems too, but correcting the above will be a step in the right direction.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help program c, converting numbers to digits, string.h
    By Joshua Rhee in forum C Programming
    Replies: 1
    Last Post: 02-19-2013, 04:19 PM
  2. c++ program-the number of common digits of 2 numbers a & b
    By Sky_Daughter in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2011, 05:03 PM
  3. Reading number of digits
    By Mentallic in forum C Programming
    Replies: 7
    Last Post: 03-27-2010, 11:06 PM
  4. Reading in an unknown number of numbers
    By cboard_member in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2006, 04:21 AM
  5. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 04:31 AM