c语言如何先写文件体,再写文件头

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. // 测试时使用的文件  
  5. #define SRC_FILE_NAME     (const char *)"test_a.txt"  
  6. // 文件头预留64个字节  
  7. #define FILE_HEADER_LEN   64  
  8. /*============================================================================== 
  9. 函 数 名 : GetFile 
  10. 功    能 : 获取文件句柄 
  11. 算法实现 : 无 
  12. 参    数 : [in] const char *pFileName - 文件名 
  13. 返 回 值 : 成功-得到的文件句柄,失败-NULL 
  14. 日    期 : 2011/02/11 
  15. 作    者 : jernymy 
  16. ==============================================================================*/  
  17. static FILE *GetFile(const char *pFileName)  
  18. {  
  19.     // 使用rt+的方式打开文件  
  20.     FILE *fpSrc = fopen(pFileName, "rt+");  
  21.       
  22.     if (NULL != fpSrc)  
  23.     {  
  24.         // 文件存在  
  25.         return fpSrc;  
  26.     }  
  27.     printf("open %s /"rt+/" fail, may be create first!/n", pFileName);  
  28.     // 创建文件  
  29.     fpSrc = fopen(pFileName, "wt");  
  30.     if (NULL != fpSrc)  
  31.     {  
  32.         // 文件创建成功  
  33.         printf("create %s /"wt/" succ, pointer:%p!/n", pFileName, fpSrc);  
  34.     }  
  35.     else  
  36.     {  
  37.         // 文件创建失败  
  38.         printf("create %s /"wt/" fail, pointer:%p!/n", pFileName, fpSrc);  
  39.     }  
  40.     return fpSrc;  
  41. }  
  42.   
  43. /*============================================================================== 
  44. 函 数 名 : WriteFile 
  45. 功    能 : 写文件操作 
  46. 算法实现 : 无 
  47. 参    数 : [in] const char *pFileName - 文件名 
  48.            [in] const char *pchStr    - 写入的字符串buffer 
  49. 返 回 值 : 成功-0,失败--1 
  50. 日    期 : 2011/02/11 
  51. 作    者 : jernymy 
  52. ==============================================================================*/  
  53. static int WriteFile(const char *pFileName, const char *pchStr)  
  54. {  
  55.     FILE *fpSrc = NULL;  
  56.     int  nFileLen;  
  57.     if (NULL == pFileName)  
  58.     {  
  59.         printf("pFileName is NULL, exit!/n");  
  60.         return -1;  
  61.     }  
  62.     if (NULL == pchStr)  
  63.     {  
  64.         printf("pchStr is NULL, exit!/n");  
  65.         return -1;  
  66.     }  
  67.     fpSrc = GetFile(pFileName);  
  68.     if (NULL == fpSrc)  
  69.     {  
  70.         printf("get file fail! exit/n");  
  71.         return -1;  
  72.     }  
  73.       
  74.     // 得到文件大小-文件长度  
  75.     fseek(fpSrc, 0L, SEEK_END);  
  76.     nFileLen = ftell(fpSrc);  
  77.     // 写文件头后面部分  
  78.     if (0 == nFileLen)  
  79.     {  
  80.         nFileLen = FILE_HEADER_LEN;  
  81.     }  
  82.     fseek(fpSrc, nFileLen, SEEK_SET);  
  83.     if (FILE_HEADER_LEN == nFileLen)  
  84.     {  
  85.         fprintf(fpSrc, "/n");  
  86.     }  
  87.     fprintf(fpSrc, "%s/n", pchStr);  
  88.     // 写文件头部分  
  89.     fseek(fpSrc, 0L, SEEK_END);  
  90.     nFileLen = ftell(fpSrc);  
  91.     fseek(fpSrc, 0L, SEEK_SET);  
  92.     fprintf(fpSrc, "File size:%09d", nFileLen);  
  93.     // 关闭文件  
  94.     if (NULL != fpSrc)  
  95.     {  
  96.         fclose(fpSrc);  
  97.         fpSrc = NULL;  
  98.     }  
  99.     return 0;  
  100. }  
  101.   
  102. /*============================================================================== 
  103. 函 数 名 : main 
  104. 功    能 : 程序主函数 
  105. 算法实现 : 无 
  106. 参    数 : 无 
  107. 返 回 值 : 成功-0,失败--1 
  108. 日    期 : 2011/02/11 
  109. 作    者 : jernymy 
  110. ==============================================================================*/  
  111. int main(void)  
  112. {  
  113.     int nIdx;  
  114.     char achBuf[20] = {0};  
  115.     for (nIdx = 0; nIdx < 20; nIdx++)  
  116.     {  
  117.         sprintf(achBuf, "Index:%d%c", nIdx, 0);  
  118.         WriteFile(SRC_FILE_NAME, achBuf);  
  119.     }  
  120.     return 0;  
  121. }  

文章标签:  语言 c null file header 算法

猜你喜欢

转载自blog.csdn.net/bruce135lee/article/details/80757792
今日推荐