Thread: My code is buffering my output. I don't find this funny.

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    16

    My code is buffering my output. I don't find this funny.

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    //#include <stdlib.h>                                                    // used by atoi and malloc
    #include <stdio.h>                                                        // printf library
    #include <string.h>                                                        // library manipulates strings.
    
    
    char message_cache1[240];
    char message_cache2[240];
    
    
    int main() {
        FILE *fp;
        char filename[]="ABseeD002.txt";
        fp=fopen(filename, "w");
        if (fp == NULL) { printf("\n The file %s could not be opened.", filename); } //  if error occurs
        printf("Items will be initiated in the same DIRECTORY as this program. Enter EXIT on a BLANK line to exit.\n"); 
        printf("Please enter a message you desire to encode. Limited to 100 characters per line.\n:");                                                
        gets(message_cache1); //  gets message from keyboard
        while (!strstr(message_cache1, "EXIT")) {
            printf("\nlengthofcache1:%d\n", strlen(message_cache1));
            if ((strlen(message_cache1)) > 5) { 
                for (int i = 5; i < 240; i++) { 
                    if (message_cache1[i]!=NULL) { message_cache2[i-5] = message_cache1[i];    }
                }
                printf("\nlengthofcache2:%d\n", strlen(message_cache2));
            }
            printf("\n%s\n", message_cache1);
            printf("\n%s\n", message_cache2);
            for  (int count=0; count <=4; count++) // review for loops maybe sing a song about them.
                {
                    int ascii_cache=message_cache1[count];
                    if (ascii_cache >= 32 && ascii_cache <= 99)     {    fprintf(fp, "0");                    } // dump the leading 0 to file.
                    if (ascii_cache != 0)                            {    fprintf(fp, "%d", ascii_cache);        } // dump to file.    %d = integer    
                }
            fprintf(fp, "\n");
            strcpy(message_cache1, "");                        // message 1 empty
            printf("\nlengthofcache1:%d\n", strlen(message_cache1));
            printf(":%s", message_cache2);                    // prints message 2
            gets(message_cache1);                            // gets new message1
            printf("\nlengthofcache1:%d\n", strlen(message_cache1));
            strcat(message_cache2, message_cache1);            // adds message1 to message2
            strcpy(message_cache1, "");                        // message 1 empty
            //printf("\nMessage1:%s", strlen(message_cache1));
            //puts(message_cache1);
            strcpy(message_cache1, message_cache2);            // copies 1 to 2
            strcpy(message_cache2, "");                        // empties 2
            printf("\nlengthofcache1:%d\n", strlen(message_cache1));
            printf("\nlengthofcache2:%d\n", strlen(message_cache2));
            printf("\n%s\n", message_cache1);
            printf("\n%s\n", message_cache2);
        }
        fclose(fp);  // closes the cfile pointer 
        printf("Encoding....completed to the file %s.", filename);
        return 0;
    }


    INPUT 123456
    OUTPUT:
    049050051052053
    054051052053

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,426
    2022 and still using gets() - Ugh!!!!

    gets has been dead for a decade.
    gets, gets_s - cppreference.com

    What are you expecting to see?
    Code:
    $ ./a.out 
    Items will be initiated in the same DIRECTORY as this program. Enter EXIT on a BLANK line to exit.
    Please enter a message you desire to encode. Limited to 100 characters per line.
    :123456
    
    lengthofcache1:6
    
    lengthofcache2:1
    
    123456
    
    6
    
    lengthofcache1:0
    :6
    
    lengthofcache1:0
    
    lengthofcache1:1
    
    lengthofcache2:0
    
    6
    
    
    
    lengthofcache1:1
    
    6
    
    
    
    lengthofcache1:0
    :EXIT
    
    lengthofcache1:4
    
    lengthofcache1:4
    
    lengthofcache2:0
    
    EXIT
    
    
    Encoding....completed to the file ABseeD002.txt.$ 
    $ 
    $ cat ABseeD002.txt
    049050051052053
    054051052053
    If you don't want to see the end junk, then you need to look at how you copy a fragment of a string, and how you take care of the \0 at the end of the string.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    16
    I charge for looking at your answer. ;-)

  4. #4
    Registered User
    Join Date
    Oct 2021
    Posts
    16

    methods

    COMMANDS TO EMPTY A STRING
    Code:
    memset(message_cache1,0,sizeof(message_cache1));  --> SUCCESSS
    
    strcpy(message_cache1, "");  --> FAIL (LEFT gibberish hidden)

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    394
    Quote Originally Posted by cprogramnoob View Post
    COMMANDS TO EMPTY A STRING
    Code:
    memset(message_cache1,0,sizeof(message_cache1));  --> SUCCESSS
    
    strcpy(message_cache1, "");  --> FAIL (LEFT gibberish hidden)
    The string is only valid up to and including the terminating null character, so the second one is not a fail.
    Last edited by hamster_nz; 12-04-2022 at 07:48 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,426
    Code:
    lengthofcache1:1
    21	        if ((strlen(message_cache1)) > 5) { 
    (gdb) n
    27	        printf("\n%s\n", message_cache1);
    (gdb) 
    
    6
    28	        printf("\n%s\n", message_cache2);
    (gdb) 
    
    
    29	        for  (int count=0; count <=4; count++) // review for loops maybe sing a song about them.
    (gdb) 
    31	                int ascii_cache=message_cache1[count];
    (gdb) p message_cache1
    $5 = "6\000\063\064\065\066", '\000' <repeats 233 times>
    Your for loop blindly assumes there are 5 characters in the buffer.

    Any loop dealing with strings needs to check for the \0 character.
    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.

  7. #7
    Registered User
    Join Date
    Oct 2021
    Posts
    16
    Thank you for helping be careful of the gibberish against me out there. When I say FAIL it failed for me. Your success was not mine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting problems in double buffering code :(
    By joaquim in forum Windows Programming
    Replies: 10
    Last Post: 10-13-2014, 03:14 AM
  2. funny output at the end of textfile after sort.
    By csharp100 in forum C Programming
    Replies: 7
    Last Post: 10-21-2012, 05:11 PM
  3. Help With Funny Integer Output
    By Spamtacus in forum C Programming
    Replies: 16
    Last Post: 11-26-2011, 01:22 PM
  4. Funny output on hex value of number
    By shav in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 08:19 AM
  5. Did anyone else find this funny?
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-15-2002, 06:03 PM

Tags for this Thread