c/c++ 数据文件读入测试程序

// file_operate_test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "stdlib.h"
#include   
#include 


#define byte unsigned char

using namespace std;

typedef struct A{
unsigned int para1;
unsigned short para2;
unsigned int para3;
unsigned short para4;
}A;

int _tmain(int argc, _TCHAR* argv[])
{
// printf(“hello world!! sizeof = %d \n”,sizeof(int));
// system(“pause”);
//return 0;

int a[100];
byte *p;
int i = 0;
int j = 0;
A test4;
test4.para1 = 20;
test4.para2 = 15;
test4.para3 = 20;
test4.para4 = 15;
p = (byte*)(&test4);
FILE* fp=fopen("d:\\new_2.dat","r"); 
FILE* fw=fopen("d:\\new_3.dat","w"); 
FILE* fw2=fopen("d:\\new_4.dat","w"); 
if(fp==NULL) 
{
    printf("无文件");
    return -1;
}
while(!feof(fp))
{
        fscanf(fp,"%x",&a[i]);
        if(a[i] == 0xcccccccc){break;}
        printf("十六进制  %x ,十进制 %d ",a[i],a[i]);
        fputs("0x",fw);
        fprintf(fw,"%x\n",a[i]);
        i++;
    printf("\n");
}
printf("unsigned short = %d, int = %d",sizeof(unsigned short),sizeof(int));
printf("i ======== %d\n", i);
for(i=0;i<8;i++)
{
    //if(i%2 == 0 ){printf("\n");}
    printf("%x\n",p[i]);
    fputs("0x",fw2);
    fprintf(fw2,"%0x\n",p[i]);

}

fclose(fp); 
fclose(fw);
fclose(fw2);
system("pause");
return 0;  

}

你可能感兴趣的:(c++小程序)