『漫游』酷论坛>『海外生活』>C program problem!!! who can ..

C program problem!!! who can help me

ikarigendou@2004-05-10 13:41

no syntax error, but it can not compile................
this is requirement:
The bsplit Program
The bsplit program splits (i.e. breaks up) files into smaller files (which we’ll refer to as chunks). It can
take either 1 or 2 command-line arguments. The first argument (i.e. the one after the program name) is the
name of the file to split & must be specified; the second argument is optional & specifies the size of each
chunk (except for the last chunk, which may be smaller or larger — see below). The size of each chunk can
be given by an integer (which specifies the number of bytes) or by an integer immediately followed by the
letter k or K only (which specifies the number of kilobytes). (Note: 1 kilobyte = 1024 bytes.) If the size of
each chunk is not specified, a default size of 1000 bytes is used.
What are the names of the chunks? The names of the chunks are always x.00%, x.01%, x.02%, ..., upto
x.99% (i.e. the first chunk is named x.00%, the second x.01%, etc.) This means that there can be at most
100 chunks. In general, each chunk has the same size (specified by the second command-line argument, or
1000 bytes if not specfied) except for the last chunk, which may be smaller. However, if the original file is
so large that 100 chunks of the specified size are not sucient to hold it, then the first 99 chunks have the
specified size & the last chunk (x.99%) contains the rest of the file which will be larger than the specified
size. In this case, the program should print a warning message. Note that this is the only case where a
chunk (the last chunk) is larger than the specified size.
Let’s look at some examples. Assume that we have 2 files normal.dat & large.dat of sizes 2500 bytes
& 101,000 bytes respectively. Consider the invocations:
1
• bsplit normal.dat: split normal.dat into chunks of 1000 bytes. There should be 3 chunks x.00%,
x.01% & x.02%. x.00% should contain the first 1000 bytes of normal.dat, x.01% the next 1000 bytes
& x.02% the last 500 bytes of normal.dat
• bsplit normal.dat 2kb: this is not a valid invocation — 2kb is not a valid size (because of the extra
b)
• bsplit normal.dat 2k: split normal.dat into chunks of 2 kilobytes. There should be 2 chunks x.00%
& x.01% with x.00% containing the first 2048 bytes & x.01% containing the last 452 bytes
• bsplit large.dat: split large.dat into chunks of 1000 bytes. There should be 100 files: x.00%, ... ,
x.99%. The first 99 files should each have size 1000 bytes; the last file, x.99%, should have size 2000
bytes. A warning message should be printed.
Now, it may happen that we already have some files named x.??% (where each ? is a digit) when we are
splitting a file. In this case, just to make things simple, bsplit is allowed to silently overwrite those files.
(This means that we don’t have to write additional code to test for the existence of x.??% files.)
The program should print an error message & exit if a “fatal” error is encountered. This should happen,
for example, when the specified file cannot be read or when an output file cannot be written.
The source file should be named bsplit.c

this is my program:
#include
#include
#include

#define defsize 1024
#define filesize 5

int main(int argc, char *argv[]){
int c; // the charactor
int i; //copy size
int j; //the number of charactor
int k=0; //the number of the output files

FILE *ipf,*opf;
char buffer[defsize];
char outfile[filesize];
char input[defsize];

/*defult the how many bytes you want*/
i=1000;

/*check if the command line argument is correct*/
if((argc>2)){
fprintf(stderr,"the number of parameter is invalid\n");
/*close the whole program*/
exit(0);
}

if(sscanf(argv[1],"%s",input)!=1){
fprintf(stderr,"please input correct file name\n");
exit(0);
}



/*check if the size of input size is correct*/
if(sscanf(argv[2],"%d,%s",&i,buffer)==2){
/*check if the the mount of each chunk is valid*/
if((i<1)&&((buffer[0]=='k')||(buffer[0]=='K')))
j= i*defsize;

else
fprintf(stderr," the size of chunk is invalid\n");
/*close the program*/
exit(0);
}
else{
j=1000;
i=0;
}


/*open the file will be splited*/
if((ipf=fopen(input,"rb"))==NULL){
fprintf(stderr,"can not open the file\n");
exit(0);
}

k=0;
while(1){
sprintf(outfile,"x.%02d%",k++);
if((opf=fopen(outfile,"wb"))==NULL){
fprintf(stderr,"can ont open then output file\n");
exit(1);
}
while((c=(fgetc(ipf))!=EOF)){
if(sizeof(opf)<=j){
fputc(c,opf);
}
else{
fprintf(stderr,"the size of output file is invalid\n");
remove(outfile);
exit(0);
}
fclose(opf);
printf("%d byts\n",j);
}
}
fclose(ipf);
return 0;
}
引用

椎名可卡@2004-05-10 13:52

头晕啊~~~~看来是帮不了你了~~~
引用

sunny_rain@2004-05-10 14:08

That looks like it's the language of Mars... O_o
I'm never good at computer programming, afraid I can't help either~~
引用

阿苏@2004-05-11 00:24

gcc -Wall

看看有什么错误啊...
引用

| TOP