c 如何读取大文件?(在线等待)

dd97911 2005-05-20 10:56:39
我用一个文件t1.cfg 内容如下:
DIR=`pwd`/openssl
PRIV=$DIR/private

mkdir $DIR $PRIV $DIR/newcerts
cp /usr/share/ssl/openssl.cnf $DIR
replace ./demoCA $DIR -- $DIR/openssl.cnf

# Create necessary files: $database, $serial and $new_certs_dir
# directory (optional)

touch $DIR/index.txt
echo "01" > $DIR/serial

#
# Generation of Certificate Authority(CA)
#




openssl req -new -x509 -keyout $PRIV/cakey.pem -out $DIR/cacert.pem \
-config $DIR/openssl.cnf

# Sample output:
# Using configuration from /home/monty/openssl/openssl.cnf
# Generating a 1024 bit RSA private key
# ................++++++
# .........++++++
# writing new private key to '/home/monty/openssl/private/cakey.pem'
# Enter PEM pass phrase:
# Verifying password - Enter PEM pass phrase:
# -----
# You are about to be asked to enter information that will be incorporated
# into your certificate request.
# What you are about to enter is what is called a Distinguished Name or a DN.
# There are quite a few fields but you can leave some blank
# For some fields there will be a default value,
# If you enter '.', the field will be left blank.
# -----
# Country Name (2 letter code) [AU]:FI
# State or Province Name (full name) [Some-State]:.
# Locality Name (eg, city) []:
# Organization Name (eg, company) [Internet Widgits Pty Ltd]:MySQL AB
# Organizational Unit Name (eg, section) []:
# Common Name (eg, YOUR name) []:MySQL admin
# Email Address []:

#
# Create server request and key
#



openssl req -new -keyout $DIR/server-key.pem -out \
$DIR/server-req.pem -days 3600 -config $DIR/openssl.cnf

# Sample output:
# Using configuration from /home/monty/openssl/openssl.cnf
# Generating a 1024 bit RSA private key
# ..++++++
# ..........++++++
# writing new private key to '/home/monty/openssl/server-key.pem'
# Enter PEM pass phrase:
# Verifying password - Enter PEM pass phrase:
# -----
# You are about to be asked to enter information that will be incorporated
# into your certificate request.
# What you are about to enter is what is called a Distinguished Name or a DN.
# There are quite a few fields but you can leave some blank
# For some fields there will be a default value,
# If you enter '.', the field will be left blank.
# -----
# Country Name (2 letter code) [AU]:FI
# State or Province Name (full name) [Some-State]:.
# Locality Name (eg, city) []:
# Organization Name (eg, company) [Internet Widgits Pty Ltd]:MySQL AB
# Organizational Unit Name (eg, section) []:
# Common Name (eg, YOUR name) []:MySQL server
# Email Address []:
#



# Please enter the following 'extra' attributes
# to be sent with your certificate request
# A challenge password []:
# An optional company name []:

#
# Remove the passphrase from the key (optional)
#

openssl rsa -in $DIR/server-key.pem -out $DIR/server-key.pem

#
# Sign server cert
#
openssl ca -policy policy_anything -out $DIR/server-cert.pem \
-config $DIR/openssl.cnf -infiles $DIR/server-req.pem

# Sample output:
# Using configuration from /home/monty/openssl/openssl.cnf
# Enter PEM pass phrase:
# Check that the request matches the signature
# Signature ok
# The Subjects Distinguished Name is as follows
# countryName :PRINTABLE:'FI'
# organizationName :PRINTABLE:'MySQL AB'
# commonName :PRINTABLE:'MySQL admin'
# Certificate is to be certified until Sep 13 14:22:46 2003 GMT (365 days)
# Sign the certificate? [y/n]:y
#
#
# 1 out of 1 certificate requests certified, commit? [y/n]y
# Write out database with 1 new entries
# Data Base Updated

#
# Create client request and key
#
openssl req -new -keyout $DIR/client-key.pem -out \
$DIR/client-req.pem -days 3600 -config $DIR/openssl.cnf

# Sample output:
# Using configuration from /home/monty/openssl/openssl.cnf
# Generating a 1024 bit RSA private key
# .....................................++++++
# .............................................++++++
# writing new private key to '/home/monty/openssl/client-key.pem'
# Enter PEM pass phrase:
# Verifying password - Enter PEM pass phrase:
# -----
# You are about to be asked to enter information that will be incorporated
# into your certificate request.
# What you are about to enter is what is called a Distinguished Name or a DN.
# There are quite a few fields but you can leave some blank
# For some fields there will be a default value,
# If you enter '.', the field will be left blank.
# -----
# Country Name (2 letter code) [AU]:FI
# State or Province Name (full name) [Some-State]:.
# Locality Name (eg, city) []:
# Organization Name (eg, company) [Internet Widgits Pty Ltd]:MySQL AB
# Organizational Unit Name (eg, section) []:
# Common Name (eg, YOUR name) []:MySQL user
# Email Address []:
#
# Please enter the following 'extra' attributes
# to be sent with your certificate request
# A challenge password []:
# An optional company name []:

