使用fread
和fwrite
复制二进制文件时出现问题:
循环只运行两次(40个字节),但文件长度为160个字节:
#include <string.h>
#define PER_READ 30
int main(void)
{
char buffer[500] = { 0 };
FILE* CSV = fopen( "CSV.csv", "rb" );
FILE* csvDest = fopen( "CSVDest.csv", "wb" );
unsigned int finished = 0;
unsigned int counter = 0;
do
{
finished = fread( buffer, sizeof( char*), PER_READ, CSV );//Read all from CSV to a string name buffer
finished += PER_READ * counter;
counter++;
} while (finished == PER_READ * counter);
fwrite( buffer, sizeof( char* ), finished, csvDest );// write all to a the file CSVDest
system( "PAUSE" );
return (0);
};
答案 0 :(得分:1)
你的实现可能是一个像这样的简单循环,它会重复,直到没有字节读取。请注意,sizeof(char*)
错误 - 指针的大小,sizeof(char)
的定义是1
。
#include <stdio.h> // include proper header
#define BUFFSIZE 512 // power of 2 is kind to system
int main(void)
{
char buffer[BUFFSIZE];
size_t bytes;
FILE *fin, *fou;
fin = fopen("CSV.csv", "rb");
fou = fopen("CSVDest.csv", "wb");
if(fin == NULL || fou == NULL)
return 1; // or other action
while ((bytes = fread(buffer, 1, BUFFSIZE, fin)) != 0) {
if(fwrite(buffer, 1, bytes, fou) != bytes) {
return 1; // or other action
}
}
fclose(fou);
fclose(fin);
return 0;
}
答案 1 :(得分:0)
#include <string.h>
#include <stdio.h>
#define PER_READ 30
int main( void )
{
char buffer[500] = { 0 };
FILE* CSV = fopen( "CSV.csv", "rb" );
FILE* csvDest = fopen( "CSVDest.csv", "wb" );
unsigned int finished = 0;
unsigned int counter = 0;
unsigned int numBlocksRead = 0;
do
{
numBlocksRead = fread( buffer, sizeof( char ), PER_READ, CSV );
finished += numBlocksRead*sizeof( char );
counter++;
fwrite( buffer, sizeof( char ), numBlocksRead, csvDest );
} while (finished == PER_READ * counter);
fclose( CSV );
fclose( csvDest );
system( "PAUSE" );
return (0);
}
答案 2 :(得分:0)
你这样做:
$("button").click(function(){
svgSource = phylocanvas.getSvgSource();
console.log(svgSource);
if(svgSource){
var hiddenElement = document.createElement('a');
document.body.appendChild(hiddenElement); // Add the element to the DOM
hiddenElement.setAttribute("type", "hidden"); // make it hidden
hiddenElement.href = 'data:attachment/text,' + encodeURI(svgSource);
hiddenElement.target = '_blank';
hiddenElement.download = '<TMPL_VAR NAME="TREENAME">.svg';
console.log(hiddenElement.download);
hiddenElement.click();
}
});
然后你比较:
finished = fread( buffer, sizeof(char), PER_READ, CSV );
finished += PER_READ * counter;
预计这会怎么样?