Thread: values vs blocks

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    44

    values vs blocks

    Is it possible to manipulation a file in memory where we can operate on the text just as we do with regular file; then save it to disk under a new file name without destroying the original file?

    Weeks ago, I found two examples that gave me a glimmer of hope, but I still have not touch first base where one would need to open a real file on disk when using these two examples.

    Nonetheless to get to it I tried many ways to squeeze in the isspace() function for simplicity. In the fork-code, it just is added no matter what I alter, and in the malloc-code it over write the first half of the original file in memory. My head is spinning and feetís are drowning.

    Any help would be appreciated.

    Thank you



    FORK:
    Code:
    #include <ctype.h> 
    #include <unistd.h> 
    #include <stdio.h> 
    
    
      char c;
      int i=0;
      char str[]="A B C\n";
    
    
    int main(){
      int p[2]; pipe(p); FILE *f = fdopen( p[1], "w" );
    
    
      if( !fork() ){
        fprintf( f, "working" );
        return 0;
      }
    
    
      fclose(f); close(p[1]);
      char buff[100]; int len;
      while( (len=read(p[0], buff, 100))>0 )
        printf(" from child: '%*s'", len, buff );
      puts("");
    
    
      //  PART 2
      //  
      while (str[i])
      {
        c=str[i];
        if (isspace(c)) c='\n';
        putchar (c);
        i++;
      }
    }

    MALLOC:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h> 
    #include <unistd.h>
    
    
    char c;
    int i=0;
    char str[]="A B C\n";
    
    
    int i;
    int written = 0;
    
    
    int main(int argc, char **argv)
    {
        
        FILE *f = fopen("/dev/null", "w");
    
    
        char *buf = malloc(100);
        setbuffer(f, buf, 100);
        for (i = 0; i < 10; i++)
        {
            written += fprintf(f, "Number %d\n", i);
        }
        for (i = 0; i < written; i++) {
            printf("%c", buf[i]);
       
      //  PART 2
      //  
      while (str[i]){
        c=str[i];
        if (isspace(c)) c=' ';
        putchar (c);
        i++;
        }
      }
    }

  2. #2
    Registered User
    Join Date
    May 2019
    Posts
    214
    Since you ask about files on disk but your code sample doesn't create any files on disk, it's hard to make assumptions about what you're asking, doing or mean.

    If I assume that putchar in these loops somehow represents disk output, which is quite a stretch, I note that in the "malloc" code, i has been set to written - 1 by the preceding loop, so I have no idea what you intend or expect, but if i is beyond the length of str, I'd expect a crash.

    Try to clarify what you intend and what you're asking, because it's hard to figure out what to say here.

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    Hi @Niccolo, sorry about the confusion. C is my first programming language. I been at it since May of this year. I bet my words and examples could only be decipher by tenderfoots like myself since we are the ones who think we found magic in C but donít know how to explain anything.


    My coding box is up side down because I donít understand most of C yet. Itís probably something simple that I have not tried. Iíll write a working example by tomorrow that mimic it perfectly. Thanks for your reply.

  4. #4
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    @Niccolo, Sorry about the confusion. I been reading for nearly 24 hours already. I found my answers in subjects like types of buffers, buffer manipulation functions and more. Being a new to C is going to take me more time then for the average person to learn it. I’m kind of slow like that.

    Thanks to your reply, it energized me to google deeper, and read slower to make sure I don’t miss a word. After re-reading my first post over again I clearly see what you mean.

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    214
    it energized me to google deeper
    The single best skill to learn about any language!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-10-2013, 02:29 AM
  2. Replies: 13
    Last Post: 07-27-2013, 10:20 AM
  3. Replies: 2
    Last Post: 07-21-2013, 10:35 PM
  4. write() and read() int values from socket returning wrong values.
    By Premjith P S in forum Linux Programming
    Replies: 8
    Last Post: 11-29-2012, 02:59 PM
  5. passing int values and *double values through message queues
    By Hyp3rTension in forum C Programming
    Replies: 11
    Last Post: 05-11-2012, 05:04 AM

Tags for this Thread