C语言输入输出与文件操作
代码
2. scanf : Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format tag within the format string. http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
3. puts: Writes the C string pointed by str to stdout and appends a newline character ('\n'). //int puts ( const char * str ); http://www.cplusplus.com/reference/clibrary/cstdio/puts/
#include <stdio.h>
int main ()
{
char string [] = "Hello world!";
puts (string);
}
4. putc: int putc(int character, FILE* stream); //int putc ( int character, FILE * stream ); http://www.cplusplus.com/reference/clibrary/cstdio/putc/
#include <stdio.h>
#include <process.h>
int main ()
{
FILE * pFile;
char c;
pFile=fopen("alphabet.txt","wt");
for (c = 'A' ; c <= 'Z' ; c++)
{
putc (c , pFile);
}
fclose (pFile);
system("alphabet.txt");
return 0;
}
5. fputc: Writes a character to the stream and advances the position indicator. //int fputc ( int character, FILE * stream ); http://www.cplusplus.com/reference/clibrary/cstdio/fputc/
#include <stdio.h>
#include <process.h>
int main ()
{
FILE * pFile;
char c;
pFile = fopen ("alphabet.txt","w");
if (pFile!=NULL)
{
for (c = 'A' ; c <= 'Z' ; c++)
{
fputc ( (int) c , pFile );
}
for (int i = 0 ; i < 128; ++i )
{
if ( 0 == (i % 10) )
{
putc('\n', pFile);
}
fputc(i, pFile);
}
fclose (pFile);
}
system("alphabet.txt");
return 0;
}
6. fputs: Writes the string pointed by str to the stream. //int fputs ( const char * str, FILE * stream ); Writes the string pointed by str to the stream.This final null-character is not copied to the stream. http://www.cplusplus.com/reference/clibrary/cstdio/fputs/
int main ()
{
FILE * pFile;
char sentence [256];
printf ("Enter sentence to append: ");
fgets (sentence,255,stdin);
pFile = fopen ("mylog.txt","a");
fputs (sentence,pFile);
fclose (pFile);
system("mylog.txt");
return 0;
}
7. getc:The internal file position indicator is then advanced by one character to point to the next character. //int getc ( FILE * stream ); http://www.cplusplus.com/reference/clibrary/cstdio/getc/
代码
8. gets: Reads characters from stdin and stores them as a string into str until a newline character ('\n') or the End-of-File is reached. The ending newline character ('\n') is not included in the strin. A null character ('\0') is automatically appended after the last character copied to str to signal the end of the C string.
//char * gets ( char * str ); http://www.cplusplus.com/reference/clibrary/cstdio/gets/
#include <stdio.h>
int main()
{
char string [256];
printf ("Insert your full address: ");
gets (string);
printf ("Your address is: %s\n",string);
return 0;
}
7. fgetc: The internal file position indicator is then advanced by one character to point to the next character. fgetc and getc are equivalent, except that the latter one may be implemented as a macro. //int fgetc ( FILE * stream );http://www.cplusplus.com/reference/clibrary/cstdio/fgetc/
#include <stdio.h>
int main ()
{
FILE * pFile;
int c;
int n = 0;
pFile=fopen ("myfile.txt","r");
if (pFile==NULL) perror ("Error opening file");
else
{
do {
c = fgetc (pFile);
if (c == '$') n++;
} while (c != EOF);
fclose (pFile);
printf ("The file contains %d dollar sign characters ($).\n",n);
}
return 0;
}
8. fgets: Reads characters from stream and stores them as a C string into str until (num-1) characters. //char * fgets ( char * str, int num, FILE * stream ); http://www.cplusplus.com/reference/clibrary/cstdio/fgets/
#include <stdio.h>
int main()
{
FILE * pFile;
char mystring [100];
pFile = fopen ("myfile.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else {
fgets (mystring , 100 , pFile);
puts (mystring);
fclose (pFile);
}
return 0;
}
9. fgetpos: Gets the information needed to uniquely identify the current value of the stream's position indicator and stores it in the location pointed by position. http://www.cplusplus.com/reference/clibrary/cstdio/fgetpos/
#include <stdio.h>
int main ()
{
FILE * pFile;
int c;
int n;
fpos_t pos;
pFile = fopen ("myfile.txt","r");
if (pFile==NULL) perror ("Error opening file");
else
{
c = fgetc (pFile);
printf ("1st character is %c\n",c);
fgetpos (pFile,&pos);
for (n=0;n<3;n++)
{
fsetpos (pFile,&pos);
c = fgetc (pFile);
printf ("2nd character is %c\n",c);
}
fclose (pFile);
}
return 0;
}
10. fsetpos: Changes the internal file position indicator associated with stream to a new position. a call to fsetpos allows to switch between reading and writing. //http://www.cplusplus.com/reference/clibrary/cstdio/fsetpos/
代码
(2)字符串
#include <stdio.h>
int main ()
{
char buffer [50];
int n, a=5, b=3;
n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
printf ("[%s] is a %d char long string\n",buffer,n);
return 0;
}
Output:
[5 plus 3 is 8] is a 13 char long string
#include <stdio.h>
int main ()
{
char sentence []="Rudolph is 12 years old";
char str [20];
int i;
sscanf (sentence,"%s %*s %d",str,&i);
printf ("%s -> %d\n",str,i);
return 0;
}
Output:
Rudolph -> 12
#include <stdio.h>
int main ()
{
FILE * pFile;
long size;
pFile = fopen ("myfile.txt","rb");
if (pFile==NULL) perror ("Error opening file");
else
{
fseek (pFile, 0, SEEK_END);
size=ftell (pFile);
fclose (pFile);
printf ("Size of myfile.txt: %ld bytes.\n",size);
}
return 0;
}
#include <stdio.h>
int main ()
{
int n;
FILE * pFile;
char buffer [27];
pFile = fopen ("myfile.txt","w+");
for ( n='A' ; n<='Z' ; n++)
fputc ( n, pFile);
rewind (pFile);
fread (buffer,1,26,pFile);
fclose (pFile);
buffer[26]='\0';
puts (buffer);
return 0;
}