Going too far?

I wasn't sure if I should post this in this section, it is programming related, and it is a question, but it's not a question in the sense of "Help me, I can't do this", so I'm posting it here.

Is this sort of thing going too far?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* This function creates a new file for editing using getchar() and an infinite loop (until escape is pressed) */
void newFile() {
    FILE* f;
    char fname[FILENAME_MAX];

    if (!(printf("Enter a suitable filename, maximum %d characters.", FILENAME_MAX))) {
        error(1, "output failure");
    }

    if (!(fgets(fname, sizeof(fname),stdin))) {
        error(0, "invalid input");
    }

    // ... I haven't gotten this far yet
}

/* Prints an error message and number. */
void error(const int n, const char* error) {
    if (!(printf("Error %d occurred - %s.\n", n, error))) {
        error(1, "output failure");
    }
}


In terms of error checking, is this ridiculous or should I keep going on this track?

My personal opinion is that it has a return value for a reason.
I'm most unsure about the error function recursing if there's an error... I mainly created it because it was going to be hard getting my error messages to be consistent.
Last edited on
Yes, it is, and yes, it's likely to recurse.

Let me get this straight. You're inside the error reporting function. You get an error while trying to output, and then you call the error reporting function, which you know will attempt to perform the very same operation that just failed?
Well, you have a bug...

If the array is FILENAME_MAX bytes wide, then you better hope that the user enters
at most FILENAME_MAX-1 chars so you have room for the \0 at the end.

Personally, I agree with you that return codes should be checked, but in practice
checking the return code of printf might be a little too much. First of all, you have
to do that on every printf, meaning a lot of braincells spent just on that.
But secondly, what really can you do if printf fails? If printf() fails, would you not
think that your error() function would also fail to output the error? And if so, how
will you otherwise inform the user of the error?

Thirdly, if you are going to check the return code of printf, printf() returns the number
of characters printed. Should you not check to ensure that printf() returned the right
number of characters printed and not some number less than what you wanted?
(If my other argument didn't convince you to ignore printf()'s return code, then this
one should, because you'll expend a lot of effort determining what the right number
of characters is to test against.)

EDIT: (since helios beat me to the punch, I'll also add that many programmers will
handle one error, but not double faults, unless of course you are writing mission
critical software and lives depend on it.)
Last edited on
I didn't think of the \0 on the end :(
However I doubt the user will ever type in 4096 characters... which is what, on GCC at least, FILENAME_MAX is set to :l

If printf fails I can... write in the text file "OMG PRINTF FAILED, SAVE US!"...
No, but seriously, that's a good point. I also didn't think of the function recursing and then repeating the error.

But Jsmith, printf() only returns the number of characters printed if it succeeds. Otherwise it returns null, hence why I was checking that it != false.

By the way, lives do depend on it! Where would we, as humans, be without badly-coded, abysmally boring text editors?

Thanks.
Last edited on
http://www.cplusplus.com/reference/clibrary/cstdio/printf/
On failure, a negative number is returned.


Huh? Since when do text editors need to be fun?
Oh for god's sake. This not reading things properly thing is really getting on my nerves...

Since the dawn of time text-editors have been used as games, everybody knows that :l
But really, I meant "boring" as in there's nothing to it. In a Linux terminal it looks nice because of the nice formatting. In a windows terminal it might look ok if you mess with the colour settings... but the font for the command prompt is horrible. And the only alternative is "Lucida Console" which just looks like some weird Microsoft programmer who thought he was a "free-spirit" added that for the sake of being different...
The actual program is simply boring.
Maybe you should start by not writing boring programs.
=[
But then how would I learn to create fantastic, feature rich programs? Surely the first program you ever created would have been something rather boring. How many people can honestly say the first program they wrote was a 3D game?

It isn't boring to write, anyway. Writing it is actually more fun than I've ever had writing any other program. It's just I can't see why it would be interesting to use. Where notepad++, gedit, jEdit, Code::Blocks and many other syntax-highlighting editors have loads of interesting (although in some cases, useless) features, mine is going to have a function where it opens a text file (read only) and applies some basic syntax highlighting. Oh well. It's fun to write.
The first program I wrote is boring to me now. Back then it was interesting because I was learning how to program.
If you're thinking about "interesting" and "boring" in such terms, you'll never be able to do something that isn't boring. You'll always be affected by the "the grass is greener on the other side" effect.

As for the editor, you could "pull an Emacs". Write the simplest editor possible, and then on top of that put a powerful interpreter for plugins. You're done.
Similarly, this is interesting for me because I'm learning and at the same time, hopefully, producing something somewhat useful. It's fun to create because I'm creating it, I'm in the metaphorical driver's seat. I have to think about getting around things that won't work, and such. That's why it's fun for me. But I imagine the actual program would be boring to any prospective end user.

I don't really like emacs. I find it somewhat unintuitive to use. To create a new file, you've got to actually create the file before you can type it up. In every other text editor I've ever used you just start bashing keys and then hit save.
Topic archived. No new replies allowed.