C语言输入输出与文件操作

 

 

//这个文件主要是:测试C语言的输入输出
一: 输入输出
  (1)标准
1. printf:  Writes to the standard output (stdout) a sequence of data formatted as the format argument specifies   http://www.cplusplus.com/reference/clibrary/cstdio/printf/

 代码

 

         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/

/* puts example : hello world! */
#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/

代码
/* putc example: alphabet writer */
#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/

代码
/* fputc example: alphabet writer */
#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/

代码
#include <stdio.h>

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/

/* gets example */
#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/

代码
/* fgetc example: money counter */
#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/

代码
/* fgets example */
#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/

代码
/* fgetpos example */
#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/

 

 

 

 代码

 

 

 After this code is successfully executed, a file called myfile.txt will contain:
 This is a sample

 

 (2)字符串

1. sprintf: On success, the total number of characters written is returned. This count does not include the additional null-character automatically appended at the end of the string.    //int sprintf ( char * str, const char * format, ... );http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
代码
/* sprintf example */
#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 

2. sscanf: Reads data from str and stores them according to the parameter format into the locations given by the additional arguments  //格式: [=%[*][width][modifiers]type=]  http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/
代码
/* sscanf example */
#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 

3. strcpy
4. strrchr
5. strncmp
  (3)输入输出
        1. fprintf
2. fscanf
二: 文件操作
        1. fopen
2. fclose
3. ftell: Returns the current value of the position indicator of the stream.  http://www.cplusplus.com/reference/clibrary/cstdio/ftell/
代码
/* ftell example : getting size of a file */
#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;
}

 

 

4. fseek: Sets the position indicator associated with the stream to a new position defined by adding offset to a reference position specified by origin.    //int fseek ( FILE * stream, long int offset, int origin ); P http://www.cplusplus.com/reference/clibrary/cstdio/fseek/ 
5. rewind: Sets the position indicator associated with stream to the beginning of the file.  A call to rewind is equivalent to: fseek ( stream , 0L , SEEK_SET );  //void rewind ( FILE * stream ); http://www.cplusplus.com/reference/clibrary/cstdio/rewind/
代码
/* rewind example */
#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;
}

 

 

三: windows 文件操作
1. WIN32_FIND_DATA //文件信息
2. FindFirstFile
3. FindNextFile
4. DeleteFile
5. MoveFive
*/
posted @ 2010-09-30 18:01  木叶道  阅读(722)  评论(0编辑  收藏  举报