0

I need to make a program that takes string data from one file and copy every third char from it to another file.

I am not sure if I am doing it right. The idea I got is to first create one array where I will store original data from file1 and then using 'for' loop I will modify the data and store in in the second array: (eg for(i=0; i < arraysize; i+=3); The thing is I dont have an idea how to transfer input to my array and how to do it backwards to have my modified data go to file2.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BUFFER_SIZE 50

int main(int argc, char *argv[]) {
    char buffer[BUFFER_SIZE];
    char modified[BUFFER_SIZE];
    int input_fd, output_fd;
    ssize_t ret_in, ret_out;
    if(argc !=3 || strcmp(argv[1], "--help") == 0)
    {
        printf("Usage: %s file_origin file_destination\n", argv[0]);
        return 2;
    }
    input_fd = open(argv[1], O_RDONLY);
    if(input_fd == -1)
    {
        perror("There is no such file");
        return 2;
    }
    out_fd = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0644);
    if(output_fd == -1)
    {
        perror("create");
        return 3;
    }

Could someone please tell me how to use function read/write correctly to stream my data to array and how to do it the other way.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • in your comment on Marc M's answer, you say that you need to do this in a non-buffered way; this is a little confusing because your own code makes use of a buffer; but, for a non-buffered approach, see the first suggestion of my answer : "simply read from the input file one byte at a time, and write every third byte to the output file"; but to do this, you probably want to consider various i/o function calls; I listed a good set of ones to start with in my answer; read up on those and you will learn a lot on how you can accomplish this – landru27 Jun 20 '19 at 21:43

3 Answers3

1

Welcome to Stackoverflow!

Given the exact description of your assignment, I would not use a buffer; you could simply read from the input file one byte at a time, and write every third byte to the output file. This avoids any buffer-management overhead.

But, if you do read from the input file into a buffer, you do not need to modify that buffer in any way, nor do you need a second buffer. After reading all the data, simply iterate through the input buffer, outputting every third byte to the output file.

But, if you want/need to reuse the output in some way, you can simply populate a second buffer from the input buffer in the same manner (loop over the input buffer, skipping two bytes each iteration), and then write that second buffer to the output file. (This way, you still have the same output in that second buffer, and you can reuse it in some manner.)

The approach you take will dictate the best functions to use. I see you already know about open(). Read up on read(), write() and close(), but also read up on fopen(), fgetc(), fgets(), fread(), fwrite() and fclose(). There is a lot for you to learn from reading about these various functions, how they are similar to each other, how they differ from each other, and the pros and cons of each. Reading about them will lead you to learn about other related file operations (like seeking, rewinding, etc.), which will serve you well as you learn more about C and programming in general.

Please note that for the approaches using buffers, you need to be very careful about the size of your buffers vs. the size of the input file. There are many pitfalls here. If this is an assignment for a class of some sort, then those considerations might show up in later lessons / assignments, and maybe it's too much to take on just now. But it's never too early to start thinking about what you do and don't know about the input your program will need to handle.

landru27
  • 1,654
  • 12
  • 20
0

If you do not need cin or cout, I would suggest the following (I assumed strings are ended with newline and those should be preserved in the output and that counting the 3rd character starts anew in every line read):

FILE *f1=fopen("_infile.txt","rt");
FILE *f2=fopen("_outfuile.txt","wt");

char buffer[MAXBUFLEN];

while (!feof(f1)) {
    if (fgets(buffer,MAXBUFLEN,f1)>0) {
        for(int i=2;i<strlen(buffer);i+=3) {
            fprintf(f2,"%c",buffer[i]);
        }
        fprintf(f2,"\n");
    } else break;
}

fclose(f1);
fclose(f2);
Marc M
  • 1
  • 1
  • What is the relevance of `cin` or `cout` to a C question that mentions neither? – Jonathan Leffler Jun 20 '19 at 15:17
  • 1
    Note that [`while (!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) — though your code is a bit more subtly wrong than most. You do check the `fgets()` call, which is the step usually overlooked, but you really don't need the `if` with `break`; you should simply use `while (fgets(buffer, sizeof(buffer), f1) != 0) { … }`. Using `feof()` is almost always unnecessary and irrelevant; it is here, too. – Jonathan Leffler Jun 20 '19 at 15:20
  • @Jonathan Leffler: cin and cout is what came first to my mind when reading "stream my data". Your code is simpler, and I agree that using feof is not necessary, but it's also not wrong. – Marc M Jun 20 '19 at 15:57
  • 2
    Welcome to SO. When answering C questions, don't mention random C++ concepts — that's careless and potentially confusing. I said that your use of `feof()` is less wrong than most — it will in fact work correctly, as you claim. But it is just wasteful coding, using 2 extra lines because of the way you formatted the code — it would be 4 or 5 lines in many styles. And the `!feof()` test will never fail because your loop exits via the `break` before the test is run at the time when it would fail. It won't fail before attempting to read the first line of an empty file, for example. – Jonathan Leffler Jun 20 '19 at 16:03
  • Hi, the thing is I have to do it non-buffered way –  Jun 20 '19 at 16:41
0

This will read input file and reprint every third character to output. You can adapt it to you situation.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    size_t i; // index
    int c; // char read

    FILE *FIN, *FOUT; // file streams
    if ((FIN = fopen("in.txt", "rb")) == NULL) {
        printf("Error opening input file.\n Exiting.\n");
        exit(1);
        }
    if ((FOUT = fopen("out.txt", "wb")) == NULL) {
        printf("Error opening output file.\n Exiting.\n");
        exit(1);
        }
    // read input and reprint every third character
    for(i=0;;i++)
        {
        c = fgetc(FIN); // read byte
        if(c == EOF)
            {
            break; // reached end of file (input), leave loop
            }
        if((i%3)==2) // get every third character by modulo(i)
            {
            fputc(c, FOUT); // write output
            }
        }

    fclose(FIN);
    fclose(FOUT);

return 0;
}
tansy
  • 486
  • 3
  • 8