I’m having a problem copying text from one buffer to another buffer. At the same time I trying to give these buffers something more advance to do because I need to know all limitation as I go. removeWhiteSpaces is afvanced enough. About my buffers; I get the same result with memcpy and memmove.
If it's not too much to ask, I need solutions to both, memmove and removeWhiteSpaces? If there are better ways than memcpy - memmove, I would appreciate any model. Here's my simplified example. It’s a bit more then half-way there:
Code:
/**
* FILE to BUFFER - the real thing - BUFFER to BUFFER
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
FILE *fp1;
FILE *fp2;
char *buffer1;
char *buffer2;
long lengh_1;
long lengh_2;
/***********************************************************************
* TEXT is in BUFFER-1
**********************************************************************/
fp1 = fopen("_1.txt", "r");
fseek(fp1, 0L, SEEK_END); // Get num of bytes
lengh_1 = ftell(fp1);
fseek(fp1, 0L, SEEK_SET);
buffer1 = (char*)calloc(lengh_1, sizeof(char));
fread(buffer1, sizeof(char), lengh_1, fp1);
fclose(fp1);
free(buffer1);
/***********************************************************************
* BUFFER-1 wish to be use by BUFFER-2
**********************************************************************/
fp2 = fopen("_2.txt", "r");
fseek(fp2, 0L, SEEK_END); // Get num of bytes
lengh_2 = ftell(fp2);
fseek(fp2, 0L, SEEK_SET);
buffer2 = (char*)calloc(lengh_2, sizeof(char));
fread(buffer2, sizeof(char), lengh_2, fp2);
fclose(fp2);
free(buffer2);
/**************************************************** DO SOMETHING */
puts("buffer1 before memcpy:");
puts(buffer1);
memmove (buffer1, buffer2, sizeof(lengh_1)); // Copies 2 to 1
puts("buffer1 after memcpy:");
puts(buffer1);
//printf("SUCCESS:\n\n%s", buffer1);
//printf("SUCCESS:\n\n%s", buffer2);
// puts("\nbuffer1 after memcpy ");
// puts("\nbuffer2 after memcpy ");
return 0; }
/***********************************************************************
* TEST
**********************************************************************/
/**
printf("SUCCESS:\n\n%s", buffer1);
char *copyFrom = buffer1;
for (; *copyFrom && ' ' == *copyFrom; ++copyFrom);
memmove(buffer1, copyFrom, strlen(copyFrom));
printf("SUCCESS:\n\n%s", buffer1); */
//......................................................................
//......................................................................
/**
size_t i = 0, j = strlen(buffer2);
while (j > 0 && isspace((unsigned char)buffer2[j - 1])) buffer2[--j] = '\0';
while (isspace((unsigned char)buffer2[i])) i++;
if (i > 0) memmove(buffer2, buffer2 + i, j - i + 1);
ERROR: 'isspace' is invalid in C99 */
I forgot to post my mismatched result. Only a expert would know thru reading the code. I need to know what is causing this leak too.
Code:
buffer1 before memcpy:
123456789_ABCDEFGHI
This line has spaces.
This line has a tab space
How do I remove all leading tabs and spaces
to shift all strings to the left using char buffer(s)?
buffer1 after memcpy:
I am buf9_ABCDEFGHI
This line has spaces.
This line has a tab space
How do I remove all leading tabs and spaces
to shift all strings to the left using char buffer(s)?
.
Other then that, C is getting easy!