C文件操作之写入统计信息到文件头
2012-05-25 17:41
246 查看
有一个小需求,文本文件存储的10几个字段,包括用户信息,产品信息,和价格信息,每个字段用符号'|'隔开,比如9962519126|20120524143922|Umai:PROG/330664@BESTV.SMG.SMG|城中大盗[英]|null|80|0|5|90002|0|2012052414412604|1|0|0
要求第一行是记录数|费用总和
实现方式为:当组合完所要写入文件的信息后,先把记录数加1,费用相加,写入第一行,然后把游标定位在文件当前末尾,再写入信息内容。以此进行循环!
简单代码为:
#include "stdafx.h"
#include<stdio.h>
#include<string>
int main(int argc, char* argv[])
{
char Inf[3][20] = {0};
char strs[3][10];
int i=0;
int iFeeSum = 0;
int iCount = 0;
int index = 0;
char lz_Recond[20] = {0};
char temp[20] = {0};
//用户名称|产品名称|费用
strcpy(Inf[0],"you|90002|100\n");
strcpy(Inf[1],"me|90003|100\n");
strcpy(Inf[2],"she|90004|100\n");
printf("inf=%s\n",Inf[0]);
FILE* fp= fopen("Record.txt","w" );
if ( fp == NULL )
{
printf("Can not open MdnFile :%s\n" );
exit( -1 );
}
printf("inf=%s\n",Inf[i]);
while(i < 3)
{
strcpy(temp, Inf[i]);
char *p = strtok(temp, "|"); //分离出需要的字段
while(p != NULL)
{
strcpy(strs[index],p);
p = strtok(NULL, "|");
index++;
}
iFeeSum = iFeeSum+atoi(strs[2]);
iCount = iCount + 1;
index = 0;
sprintf(lz_Recond,"%d|%d\n",iFeeSum,iCount);
printf("lz_Recond=%s\n",lz_Recond);
rewind(fp); //游标定位在文件头
fputs(lz_Recond, fp);
fseek(fp,0,SEEK_END); //游标定位在文件当前位置
printf("inf=%s\n",Inf[i]);
fputs(Inf[i],fp);
i++;
}
fclose(fp);
return 0;
}
要求第一行是记录数|费用总和
实现方式为:当组合完所要写入文件的信息后,先把记录数加1,费用相加,写入第一行,然后把游标定位在文件当前末尾,再写入信息内容。以此进行循环!
简单代码为:
#include "stdafx.h"
#include<stdio.h>
#include<string>
int main(int argc, char* argv[])
{
char Inf[3][20] = {0};
char strs[3][10];
int i=0;
int iFeeSum = 0;
int iCount = 0;
int index = 0;
char lz_Recond[20] = {0};
char temp[20] = {0};
//用户名称|产品名称|费用
strcpy(Inf[0],"you|90002|100\n");
strcpy(Inf[1],"me|90003|100\n");
strcpy(Inf[2],"she|90004|100\n");
printf("inf=%s\n",Inf[0]);
FILE* fp= fopen("Record.txt","w" );
if ( fp == NULL )
{
printf("Can not open MdnFile :%s\n" );
exit( -1 );
}
printf("inf=%s\n",Inf[i]);
while(i < 3)
{
strcpy(temp, Inf[i]);
char *p = strtok(temp, "|"); //分离出需要的字段
while(p != NULL)
{
strcpy(strs[index],p);
p = strtok(NULL, "|");
index++;
}
iFeeSum = iFeeSum+atoi(strs[2]);
iCount = iCount + 1;
index = 0;
sprintf(lz_Recond,"%d|%d\n",iFeeSum,iCount);
printf("lz_Recond=%s\n",lz_Recond);
rewind(fp); //游标定位在文件头
fputs(lz_Recond, fp);
fseek(fp,0,SEEK_END); //游标定位在文件当前位置
printf("inf=%s\n",Inf[i]);
fputs(Inf[i],fp);
i++;
}
fclose(fp);
return 0;
}
相关文章推荐
- 写入文件操作 之 信息管理系统 c语言版
- 利用POI对MS Excel进行读写(可以设置写入文件的样式),利用tm-extractor对Word进行读取操作并将信息用(log4j)日志信息显示出
- linux sudo 重定向,实现只有系统管理员才有权限操作的文件中写入信息
- 文件部分-----(3)ASCII码文件读写操作----将学生信息写入到文件中,再从文件中读取信息
- (转)linux sudo 重定向,实现只有系统管理员才有权限操作的文件中写入信息
- 文件部分-----(6 )二进制文件读写操作----将学生信息写入到文件中,再从文件中读取信息
- 文件操作写入和读出结构体--修改文件信息
- 统计系统的登录信息 并写入到文件中
- win7中安装asp.net的问题 编译器错误信息: CS0016: 未能写入输出文件
- Python 获取磁盘信息的脚本及常用文件操作等
- 在Linux DeviceTree增添dtsi文件并在驱动中读取节点信息写入sys文件系统
- 深入分析FFmpeg之一-打开媒体文件并读取文件头信息
- Java中按照行读取和写入文件 日志常用操作 解决中文乱码问题
- 编译器错误信息:CS0016:未能写入输出文件 c:\WINDOWS\Microsoft.NET\Framework\...的解决办法
- 打开指针C++文件读写操作(一)将字母表写入TXT文本文件
- 登录对话框之VC中用INI文件保存登录信息的操作
- 文件基本操作之获取文件基本信息
- ★实验 8-2 1. 创建两个守候进程。每个守候进程各自创建独立的日志文件。 2. 每隔 1s 向日志文件中写入如下信息: a) 守候进程 1:未使用的内存大小(MemFree) b) 守
- Java 文件写入读取统计字符