Change in sequence due to a '\n'

This is a discussion on Change in sequence due to a '\n' within the Linux Programming forums, part of the Platform Specific Boards category; Hi! I am trying to run the following code. Code: #include <stdio.h> #include <errno.h> int main() { char s[10]; int ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    Change in sequence due to a '\n'

    Hi!

    I am trying to run the following code.

    Code:
    #include <stdio.h>
    #include <errno.h>
    
    
    int main()
    {
            char s[10];
            int nread;
            FILE* fp;
            char c;
    
            fp = fopen("abc", "w+");
            printf("error: %d, error_string: %s", errno, strerror(errno));
    
            fprintf(fp, "Hello\n");
            fseek(fp, 0, SEEK_SET);
    
            while((c = fgetc(fp)) != EOF)
                    write(1, &c, 1);
    
            fclose(fp);
            return 0;
    }
    It compiles fine.
    (none):~/prog # gcc -o err err.c
    (none):~/prog #

    But when I try to run it I am getting -

    (none):~/prog # ./err
    Hello
    error: 0, error_string: Success(none):~/prog #

    If I put a newline after printf like below -

    Code:
    printf("error: %d, error_string: %s\n", errno, strerror(errno));
    I am getting the following output -

    (none):~/prog # ./err
    error: 0, error_string: Success
    Hello
    (none):~/prog #

    Why does this change in sequence happen?

    Thanks and regards,

    Arun

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I hope you're not actually running / compiling it as root, that'd be silly.

    Depending on how you've got output buffering set-up, it will flush at the end of every line or when the buffer fills up. Think of it as so,

    Scenario 1.
    out = "error: 0, error_string: Success(none):"
    out ="hello\n" # flush

    Scenario 2.
    out = "error: 0, error_string: Success(none):\n" #flush
    out ="hello\n" #flush

    If you want to change buffering use setvbuf or call fflush().

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    Hi!

    Thanks for the reply. What is silly if it is run as root?

    Thanks,

    Arun

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > What is silly if it is run as root?
    Because of the amount of extra damage you can do if you get it wrong.

    Say for example you had a buggy 'rm' command and you tried to remove /
    Running as root will hang you out to dry.
    Running as a mortal will tell you to take a hike.

    Likewise, reading email as root, surfing the web as root.
    The root account is for necessary admin only, not a convenience for day to day work. It's not like it's that hard to leave another terminal window open if you need to do root work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    Hi!

    Which method in this piece of code is comparable to # rm /?

    Thanks,

    Arun

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    or # rm -rf /?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    I was merely highlighting a possible future scenario.

    Or are you suggesting that you'll do the right thing when it matters (because I won't believe you).

    Or are you going to wait for someone to "own" your box because you've become overly complacent about running as root?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by arunj View Post
    Hi!

    Which method in this piece of code is comparable to # rm /?
    If you have bugs in your code, anything is possible.

    Back in the days of DOS, where everything basically "ran as root," I completely blitzed my entire system MORE THAN ONCE by making simple programming errors. It can happen to you.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    If you have bugs in your code, anything is possible.

    Back in the days of DOS, where everything basically "ran as root," I completely blitzed my entire system MORE THAN ONCE by making simple programming errors. It can happen to you.
    I once type "delete *.*" on "the wrong disk" (meaning do delete a:*.*, but I ended up deleting c:*.* instead). Not a good idea.

    If you are not developing system code, where you have to run as root to be able to run the code in the first place, I suggest you do "adduser yourname" and then use the new account for code development. You can always set up a "sudo" account for "yourname" and then do "sudo some-command" to perform commands as root.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by matsp View Post
    I once type "delete *.*" on "the wrong disk" (meaning do delete a:*.*, but I ended up deleting c:*.* instead). Not a good idea.
    One of my boo-boos was trying to access a structure member through a far NULL pointer. A far NULL pointer, of course, points to the interrupt vector table. So I ended up smashing the vector for one of DOS's disk write routines, and it happened inside a TSR, right in the middle of DOS trying to update something in the FAT. Imagine yanking the rug out from under somebody trying to carefully paint a china lamp.

    The FAT was hosed, that was a reinstall on that one.

  11. #11
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    To be fair, though, it is sometimes needful to develop as root -- Like when you are attempting to control hardware without drivers. Cannot do it without being root, or at least chmod +S the file -- which is the same thing.

    EDIT: Though, I will say that I did do the following command(s) and really screwed myself:
    modprobe mtdcore
    modprobe jffs2
    modprobe mtdram total_size=63488 erasesize=12
    modprobe mtdchar
    modprobe mtdblock
    dd if=fs.img of=/dev/mtd0
    mount -t jffs2 /dev/mtdblock0 mnt
    cd mnt/mnt/etc
    cp * /etc

    oops!!!!!! CRAP!!!!, I mean
    cp * ../../etc
    SNOT BUBBLES!!!!!!

    Dumb dumb di dumb, Dumb di dumdum dumb di dumb. <-- meant to be sung not read.
    Last edited by Kennedy; 10-22-2007 at 12:30 PM.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    Quote Originally Posted by Salem View Post
    I was merely highlighting a possible future scenario.

    Or are you suggesting that you'll do the right thing when it matters (because I won't believe you).

    Or are you going to wait for someone to "own" your box because you've become overly complacent about running as root?
    Hi!

    It often happens that if I don't do the wrong thing and learn how to rectify it again I'll never be able to grab the correct concept.
    And I think comments like "because I won't believe you" and "you've become overly complacent about running as root" etc. are a bit personal. Are these lines really necessary to answer a question like " What is the problem if I run it as a root"?

    Thanks and reagrds,

    Arun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++builder6 change form names problem
    By Leite33 in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2008, 08:20 AM
  2. how to change static char* and not lose mem ?
    By jabka in forum C Programming
    Replies: 15
    Last Post: 09-07-2007, 05:33 PM
  3. Change Value in an array
    By beginner999 in forum C Programming
    Replies: 3
    Last Post: 01-18-2003, 06:16 AM
  4. Replies: 2
    Last Post: 11-08-2002, 02:22 AM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21