简体   繁体   中英

Read and write bytes in C (from/in file)

I have to solve the following problem in C for the operating systems class:

At the command line three file are given, two for input, one for output. The output file is obtained from input files, as follows: 10 bytes from the first file, 20 bytes from the second file, next 10 bytes from the first file, next 20 bytes from the second file and so on, until an input file is finished.

Simple but inefficient solution: read and write groups of 10 / 20 bytes. An efficient (but not simple) solution: read and write blocks with 5000 bytes.

I tried this for the simple solution. But it doesn't work... Can anyone help me?

#include<stdio.h>

int main(int argc, char* argv[], char* envp[]){
if (argc != 4){
    printf("%s", "Usage: c4h filename\n");
    return 0;
}

int c1,c2,i;
FILE *in1;
FILE *in2;
FILE *out;

in1 = fopen(argv[1], "r");
in2 = fopen(argv[2], "r");
out = fopen(argw[3], "wb");


if (in1 == NULL){
    printf("%s", "File not found!\n");
    return 0;
}

if (in2 == NULL){
    printf("%s", "File not found!\n");
    return 0;
}

while(feof(in1)!=0 && feof(in2)!=0){
    for(int i=0;i<20;i++)
        if(feof(in1)!=0){
            c1 = fgetc(in1);
            fputc(c1,out);
        }

    for(int i=0;i<10;i++){
        if(feof(in2)!=0){
            c2 = fgetc(in2);
            fputc(c2,out);
        }

}

fclose(in1);
fclose(in2);
fclose(out);
return 0;
}

There were three problems with your code:

  1. Typo with argw instead of argv
  2. Missing closing } for the while loop
  3. feof(file)!=0 evaluates to true when you are at the eof, the exact opposite of the condition you want, replace with !eof(file) which evaluates to true only if you are not at eof

And one smaller problem, you redefine the variable i in your for loops: for(int i=0;i<10;i++) should be for(i=0;i<10;i++) since you define i earlier in the code.

You should try compiling your code before taking it to SO! The code you posted didn't even compile for me. Here's the working code:

#include<stdio.h>

int main(int argc, char* argv[], char* envp[]){
if (argc != 4){
    printf("%s", "Usage: c4h filename\n");
    return 0;
}

int c1,c2,i;
FILE *in1;
FILE *in2;
FILE *out;

in1 = fopen(argv[1], "r");
in2 = fopen(argv[2], "r");
out = fopen(argv[3], "wb");


if (in1 == NULL){
    printf("%s", "File not found!\n");
    return 0;
}

if (in2 == NULL){
    printf("%s", "File not found!\n");
    return 0;
}

while(!feof(in1) && !feof(in2)){
    for(i=0;i<20;i++)
        if(!feof(in1)){
            c1 = fgetc(in1);
            fputc(c1,out);
        }

    for(i=0;i<10;i++){
        if(!feof(in2)){
            c2 = fgetc(in2);
            fputc(c2,out);
        }

        }
}

fclose(in1);
fclose(in2);
fclose(out);
return 0;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM