⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stream.cpp

📁 3个vxworks 程序
💻 CPP
字号:
//实现功能为,从一文本文件中读取数据
#include "stdio.h"
#include "memory.h"
#include "stdlib.h"
#include "afx.h"

#include "math.h"
#include <fstream.h>
#include <iostream.h>
#include <CString>
#include <time.h> //C语言的头文件

typedef struct Data_tag
{
	double	dPow;
	bool	bMark;
}Data;

int ExtractFile(char* sFileDir,int l,char* buffer);
int DirList();

// global variants
const int MAX_SIZE = 1000000;
const int CHANNEL = 2400;
const int TIME = 20;

Data g_SpecPow[CHANNEL][TIME];
//int g_iQsetCount = 10;
double g_dThreshold[CHANNEL];
//distance from the double g_dThresholdTotal
//double g_dThresholdTotalDev = 5;
double power[CHANNEL];

int main()
{

	time_t now; //实例化time_t结构 
	struct tm *timenow; //实例化tm结构指针 
	time(&now); 
	//time函数读取现在的时间(国际标准时间非北京时间),然后传值给now 
	timenow = localtime(&now); 
	//localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区) 
	printf("Local time is %s\n",asctime(timenow)); 
	//上句中asctime函数把时间转换成字符,通过printf()函数输出 
	ofstream Distimefile("time.txt",ios::out);
	if(!Distimefile.fail())
	{
		Distimefile<<asctime(timenow)<<"\n";
		Distimefile.close();
	}

	if(DirList() == -1)		// Start reading measurement file and extracing data...
	{
		return -1;
	}

	time(&now); 
	//time函数读取现在的时间(国际标准时间非北京时间),然后传值给now 
	timenow = localtime(&now); 
	//localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区) 
	printf("Local time is %s\n",asctime(timenow)); 
	//上句中asctime函数把时间转换成字符,通过printf()函数输出 
	ofstream Dis1timefile("time.txt",ios::app);
	if(!Dis1timefile.fail())
	{
		Dis1timefile<<asctime(timenow)<<"\n";
		Dis1timefile.close();
	}

	for(int m=0;m<CHANNEL;m++)
	{
		for(int n=0;n<TIME;n++)
		{
			power[m]+=g_SpecPow[m][n].dPow;
		}
		power[m]=power[m]/TIME;
	}
	ofstream outfile("abc.txt",ios::out);
	if(!outfile.fail())
	{
		for(int j=0;j<CHANNEL;j++)
		{
			CString d_temp;
			d_temp.Format("%f",power[j]);
			outfile<<d_temp<<"\n";
		}
	}
	outfile.close();


	time(&now); 
	//time函数读取现在的时间(国际标准时间非北京时间),然后传值给now 
	timenow = localtime(&now); 
	//localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区) 
	printf("Local time is %s\n",asctime(timenow)); 
	//上句中asctime函数把时间转换成字符,通过printf()函数输出 
	ofstream Dis2timefile("time.txt",ios::app);
	if(!Dis2timefile.fail())
	{
		Dis2timefile<<asctime(timenow)<<"\n";
		Dis2timefile.close();
	}


	return 0;
}

int ExtractFile(char *sFileDir,int l,char* buffer)		// para j is for different measure time
{
	FILE* fp;
	if((fp = fopen(sFileDir, "r")) == NULL)
	{
		printf("can not open the file, check file dir");
		return -1;
	}
	//将buffer数组清零
	memset(buffer, 0, MAX_SIZE);
	fread(buffer,sizeof(char),MAX_SIZE, fp);
//  extract data
    char  *P=buffer;
	int count=0,count1=0;
    while(count<2)
	{   
        count1++;
		if(*P++ =='\n')
			count++;
		else 
			continue;
	}
	if(fseek(fp,count1,0)!=0)
	{
		cout<<"Fseek function has something wrong !"<<endl;
		return 1;
	}
	fread(buffer,sizeof(char),MAX_SIZE, fp);
	for(int i=0;i<20;i++)
	{
		cout<<*(buffer+i)<<endl;
	}
	/*
    char *pStart = buffer,*pEnd = buffer;  
	
	for(int j=0;j<20;j++)
	{
		int i=0;
		while(i<CHANNEL)
		{
			if(*pEnd++ == '\n')	// seperator commar
			{
				g_SpecPow[i][j+l*20].dPow = atof(pStart);
				i++;
				pStart = pEnd;
				cout<<"正在读取"<<'\r';
			}
		}
		char  *P1;
	    P1=pEnd;
		int  count2=0,count3=0;
		while(count2<2400)
		{    
			count3++;
		    if(*P1++ =='\n')
			  count2++;
		    else 
			 continue;
		}
        pStart = pStart+count3+2400,pEnd =pEnd+count3+2400;  
          // cout<<count3<<endl;
	}
	if(!fclose(fp))
		//printf( "The time %d is finished\n",j );
		cout<<endl;
        cout<<"文件读取完毕"<<endl;
	*/
	return 0;
}


int DirList()	// list all measurement result files
{
	printf("\nStart reading measurement file and extracing data...\n\n");
    
	CFileFind finder;
	CString csDir(".\\Results");	// directory
    BOOL bFound = finder.FindFile(csDir+"\\*.*");	// file
	
	if(!bFound)
	{
		printf("no measure File found!\n");
		return -1;
	}
	
	char buffer[MAX_SIZE];

    int k=0;
	while (bFound)
	{
		bFound = finder.FindNextFile();
	  
		if(finder.IsDirectory())
		{
			continue;
		}
		printf("%s\n", finder.GetFileName());
		csDir = CString(".\\Results\\")+finder.GetFileName();
        
		ExtractFile(csDir.GetBuffer(1400),k,buffer);//max 100 bytes  
		if(bFound == 1)
		{k++;}
	
	}

	return 0;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -