In general, in a couple of hours I will have a school C Olympiad, to which I was enrolled by force. In general, I know what's what, but there is a prerequisite: take the data from the file and write the result to the file too. Google - huge mana for work (there is no time to study). I ask you to write the most basic functions for work (opening, reading, writing, etc.) with examples. I will be grateful.

    3 answers 3

    To work with files you will need

    #include <stdio.h> 

    The open file is described by the FILE structure. When you open the file, you must specify the mode. If an error occurs, the function returns NULL.

     FILE *pFile = fopen("Izengardjke.txt", "w"); /* r - чтение, файл должен быть * w - запись, старое содержимое удаляется, файл создаётся * a - дополнение в конец файла, файл создаётся * a+ - чтение и дополнение, содержимое сохраняется. * читать можно всё (перемещаться по файлу с помощью * fseek(FILE*, int offset, int origin) или * rewind(FILE*)), но при попытке записи * указатель будет перемещаться в конец файла */ 

    The entry looks like this:

     char buffer[] = "Hello, World!"; fwrite(buffer, sizeof(char), sizeof(buffer), pFile); 

    Reading is as follows:

     fread(&buffer, sizeof(char), sizeof(buffer), pFile); 

    And do not forget to close the file.

     fclose(pFile); 
    • 3
      I think in such cases it is better to use fscanf / fprintf - andrybak
    • For the text - yes, I already took advantage of your comment :) - Surendil
    • Wow. where I did not look for a simple manual on working with files in C-plainly understandable and did not find it .. ATP. Threat though I was probably looking bad) - sudo97

    To work with files, you need the fstream library. The fstream class is a derived io class, just like iostream. Therefore, you can use all the same functions as in I / O.

     ifstream f("filename");//создать input-file object тоесть файл ИЗ которого читать ofstream f1("filename");//output-file object, файл В который записывать char ch; f.get(ch);//считать один символ из filename f1 << ch; //записать символ в filename 
    • So after all the Olympiad in C, but not in C ++! - skegg
    • tag with ++ worth - Specter
    • And in the text of the question - the Olympiad in S. - skegg
    • in my opinion the label has a higher priority - Specter
    • The Olympics have long been over probably. And the fact that the options for C and C ++ are wonderful :) - Surendil

    Here and here - quite good articles on a subject.

    PS I have never heard of being required to work with input / output files at Olympiads. Usually the choice is either this way or with I / O streams.

    • one
      Unfortunately, they demand ... - Izengardjke
    • There really require. The condition is approximately as follows: Input file: X.in, Output file: X.out size limit: 1024KB. - 3JIoi_Hy6
    • one
      @ALEX_LAY_PRO but what about the Olympiad, can you say? - VioLet
    • one
      Informatics Olympiads have been in a similar vein for a long time)) The last time was 5 years ago, tasks are similar, although they are something from informatics and it remains that knowledge of working with a file, the rest is to make the correct formula, but in fact it is correct)) Vladimir Klykov