Hello Everybody,
If it’s not one thing, it’s another. .. Just by reading, all of a sudden I want to code for the stack safely/efficiently as possible. However at this moment, my problem is the last block of code starting with char c;. This section of the code works perfectly provided you use fopen read and write. Now I want to use Array as File Buffers and malloc only when necessary. My problem is I don't know what to substitute getc, putc, fr, and fw with or anything else within. The code got sick before I did.
I test all I could find and created a lot of what I thought could work. I read much as I could understand about Arrays and I just learned the differences between Array-Buffers and dimensional-Arrays a few days ago. It took that long. Weard, I know Nearly everything seems to be about dimensional (1D) (2D). I was barking up the wrong tree for a long time. I still like it but I’m terrible with math outside of counting my own pocket change.
However, if you follow the rest of the code you’ll see I put in a great deal of effort. I managed to do a lot more with a single disk-read. Most of it is not included for sake of readability to my problem at hand. How to force use of pointers along with some other changes is what it needs I guest, but I don't know how.
I removed all I done to within the char c; section because it would be full of slop-code that would confuse anyone who may try to help. Other then personal use, this thread was supposed to be a how-to as my way of saying thanks to those few people who woke me up to C, but I fail. I’ll just wait until I’m 100% sure during my next adventure.
Here’s the code:
Code:
#include <stdio.h>
#include <string.h>
#define MAX 500000
#define MEM (MAX+1)
unsigned int Len0; /** global or not? vv */
unsigned int Len1;
unsigned int Len2;
void load_buffer(char (*arr_1)[Len1]) /** void using Len1 ^^ */
{
printf(" sizeof(buffer): %lu\n", sizeof(*arr_1));
}
int main() {
FILE *fr, *fw;
char arr_0[MEM];
char arr_1[MEM];
char arr_2[MEM];
fr = fopen("0.txt", "r");
if (fr != NULL) {
memset(arr_0, 0, MEM); // clear them jic
memset(arr_1, 0, MEM);
memset(arr_2, 0, MEM);
/** - - - - - - - - - - - - - - - - - - - - - - - - - - - file to array */
// size_t Len0 = fread(arr_0, sizeof(char), MAX, fr); /** either will work */
Len0 = fread(arr_0, sizeof(char), MAX, fr);
if (Len0 == 0) {
fputs("File0 error.", stderr);
} else {
arr_0[Len0] = '\0';
Len1= Len0; }
printf("%s1) File was read into arr_0.\n", arr_0);
printf(" .........................\n\n");
/** - - - - - - - - - - - - - - - - - - - - - - - - - - - remove periods */
for ( Len1 = 0; Len1 < Len0; ++Len1 )
arr_1[Len1] = arr_0[Len1];
int a=0,b=0;
while(arr_1[a]!='\0') {
if(arr_1[a]!='.') {
arr_1[b++]=arr_1[a]; }
a++; }
arr_1[b]='\0';
}
printf("%s2) arr_0 was copied to arr_1.\n", arr_1);
printf(" test arr_1 remove periods.\n");
printf(" ..........................\n\n");
/** - - - - - - - - - - - - - - - - - - - - - - - - - - pass to function */
load_buffer(&arr_1);
printf("3) pass arr_1 to a function.\n");
printf(" just trying something new.\n");
printf(" ..........................\n\n");
/** - - - - - - - - - - - - - - - - - - - - - - - - - how to operate on this */
/** - - - - - - - - - - - - - - - - - - - - - - - - - inside arr_1, 2 or 3 */
/** - - - - - - - - - - - - - - - - - - - - - - - - - something like step-1, */
/** - - - - - - - - - - - - - - - - - - - - - - - - - but to remove all spaces */
/** - - - - - - - - - - - - - - - - - - - - - - - - - Hope you get my meaning. */
// ..... I am not seeking replacement of the code below. Only the fix as indicated.
printf(" Lack of knowledge. Should remove spaces\n");
printf("4) returns nothing because..\n");
printf(" .........................\n\n");
char c;
while((c=getc(fr))!=EOF)
{
if(c==' ')
{
if((c=getc(fr))=='*' )
{
do
{
c=getc(fr);
}while(c!='*');
c=getc(fr);
if(c==' ')
c=getc(fr);
if(c=='\n')
c=getc(fr);
}
else
{
if(c==' ')
{
do
{
c=getc(fr);
}while(c!=10);
c=getc(fr);
}
}
}
putc(c,fw);
}
fclose(fr);
return 0;
}
.
.
Here’s the file: 0.txt
Code:
This is the file on disk. The goal is to read it into
a Array-Buffer. From that point we only use same fp
and more buffers to perform all operations on the
*stack*. Use malloc if required or as a demo.