Thread: Array-File-Buffers to use one problematic block of code.

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    30

    Array-File-Buffers to use one problematic block of code.

    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.
    Last edited by fat32; 12-10-2018 at 06:15 AM.

  2. #2
    Registered User
    Join Date
    Oct 2018
    Posts
    30
    Heres the output re-formated:

    (/ccode/Arrays)$ ./a.out

    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.

    1) File was read into arr_0.
    .................................................. ....................

    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

    2) arr_0 was copied to arr_1.
    test arr_1 remove periods.
    .................................................. ....................

    sizeof(buffer): 233

    3) pass arr_1 to a function.
    just trying something new.
    .................................................. ....................

    Lack of knowledge. Should remove spaces

    4) returns nothing because..
    .................................................. ....................

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    30
    UNBELIEVABLE!



    After all these days I’m getting a bit closer: getchar vs getc



    https://cboard.cprogramming.com/c-programming/139887-mistake-k-r-bible.html

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    656
    getc (and getchar) return int. Your code will fail on systems where char is unsigned because an unsigned value can never equal EOF (because EOF is guaranteed to be a negative value). Also, even on systems where char is signed, your code will fail if you try to read a byte from the file with the value 0xFF (assuming EOF has the value -1); the code will interpret that as EOF and will terminate before reading the rest of the file.

    tl;dr: save the return value of getc/getchar in an int variable to avoid problems.

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    30
    Thank you christop. I been working on it! Since I read your reply, I did some serious reading about pointers. For what people say, you can’t master it in a day. I figure a month or two. All day, everyday! Anyway, if you find some time could you post a drop-in demo to show the simplest of use of your idea? All that I tried don’t work, but I did make it burp a few times.


    I must have started out the wrong way with c. I was opening and saving files to disk constantly after every function complete, switching from file1 to file2 and back again. The only good thing about it it was all build-in. For what I see you can probably do everything you need with a single open file then use as many buffers as needed to handle those many functions. This probably what everybody do anyway. The best thing I figure by accident was how easy it is to save an array-buffer directly to disk. It probably the way the c-lib do it. This made me feel that I am on the right track.


    Code:
      fwrite(arr_6, sizeof(char), Len6, fw);
    
      fclose(fw);
    }
    Works like a charm!
    Last edited by fat32; 12-11-2018 at 02:01 PM.

  6. #6
    Registered User
    Join Date
    Oct 2018
    Posts
    30

    Solved

    IBM Knowledge Center
    After trying mmap, fileno, raw read/write and all the rest, I had to face it. They said IMMPOSIBLE! Then I stop. Anyway, my problem is solved. I just realize hours ago that I only have two blocks of code that use fputs and putc. I will never them them up. Everything else is raw-code like loops for strlen, strcpy waiting to get optimize ... if any more..


    All I got to do is fread each one into its own, then reread each into array buffers and go from there. Iím lucky, they both are for totally difference operations. I was to desperate to think about think that. But if I were not I would have never got this far with c and no where at all if it had not been for this forum who gave me code to do ALL that I needed. I mean that to the bottom of my heart. After all of these wee hours I done during the past 4 months I earn my A++.... c


    My way was impossible christop. All I had to do was turn it upside down.


    Thanks for the tips. It will come in handy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two problematic recursive programs
    By Kelton2 in forum C++ Programming
    Replies: 13
    Last Post: 10-26-2014, 08:01 PM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. Book's code: Problematic
    By RoD in forum Game Programming
    Replies: 14
    Last Post: 01-21-2003, 09:08 AM
  4. my problematic first program
    By lisab in forum C Programming
    Replies: 0
    Last Post: 10-28-2002, 09:58 AM
  5. File descriptors and buffers.
    By ingofrey in forum C Programming
    Replies: 2
    Last Post: 10-17-2002, 09:40 AM

Tags for this Thread