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/

ExpandedBlockStart.gif 代码
/*  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/

ExpandedBlockStart.gif 代码
/*  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/

ExpandedBlockStart.gif 代码
#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/

ExpandedBlockStart.gif 代码
/*  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/

ExpandedBlockStart.gif 代码
/*  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/

ExpandedBlockStart.gif 代码
/*  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/
ExpandedBlockStart.gif 代码
/*  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/
ExpandedBlockStart.gif 代码
/*  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/
ExpandedBlockStart.gif 代码
/*  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/
ExpandedBlockStart.gif 代码
/*  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
*/

转载于:https://www.cnblogs.com/kddsly/archive/2010/09/30/1839657.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值