#
# Remove a passphrase from the key (optional)
#
openssl rsa -in $DIR/client-key.pem -out $DIR/client-key.pem

#
# Sign client cert
#

openssl ca -policy policy_anything -out $DIR/client-cert.pem \
-config $DIR/openssl.cnf -infiles $DIR/client-req.pem

# Sample output:
# Using configuration from /home/monty/openssl/openssl.cnf
# Enter PEM pass phrase:
# Check that the request matches the signature
# Signature ok
# The Subjects Distinguished Name is as follows
# countryName :PRINTABLE:'FI'
# organizationName :PRINTABLE:'MySQL AB'
# commonName :PRINTABLE:'MySQL user'
# Certificate is to be certified until Sep 13 16:45:17 2003 GMT (365 days)
# Sign the certificate? [y/n]:y
#
#
# 1 out of 1 certificate requests certified, commit? [y/n]y
# Write out database with 1 new entries
# Data Base Updated

#
# Create a my.cnf file that you can use to test the certificates
#

cnf=""
cnf="$cnf [client]"
cnf="$cnf ssl-ca=$DIR/cacert.pem"
cnf="$cnf ssl-cert=$DIR/client-cert.pem"
cnf="$cnf ssl-key=$DIR/client-key.pem"
cnf="$cnf [mysqld]"
cnf="$cnf ssl-ca=$DIR/cacert.pem"
cnf="$cnf ssl-cert=$DIR/server-cert.pem"
cnf="$cnf ssl-key=$DIR/server-key.pem"
echo $cnf | replace " " '
' > $DIR/my.cnf

#
# To test MySQL

mysqld --defaults-file=$DIR/my.cnf &

mysql --defaults-file=$DIR/my.cnf
///////////////////////////////////
我用fread读,代码如下:
void ReadCfgData()
{
FILE *fp=fopen("t1.cfg","r");
if(fp)
{
long m=0;
char *str=new char[1024*300];
memset(str,0,1024*300);
while(!feof(fp))
{

m=fread(fp,1,1024*300);
}
delete []str;
}
}
结果str中村的数据比t1.cfg中的数据少?请问我怎样可以把t1.cfg中的数据都读到str中?请各大侠指教!!!!
...全文
420 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
kugou123 2005-05-20
  • 打赏
  • 举报
回复
大文件一般采用二进制方式读取。所以你打开的时候,用"rb"选项。
kugou123 2005-05-20
  • 打赏
  • 举报
回复
//先取文件大小,也就是获得总字节数nSize

long nSize=0,nSize2=0,nReaded=0,nFileLength=0;
char sFileBuffer[1024]; //缓冲区1KB
FILE *fp1,*fp2;
fp1=fopen("t1.cfg","rb");
while(nReaded=(fread(sFileBuffer,sizeof(char),1024,fp1)))
{
nSize+=nReaded;
memset(sFileBuffer,'\0',sizeof(sFileBuffer)); //清空缓冲区
}

fclose(fp1);

//然后开始根据文件大小分配空间,读取数据
char *pMem=NULL,*pTempMem=NULL;
nReaded=0;
fp2=fopen("t1.cfg","rb");
pMem=(char *)malloc(sizeof(char)*nSize); //根据目标文件大小,分配适当的内存空间
memset(pMem,'\0',nSize); //初始化空间
memset(sFileBuffer,'\0',1024);
pTempMem=pMem; //保存空间首地址

while(nSize2<nSize) //判断是否读取完毕
{
nReaded=(fread(sFileBuffer,sizeof(char),1024,fp2));
nSize2+=nReaded;
memcpy(pMem,sFileBuffer,nReaded); //复制到pMem内存空间
pMem+=nReaded; //指针后移
memset(sFileBuffer,'\0',sizeof(sFileBuffer)); //清空缓冲区
} //end while
pMem=pTempMem; //恢复首地址
fclose(fp2);

这样完成了大文件读取到内存中的操作。pMem为文件数据首地址
lhj0532 2005-05-20
  • 打赏
  • 举报
回复
改成
m=fread(fp,1,1024*1024);
是否读的数据会多?
dd97911 2005-05-20
  • 打赏
  • 举报
回复
to:orbit(走了走了)
m=fread(str,1024*300,1,fp);
和前面的结果一样!!!:(
  • 打赏
  • 举报
回复
不使用循环,一次读完试试:


m=fread(str,1024*300,1,fp);
dd97911 2005-05-20
  • 打赏
  • 举报
回复
TO:idAnts(你才无聊呢)
我试过,但一样,读到的数据比文件中的小。
int n=fread(str,1,1024*300,fp);
n的值比文件的小
idAnts 2005-05-20
  • 打赏
  • 举报
回复
FILE *fp=fopen("t1.cfg","rb");
试试。
idAnts 2005-05-20
  • 打赏
  • 举报
回复
FILE *fp=fopen("t1.cfg","rb");
试试。
lhj0532 2005-05-20
  • 打赏
  • 举报
回复
fread的大小比文件内容小。

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