When should I use fputs instead of fprintf? [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
What exactly is the difference between the two?

fprintf does formatted output. That is, it reads and interprets a
format string that you supply and writes to the output stream the
results.
fputs simply writes the string you supply it to the indicated output
stream.
fputs() doesn't have to parse the input string to figure out that all you want to do is print a string.fprintf() allows you to format at the time of outputting.

As have been pointed out by other commenters (and as it's obvious from the docs) the great difference is that printf allows formatting of arguments.
Perhaps you are asking if the functions are equivalent where no additional arguments are passed to printf()? Well, they are not.
char * str;
FILE * stream;
...
fputs(str,stream); // this is NOT the same as the following line
fprintf(stream,str); // this is probably wrong
The second is probably wrong, because the string argument to fprintf() is a still a formating string: if it has a '%' character it will be interpreted as a formatting specifier.
The functionally equivalent (but less direct/efficient/nice) form would be
fprintf(stream,"%s", str);

Uhm...
...puts() just writes a string, while printf() has a number of formatting facilities for several types of data.
fputs()
http://www.cplusplus.com/reference/clibrary/cstdio/fputs/
fprintf()
http://www.cplusplus.com/reference/clibrary/cstdio/fprintf/
Documentation is useful! Learn to read it, and you'll have a powerful tool on your side.

Related

scanf and entering characters [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I was wondering what happens if in my program I ask for an integer input using scanf("%d",&num) and the user enters a string of chars and ints for example at3stf0rfun.
What would be the value of num in that case? Does it take only the integers 30 or does it also convert the chars to its' ASCII decimal values?
Scanning stops on encountering invalid input. Therefore, in your code the value of num will remain unchanged.
To detect whether this is what happened you need to examine the return value of scanf. To quote man scanf:
RETURN VALUES
These functions return the number of input items assigned. This can
be fewer than provided for, or even zero, in the event of a matching
failure. Zero indicates that, although there was input available,
no conversions were assigned; typically this is due to an invalid
input character, such as an alphabetic character for a `%d'
conversion. The value EOF is returned if an input failure occurs
before any conversion such as an end-of-file occurs. If an error or
end-of-file occurs after conversion has begun, the number of
conversions which were successfully completed is returned.

Error in accepting the value from console in c? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
while working on the c project I found the following problem.
I declared a float variable and performed scanf operation on it. The sample code is -
<#include stdio.h>
<#include conio.h>
void main()
{
clrscr();
float foo;
scanf(" %f ",&foo);
//remaining code goes here
getch();
}
I found that the error is due to the spaces given after and before %f in scanf statement.
But I don't know what is the reason behind it ?
thanks.
I think this may help you a little.
the function scanf will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- see isspace).
A single whitespace in the format string validates any quantity of
whitespace characters extracted from the stream (including none).
Also it should be like #include<stdio.h> not <#include stdio.h>

How to print using scanf() function in C programming? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Is it any possible chance to print to IO device with the help of scanf() function?
main()
{
char str[30];
scanf("\n Name ?%s",&str);
printf("\n Name Entered is %s",str);
}
Try this and help me out.
The scanf function reads input from the console and parses it. It has no means of printing anything. That's what the aptly-named printf family of functions is for.
The first argument to scanf is not a prompt (as it looks like you're assuming), it's the format string to be used to scan the input.
That scanf will fail unless your input matches exactly what's expected including the literal string "Name ?" and so forth. It will also stop at the first whitespace so entering udhayar kumar would only get your first name. If you want a prompt simply output it beforehand such as with:
char str[30];
printf ("Name? ");
scanf ("%s", str);
printf ("Name Entered is %s\n", str);
However keep in mind that unbound %s format specifiers for scanf are an easy way to crash your program or give someone a security hole they can get through (see "buffer overflow"). That's because there's no bounds checking on the input.
If you want a decent input function, check out this one. It gets an entire line (with optional prompt) and handles error conditions well.

What is the meaning of f in printf? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What is the meaning of "f" in C's printf?
The f in printf stands for formatted, its used for printing with formatted output.
As others have noted, the trailing f indicates formatted output (or formatted input for functions in the scanf family).
However, I'll add that the distinction matters because it's important for callers to know that the string is expected to have format-specifier semantics. For example, do not do this:
char* s = get_some_user_input();
printf(s); // WRONG. Instead use: printf("%s", s) or fputs(stdout, s)
If s happens to contain % characters, printing it directly with printf can cause it to access non-existent arguments, leading to undefined behavior (and this is a cause for some security vulnerabilities). Keep this naming convention in mind if you ever define your own printf-like variadic functions.
If I'm not mistaken, printf stands for "Print formatted data to stdout".
printf allows for formatting, while print doesnt. Also, print doesn't exist in C. I don't even know what printg is.

Extracting information from lines of a file in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I would like to extract file names and their corresponding MD5 sum from a check sum file in a format such as this-
MD5 (FreeBSD-8.2-RELEASE-amd64-bootonly.iso) = 2587cb3d466ed19a7dc77624540b0f72
I would prefer to do this locally within the program, which rules out awk and the like.
You can read lines easily enough using fgets(). Don't even think of using gets().
If you're reasonably confident you won't be dealing with filenames containing spaces or close parentheses, you can use sscanf() to extract the bits and pieces:
char hash_type[16];
char file_name[1024];
char hash_value[128];
if (sscanf(line, "%15s (%1023s) = %127s", hash_type, file_name, hash_value) == 3)
...good to go...
else
...something went wrong...
Note the sizes specified in the sscanf() string compared to the variable definitions; there isn't an easy way to generalize that other than by using snprintf() to create the format string:
char format[32];
snprintf(format, sizeof(format), "%%1%zus )%%%zus) = %%%zus",
sizeof(hash_type)-1, sizeof(file_name)-1, sizeof(hash_value)-1);
Your alternative is some routine forward parsing to locate the hash type and the open parenthesis before the start of the file name, and some trickier backwards parsing, skipping over the hash value and finding the equals and the last close parenthesis, and then collecting the various parts.
You should be able to implement this with fopen(), fgets() and strchr() - but first you will need to nail down the format of the file more precisely (for example: what happens if the filename includes a ) character?)
I wouldn't advocate it in most languages, but why not just hit it up with POSIX regex?

Resources