33,311
社区成员
发帖
与我相关
我的任务
分享
#include<string>
#include <tchar.h>
int main(int argc, _TCHAR* argv[])
{
FILE *fp=fopen("E:\\coffee\\MyTemp\\Price.txt","r+");
fseek(fp,0,SEEK_END);
int fileLen=ftell(fp);
char *pSaveH=new char[fileLen+1]; //(std::nothrow)
if(!pSaveH)
{
printf("pSaveH分配失败");
return 1;
}
memset(pSaveH,0,sizeof(char)*fileLen);
fseek(fp,0,SEEK_SET);
unsigned int bytesRead=fread(pSaveH,sizeof(char),fileLen,fp);
while (bytesRead != fileLen)
{
fseek(fp,bytesRead,SEEK_SET);
unsigned int bytesReadAgain = fread(pSaveH,sizeof(char),fileLen,fp);
bytesRead += bytesReadAgain;
}
fclose(fp);
printf("%s",pSaveH);
getchar();
return 0;
}
FILE * pFile;
long lSize;
char * buffer;
size_t result;
pFile = fopen ( "test.txt" , "rb" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
cout << lSize << endl;
rewind (pFile);
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
cout << result << endl;
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
/* the whole file is now loaded in the memory buffer. */
// terminate
fclose (pFile);
free (buffer);
getchar();
return 0;
#include "stdafx.h"
#include<string>
int _tmain(int argc, _TCHAR* argv[])
{
FILE *fp=fopen("E:\\coffee\\MyTemp\\Price.txt","r+");
fseek(fp,0,SEEK_END);
int fileLen=ftell(fp);
char *pSaveH=new(std::nothrow) char[fileLen+1];
if(!pSaveH)
{
printf("pSaveH分配失败");
return 1;
}
memset(pSaveH,0,sizeof(char)*fileLen);
fseek(fp,0,SEEK_SET);
unsigned int bytesRead=fread(pSaveH,sizeof(char),fileLen,fp);
fclose(fp);
printf("%s",pSaveH);
getchar();
return 0;
}
// console.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<string>
int _tmain(int argc, _TCHAR* argv[])
{
FILE *fp=fopen("E:\\coffee\\MyTemp\\Price.txt","r+");
fseek(fp,0,SEEK_END);
int fileLen=ftell(fp);
int len=800+fileLen;
char *pFruit=new(std::nothrow) char[len];
if(!pFruit){printf("pFruit分配失败"); return 1;}
memset(pFruit,0,sizeof(char)*len);
char *pSaveH=new(std::nothrow) char[fileLen+1];
if(!pSaveH) {delete[] pFruit;printf("pSaveH分配失败"); return 1;}
memset(pSaveH,0,sizeof(char)*fileLen);
fseek(fp,0,SEEK_SET);
unsigned int bytesRead=fread(pSaveH,sizeof(char),fileLen,fp);
fclose(fp);
if( pSaveH[strlen(pSaveH)-1]!='\n' || pSaveH[strlen(pSaveH)-2]=='\n' )
{//总LF个数=fileLen-strlen=(text+LF*2)-(text+LF*1)
printf("Price.txt结尾只能是1个\\n");
delete[] pSaveH;delete[] pFruit;
return 1;
}
return 0;
}