- 论坛徽章:
- 0
|
关于文件操作的问题,请大虾帮忙!!急!
我的更改如下
- /*#include <stdafx.h>;*/
- #include <stdlib.h>;
- #include <fcntl.h>;
- #include <io.h>;
- #include <string.h>;
- #include <stdio.h>;
- /////////////////////////////////////////////////////////////////////////////
- // 函 数 名 : openSaveFile
- //
- // 概要 : 打开保存的文件,如果文件不存在则创建一个新文件
- // 参数 : filepath 保存的文件路径
- // 返回値 : infile 保存文件成功
- // NULL 保存失败
- /////////////////////////////////////////////////////////////////////////////
- FILE* openSaveFile( char* filePath )
- {
- /* char sdrive[32];
- char sdir[32];
- char sfile[64];
- char sext[64];
- int cmp;
- */
- FILE *infile;
-
-
- infile = NULL;
- /*用来判断输入的文件名是否是.csv
- fnsplit( filePath,sdirve,sdir,sfile,sext );
- cmp = strcmp( sext, ".csv");
- if( cmp != 0){
- printf( "输入的文件名的后缀名不正确,应该为.csv格式" );
- return NULL;
- }
- */
- infile = fopen(filePath,"r+" );
- if( infile == NULL )
- {
- printf( "%s","文件时空的");
- return NULL;
- }
- return infile;
-
- }
- int saveSysHead( FILE* pfile )
- {
-
- int handle;
- long length;
-
- if (pfile == NULL)
- {
- printf("文件为空\n");
- return -1;
- }
- handle = fileno( pfile );
- length = filelength( handle );
- printf( "%s",length);
-
-
- //若文件为空,则写入文件头
- if( length == 0 )
- {
- fputs("shuru yige wenjian biaoti ", pfile);
- fclose( pfile );
- return 0;
- }
- else
- {
- fclose( pfile );
- }
-
- return 0;
- }
- void main()
- {
-
- char filename[128];
- FILE* infile;
-
- printf("please input save file path");
- scanf("%s",filename);
-
- //检查输入的文件名是否存在,如果不存在则创建该文件
- infile = openSaveFile( filename );
-
- //检查指定的文件中是否包含文件头,如果不包含,则写入文件头
- saveSysHead(infile);
- }
复制代码 |
|