Anda di halaman 1dari 6

La funcin fgetc retorna el carcter siguiente desde el stream de entrada apuntado

por stream. Si el stream est en el final de fichero, el indicador del final de fichero
para el stream es activado y fgetc retorna EOF. Si ocurre un error de lectura, el
indicador de error para el stream es activado y fgetc retorna EOF.

Ejemplo:
^

#include <stdio.h>
int main()
{
char nombre[10]="datos.dat";
FILE *fichero;
int i;
fichero = fopen( nombre, "r" );
printf( "Fichero: %s -> ", nombre );
if( fichero )
printf( "existe (ABIERTO)\n" );
else
{
printf( "Error (NO ABIERTO)\n" );
return 1;
}

fputc
int fputc ( int character, FILE * stream );
Write character to stream

Writes a character to the stream and advances the position indicator.


The character is written at the position indicated by the internal position indicator of
the stream, which is then automatically advanced by one.

Parameters
character
The int promotion of the character to be written.
The value is internally converted to an unsigned char when written.
stream
Pointer to a FILE object that identifies an output stream.

Return Value
On success, the character written is returned.
If a writing error occurs, EOF is returned and the error indicator (ferror) is set.

Example
1 /* fputc example: alphabet writer */
2 #include <stdio.h>
3
4 int main ()
5{
6 FILE * pFile;
7 char c;
8
9 pFile = fopen ("alphabet.txt","w");
10 if (pFile!=NULL) {
11
12
for (c = 'A' ; c <= 'Z' ; c++)
13
fputc ( c , pFile );
14
15
fclose (pFile);
16 }
17 return 0;
18 }

Edit & Run

This program creates a file called alphabet.txt and


writes ABCDEFGHIJKLMNOPQRSTUVWXYZ to it.
In these days I'm playing with the C functions of atol(), atof() and atoi(), from a blog post I find
a tutorial and applied:
Here are my results:
void main()
{
char a[10],b[10];
puts("Enter the value of a");
gets(a);
puts("Enter the value of b");
gets(b);
printf("%s+%s=%ld and %s-%s=%ld",a,b,(atol(a)+atol(b)),a,b,(atol(a)-atol(b)));
getch();
}
There is atof() which returns the float value of the string and atoi() which returns integer
value.
Now to see the difference between the 3 I checked this code:
main()
{
char a[]={"2545.965"};
printf("atol=%ld\t atof=%f\t atoi=%d\t\n",atol(a),atof(a),atoi(a));
}
The output will be

atol=2545 atof=2545.965000 atoi=2545


char a[]={heyyou};
Now when you run the program the following will be the output (why?, is there any solution to
convert pure strings to integer?)
atol=0 atof=0 atoi=0
The string should contain the numeric value. Now modify this program as
char a[]={007hey};
The output in this case (tested in Red Hat Linux) will be
atol=7 atof=7.000000 atoi=7
so the functions have taken 007 only, not the remaining part (why?).
Now consider this
char a[]={hey007?};
The output of the program will be
atol=0 atof=0.000000 atoi=0
So I just want to convert my strings to numbers and then again to the same text. I played with
these functions, and, as you see, I'm getting really interesting results.
Why is that?
Are there any other functions to convert from/to string/integer and vice versa?
EDIT:
So as an input, if I take some names, or whatever, I will convert them to integers/floats... then
apply some other functions.
Also I'm curious about if I will take the same output with the same inputs when I use any of
your suggestions?

atof, _atof_l, _wtof, _wtof_l


Visual Studio 2013

Other Versions

Convert a string to double.


double atof(
const char *str
);
double _atof_l(
const char *str,
_locale_t locale
);

double _wtof(
const wchar_t *str
);
double _wtof_l(
const wchar_t *str,
_locale_t locale
);

Parameters
str
String to be converted.
locale
Locale to use.

Return Value
Each function returns the double value produced by interpreting the input characters as a
number. The return value is 0.0 if the input cannot be converted to a value of that type.
In all out-of-range cases, errno is set to ERANGE. If the parameter passed in is NULL, the
invalid parameter handler is invoked, as described in Parameter Validation. If execution is
allowed to continue, these functions set errno to EINVAL and return 0.

Remarks
These functions convert a character string to a double-precision, floating-point value.
The input string is a sequence of characters that can be interpreted as a numerical value of
the specified type. The function stops reading the input string at the first character that it
cannot recognize as part of a number. This character may be the null character ('\0' or L'\0')
terminating the string.
The str argument to atof and _wtof has the following form:
[whitespace] [sign] [digits] [.digits] [ {d | D | e | E }[sign]digits]
A whitespace consists of space or tab characters, which are ignored; sign is either plus (+)
or minus (); and digits are one or more decimal digits. If no digits appear before the
decimal point, at least one must appear after the decimal point. The decimal digits may be
followed by an exponent, which consists of an introductory letter (d, D, e, or E) and an
optionally signed decimal integer.
The versions of these functions with the _l suffix are identical except that they use the
locale parameter passed in instead of the current locale.
Generic-Text Routine Mappings
TCHAR.H routine

_UNICODE & _MBCS not defined

_tstof

atof

_ttof

atof

Requirements
Routine(s)

Required heade

atof

<math.h> and <

_atof_l

<math.h> and <

_wtof, _wtof_l

<stdlib.h> or <w

Example
This program shows how numbers stored as strings can be converted to numeric values
using the atof function.
//
//
//
//
//

crt_atof.c
This program shows how numbers stored as
strings can be converted to numeric
values using the atof function.

#include <stdlib.h>
#include <stdio.h>
int main( void )
{
char
*str = NULL;
double value = 0;
// An example of the atof function
// using leading and training spaces.
str = " 3336402735171707160320 ";
value = atof( str );
printf( "Function: atof( \"%s\" ) = %e\n", str, value );
// Another example of the atof function
// using the 'd' exponential formatting keyword.
str = "3.1412764583d210";
value = atof( str );
printf( "Function: atof( \"%s\" ) = %e\n", str, value );
// An example of the atof function
// using the 'e' exponential formatting keyword.
str = " -2309.12E-15";
value = atof( str );
printf( "Function: atof( \"%s\" ) = %e\n", str, value );
}
Function: atof( " 3336402735171707160320 " ) = 3.336403e+021
Function: atof( "3.1412764583d210" ) = 3.141276e+210
Function: atof( " -2309.12E-15" ) = -2.309120e-012

.NET Framework Equivalent

See Also
Reference
Data Conversion
Floating-Point Support

System::Convert::ToSingle
System::Convert::ToDouble

Locale
_ecvt
_fcvt
_gcvt
setlocale, _wsetlocale
_atodbl, _atodbl_l, _atoldbl, _atoldbl_l, _atoflt _atoflt_l

Anda mungkin juga menyukai