![]() |
| | #1 |
| Registered User Join Date: Apr 2006 Location: Mumbai, India
Posts: 26
| Change in sequence due to a '\n' 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;
}
(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));
(none):~/prog # ./err error: 0, error_string: Success Hello (none):~/prog # Why does this change in sequence happen? Thanks and regards, Arun |
| arunj is offline | |
| | #2 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,295
| 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(). |
| zacs7 is offline | |
| | #3 |
| Registered User Join Date: Apr 2006 Location: Mumbai, India
Posts: 26
| Hi! Thanks for the reply. What is silly if it is run as root? Thanks, Arun |
| arunj is offline | |
| | #4 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| > 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. |
| Salem is offline | |
| | #5 |
| Registered User Join Date: Apr 2006 Location: Mumbai, India
Posts: 26
| Hi! Which method in this piece of code is comparable to # rm /? Thanks, Arun |
| arunj is offline | |
| | #6 |
| Registered User Join Date: Apr 2006 Location: Mumbai, India
Posts: 26
| or # rm -rf /? |
| arunj is offline | |
| | #7 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| 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? |
| Salem is offline | |
| | #8 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| 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. |
| brewbuck is offline | |
| | #9 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
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. | |
| matsp is offline | |
| | #10 | |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| Quote:
The FAT was hosed, that was a reinstall on that one. | |
| brewbuck is offline | |
| | #11 |
| Jaxom's & Imriel's Dad Join Date: Aug 2006 Location: Alabama
Posts: 877
| 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. |
| Kennedy is offline | |
| | #12 | |
| Registered User Join Date: Apr 2006 Location: Mumbai, India
Posts: 26
| Quote:
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 | |
| arunj is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| c++builder6 change form names problem | Leite33 | C++ Programming | 2 | 06-09-2008 08:20 AM |
| how to change static char* and not lose mem ? | jabka | C Programming | 15 | 09-07-2007 05:33 PM |
| Change Value in an array | beginner999 | C Programming | 3 | 01-18-2003 07:16 AM |
| How to change CString data to CString data(in Hex format)? (And change back) | ooosawaddee3 | C++ Programming | 2 | 11-08-2002 03:22 AM |
| trying to make change from the price of the item and the amount given. please help!!! | tobyman | C++ Programming | 2 | 09-04-2001 02:12 PM |