Home > Back-end >  C - print from a pointer in the buffer
C - print from a pointer in the buffer

Time:11-01

Working on a CS class - C course problem. Part of the problem is to create a function to print the specified length from a char buffer, given a char pointer to print from.

Signature of the function to be called in loop:

bool printLine(char cbuffer, char *ptr, int bufferFillLength)

where cbuffer is the pointer to the beginning of the buffer, ptr is the pointer to the start of the string to print. Variable bufferFillLength is the number of characters to print from the buffer starting at ptr.

The function should be called in loop until the end of the line is reached (i.e function returns false).

Here is my attempt but not working and looking for help.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define BUFFER_SIZE 300

bool printLine (char cbuffer, char *ptr, int printLength){
    char *bufferprinttext;
    
    strncpy (bufferprinttext, &cbuffer[ptr], printLength);
    printf("%s", bufferprinttext);
    if(bufferprinttext[strlen(bufferprinttext)-1] == '\n') {
       //end of line reasched - return false;
       return false;
    }
    return true;
}

int main(int argc, char *argv[])
{
    char cbuffer[BUFFER_SIZE];
    int printLength = 25;
    bool isItEndOfBuffer = false;
    int bufferCounter = 0;

    cbuffer = "Fusce dignissim facilisis ligula consectetur hendrerit. Vestibulum porttitor aliquam luctus. Nam pharetra lorem vel ornare condimentum. Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien dignissim molestie.";

    while(isItEndOfBuffer == false) {
          bufferCounter;
        
        isItEndOfBuffer = printLine(cbuffer, &cbuffer[printLength * bufferCounter], printLength);
    }
}

CodePudding user response:

cbuffer is not a pointer as you say but is passed by value, thus you are not holding it's original reference as you seem to want. Try with the signature bool printLine (char *cbuffer, char *ptr, int printLength);

CodePudding user response:

Jonathan Leffler: I have update the code per your comment. The function prints now but truncated at the beginning. Couldn't figure out what I am missing.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define BUFFER_SIZE 300

bool printLine (char cbuffer, char *ptr, int printLength){ 
    for (int i = 0; i < printLength; i  ) {
        putchar(*ptr  );
        
        if(*ptr == '\0') {
            //end of line reasched - return false;
            return true;
        }
    }

    return false;
    
}

int main(int argc, char *argv[])
{
    int printLength = 25;
    bool isItEndOfBuffer = false;
    int bufferCounter = 0;

    char* cbuffer = "Fusce dignissim facilisis ligula consectetur hendrerit. Vestibulum porttitor aliquam luctus. Nam pharetra lorem vel ornare condimentum. Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien dignissim molestie.";
    //printf ("cbuffer - start ptr=%p \n", &cbuffer[0]);

    printf("Text to print:\n %s \n\n", cbuffer);
    
    printf("But text is truncated at the begining by printLength(i.e. 25): \n");
    //printf ("\nisItEndOfBuffer=%d \n", isItEndOfBuffer );
    while( !isItEndOfBuffer ) {
          bufferCounter;
        //printf ("ptr=%p \n", &cbuffer[printLength * bufferCounter]);
        isItEndOfBuffer = printLine(*cbuffer, &cbuffer[printLength * bufferCounter], printLength);
        //printf ("\nisItEndOfBuffer=%d \n", isItEndOfBuffer );
    }

}

Output:

Text to print:
Fusce dignissim facilisis ligula consectetur hendrerit. Vestibulum porttitor aliquam luctus. Nam pharetra lorem vel ornare condimentum. Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien dignissim molestie.

But text is truncated at the begining by printLength(i.e. 25):
ligula consectetur hendrerit. Vestibulum porttitor aliquam luctus. Nam pharetra lorem vel ornare condimentum. Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien dignissim molestie.
  •  Tags:  
  • c
  • Related