Thread: Change in sequence due to a newline

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

    Change in sequence due to a newline

    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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. Change Calculator with Breakdown of the Change
    By insertcoin in forum C Programming
    Replies: 2
    Last Post: 04-16-2014, 08:25 AM
  2. getting rid of newline?
    By KBriggs in forum C Programming
    Replies: 10
    Last Post: 05-07-2010, 12:30 PM
  3. getting rid of newline
    By AngKar in forum C Programming
    Replies: 24
    Last Post: 04-28-2006, 07:52 PM
  4. change boot sequence
    By somesh in forum Tech Board
    Replies: 3
    Last Post: 06-22-2003, 11:55 PM
  5. newline
    By volk in forum C++ Programming
    Replies: 5
    Last Post: 03-17-2003, 02:50 PM