Operating System Used:Ubuntu 11.04
Compiler Used: GCC
プログラム関連のファイル:Git Hub Linkバッファにファイルの内容をコピーすることができません
私はCPP(Cプリプロセッサ)Iのと同じ仕事をするプログラムを実装しようとしています.cファイルをコンパイルします。
この特定のコードでは、Copy_file_to_buf関数はファイル全体をバッファにコピーしません。 アブサルサイズは117406C
ですが、copy_file_to_buf関数のftellは114689
と表示されています。
EDIT: あり、私は同じプログラムを使用してバッファに
dummyfile
の内容をコピーしたデータの損失はありませんが、私はtemp.c
ファイル内の別々のプログラムをcopy_file_to_buf書いています。temp.c
#include<stdio.h>
main(int argc,char **argv)
{
FILE *inputFile;
int sizeofFile, rc;
char *source_buf;
fprintf(stderr, "In Copy_file_to_buf\n");
sleep(1);
inputFile=fopen(argv[1],"r");
if (!inputFile) {
fprintf(stderr, "Oops, failed to open inputfile \"%s\"\n", argv[1]);
return NULL;
}
fseek(inputFile,0,SEEK_END);
sizeofFile=ftell(inputFile);
fseek(inputFile,0,SEEK_SET);
source_buf=calloc(1,1+sizeofFile);
rc = fread(source_buf,sizeofFile,1,inputFile);
/* now check rc */
fprintf(stderr, "Size of the file=%d; fread returned %d.\n", sizeofFile, rc);
//sleep(5);
fclose(inputFile);
source_buf[sizeofFile] = 0;
puts(source_buf);
return source_buf;
}
はfseek関数のように見える()と次のコードのために予想されるようにftellは動作していません。
puts("Copying dummyfile contents");
test_buf=Copy_file_to_buf("dummyfile");
puts(test_buf);
ファイル名:preprocessor.c
#include"myheader.h"
/* agrv[1]=preprocessor
* argv[2]=test.c
*
* Program on PREPROCESSOR
*
* Steps:
* 1.Removal of comments.
* 2.Inclusion of headerfiles.
* 3.Macro substitution.
* a.function like arguments
* b.Stringification
* c.Concatenation
* 4.Conditional compilation
* a.#Ifdef
* b.#If
* c.#defined
* d.#Else
* e.#Elif
*/
int
main(int argc, char *argv[])
{
char *source_buf,*subBuf,*rmc_buf,*test_buf;
char **main_header_names,**sub_header_names;
int main_header_count,sub_header_count;
source_buf=(char *)Copy_file_to_buf(argv[1]);//...
rmc_buf=removeComments(source_buf);//...
main_header_names=(char **)getMainHeaderNames(rmc_buf);//...
source_buf=(char *)Copy_file_to_buf(argv[1]);//...
rmc_buf=removeComments(source_buf);//...
main_header_count=mainHeaderCounter(rmc_buf);//...
printf("Main Header Count=%d",main_header_count);//...
includeHeaders(main_header_names,main_header_count);
subBuf=(char *)Copy_file_to_buf("pre.i");//...
sub_header_names=(char **)getSubHeadersNames(subBuf);//...
subBuf=(char *)Copy_file_to_buf("pre.i");//...
sub_header_count=subHeadersCounter(subBuf);//...
WriteSubHeadersToFile(sub_header_count,sub_header_names,"dummyfile");//...
puts("Copying dummyfile contents");
test_buf=Copy_file_to_buf("dummyfile");
puts(test_buf);
/*test_buf=removeComments(test_buf);
puts(test_buf);
sub_header_names=(char **)getSubHeadersNames(test_buf);
test_buf=(char *)Copy_file_to_buf("dummyfile");
sub_header_count=subHeadersCounter(test_buf);
WriteSubHeadersToFile(sub_header_count,sub_header_names,"dummyfile2");
printf("Line:%d File:%s",__LINE__,__FILE__);
*/
return 0;
}
ファイル名:CopyFile.c
#include"myheader.h"
//Copying input file data into source_buf
char * Copy_file_to_buf(char *fileName)
{
FILE *inputFile;
int sizeofFile;
char *source_buf;
puts("In Copy_file_to_buf");
inputFile=fopen(fileName,"r");
fseek(inputFile,0,2);
sizeofFile=ftell(inputFile);
sizeofFile++;
fseek(inputFile,0,0);
source_buf=calloc(1,sizeofFile);
fread(source_buf,sizeofFile,1,inputFile);
printf("SIZE OF THE FILE=%d",sizeofFile);
//sleep(5);
fclose(inputFile);
return source_buf;
}
あなたが一つのブロックを読み取るために
fread
を求めている
Microsoftプラットフォームを使用している場合、 'fopen(fileName、" rb ");'が必要な場合があります。 – wildplasser
@wildplasser 私はUnbuntu 11.04で上記のコードをコンパイルしました。 私は 'fopen(fileName、" rb ");'を試しましたが、結果は同じでした。 – SlashQB
マジック定数の代わりに定義済みのマクロ( 'SEEK_SET'、' SEEK_CUR'、または 'SEEK_END')を使用してください。 – pmg