Characters are entered from the keyboard, you need to get a file with these characters written in the reverse order (for example, when entering 12134 to the file, you need to write 43121 )

Arrays cannot be used (the number of characters can be very large) and the fseek() function

  • write each character to a new file, then assemble them in reverse order :) - Ivan Solntsev
  • one
    It is not good to post your homework here. For starters, it would be worthwhile to try to do something yourself. And then, in case of specific difficulties, ask a question. - Indian
  • five
    I vote for closing this question as irrelevant to the topic, because this is homework without trying to solve it on my own - PashaPash
  • one
    In matters relating to educational tasks, properly describe their attempts and the fact that they failed. Laying out only the wording of the task is not welcomed by the community. - Timofei Bondarev
  • one
    @Shamov main purpose of SO - the organization of knowledge. not the entertainment of bored crowds and the trolling of schoolchildren. fseek() cannot be used - into the firebox, as a question with artificial restrictions, the answer to which will not give anything to anyone. - PashaPash

3 answers 3

The main idea here is to use recursion instead of a loop to read and write characters. Indeed, in order to derive N numbers in the reverse order of reading, you must first output N-1 the last number in reverse order, and then the first one. Continuing the analogy, we reach N = 1, in this case we simply output a number.

In pseudocode, the idea looks like this:

 функция вывести_N_чисел_в_обратном_порядке(N) x = прочитать_число() если N > 1 вывести_N_чисел_в_обратном_порядке(N - 1) вывести_число(x) 

The main trick here is storing the read numbers on the call stack, which allows, first, to do without explicitly declaring the array, and second, to pull the numbers in the reverse order of the record.

  • Quote from the problem: "the number of characters can be very large." - Athari
  • @Athari doesn’t say how the program should know that it’s time to stop. This example is for the case when the program must stop through N characters. N may well be very large - Sandman
  • @Athari If the condition is that the program should stop after some specials. character, it is enough to change the condition in the ЕСЛИ - Sandman
  • And if not - you can read the file 2 times: first - to determine its length, the second - actually, for the output of characters. Or (in order of delirium) read the file, determining its length, then read it N times before the i-th character and output. Slowly, but without additional memory at all, even in the form of a stack. - fori1ton
  • one
    I'm talking about the fact that the stack is usually not rubber, it will not stand "a great number of characters". - Athari

The most interesting in this task is the question of how to insert a character at the beginning of a file, without using additional memory and temporary files, and also not moving around the file in an arbitrary manner.

There is one trick that allows you to do this. The corresponding function would look like this.

 void prepend(char ch, const char* filename) { FILE* fp = fopen(filename, "r+"); if (!fp) fp = fopen(filename, "w+"); int buf; while ((buf = fgetc(fp)) != EOF) { ungetc(buf, fp); fputc(ch, fp); ch = (char)buf; } fputc(ch, fp); fclose(fp); } 

    Perhaps a late reply, but try this:

     #include <stdio.h> #include <conio.h> void main() { char str[20]; //создаем массив строки int i; //переменная счетчик puts("Vvedite stroku\n"); //выводим на экран просьбу создания строки gets(str); //сохраняем в массив строку for (i=0; str[i]!='\0'; i++); //цикл выводит строку в прямом порядке printf("\n%s sodergit %d simvolov \n", str, i); for (str[i]='\0'; i>=0; i--) //цикл выводит строку в обратном порядке printf("%c\n", str[i]); }