Find the answer to your Linux question:
Results 1 to 2 of 2
I was writing a program that had a whole bunch of data transfer, when I realized that I have almost no idea what goes on behind the scenes of such ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2005
    Posts
    1

    Data Transfer Question


    I was writing a program that had a whole bunch of data transfer, when I realized that I have almost no idea what goes on behind the scenes of such an endevor. I am not the type of person who likes to have things happen "magically" so I was wondering if anyone knew how it worked. I have already scoured the web but have not found much of anything on this topic. But here is what I am talking about.

    For example;

    Code:
    #include <stdio.h>
    main &#40;&#41;
    &#123;
       char c ;
       FILE *fp;
       if &#40;&#40; fp = fopen &#40;"output.txt", "w"&#41;&#41; == NULL &#41;
          printf &#40;"\n Cannot open the file&#58; output.txt\n"&#41;;
       else
          &#123;
             c = 'a';
             fputc &#40; c, fp&#41;;
             fclose &#40;fp&#41;;
          &#125;
    &#125;
    When this program begins to execute, the character 'a' will be placed in memory by the C run-time environment. This character will then be sent to a file "output.txt".

    I want to know how 'a' is bound to a particular place in memory. I also want to know what happens to get this character to a particular physical record on a physical disk unit.

    I know this is kind of an advanced question, but I would love it if someone knew the answer, or if someone could point me in the right direction (because my searches didn't bring up anything close to what I wanted).

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035

    Re: Data Transfer Question

    Quote Originally Posted by maiios
    Code:
    #include <stdio.h>
    main &#40;&#41;
    &#123;
       char c ;
       FILE *fp;
       if &#40;&#40; fp = fopen &#40;"output.txt", "w"&#41;&#41; == NULL &#41;
          printf &#40;"\n Cannot open the file&#58; output.txt\n"&#41;;
       else
          &#123;
             c = 'a';
             fputc &#40; c, fp&#41;;
             fclose &#40;fp&#41;;
          &#125;
    &#125;
    1. 1 byte of memory is automagically allocated. It can hold one character ( integers -127 through 127 ) and is referenced by the label c
    2. A pointer to a FILE structure is created, and given the name 'fp'
    3. Several things happen:
    First, output.txt is open()'d. If doesn't exist, it is created. If it already exists, then it is next truncated.
    Then, fp is set to point to that memory
    4. c now contains the integer 0x61 ( decimal 97, which is the ASCII code for the letter 'a' )
    5. c, which contains the integer 0x61 ( decimal 97, ASCII a ), is copied to the file stream ( output.txt ) pointed to by the FILE pointer fp
    6. The FILE descriptor fp is closed
    -lakerdonald

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •