Блог пользователя Fk_Viper

Автор Fk_Viper, 13 лет назад, По-английски

I am a new entrant here.Please let me know how to read input files? Thanks

  • Проголосовать: нравится
  • -15
  • Проголосовать: не нравится

»
13 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

There is no input files. Input and output are standart, e.g. console.

  • »
    »
    13 лет назад, скрыть # ^ |
     
    Проголосовать: нравится -10 Проголосовать: не нравится

    Can you tell me how the contribution is calculated here?

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +6 Проголосовать: не нравится

    There are some questions in the problemset that require file i/o. One such contest that I found is :https://mirror.codeforces.com/contest/234

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      include <stdio.h>

      include <stdlib.h>

      int main()

      {

      FILE *wrp;

      int n;

      char data[100];

      char num[5];

      wrp = fopen("input.txt","r");

      char readchar;

      int i=0;

      int jl = 0;

      int x=0;

      while (1==1) {

      readchar = fgetc(wrp);
      
      if (readchar == EOF) {
      
          break;
      
      }

      if (readchar != '\n') {

      if (x==0) {
      
      num[jl] = readchar;
      
      jl++;
      
      } else {
      
      data[i] = readchar;
      i++;
      
      }

      } else { x=1; } }

      fclose(wrp);

      n = atoi(num);

      wrp = fopen("output.txt","w");

      int j=0;

      while(j<n/2) {

      if (data[j] == 'L') {

      char num1[3];

      char num2[3];

      itoa(j+1,num1,10);

      itoa(j+(n/2)+1,num2,10);

      fputs(num1,wrp);

      fputs(" ",wrp);

      fputs(num2,wrp);

      fputs("\n",wrp);

      } else {

      if (data[j+(n/2)] == 'L') {

      char num1[3];

      char num2[3];

      itoa(j+1,num1,10);

      itoa(j+(n/2)+1,num2,10);

      fputs(num2,wrp);

      fputs(" ",wrp);

      fputs(num1,wrp);

      fputs("\n",wrp);

      } else {

      char num1[3];

      char num2[3];

      itoa(j+1,num1,10);

      itoa(j+(n/2)+1,num2,10);

      fputs(num1,wrp);

      fputs(" ",wrp);

      fputs(num2,wrp);

      fputs("\n",wrp);

      }

      }

      j++;

      }

      fclose(wrp);

      return 0;

      }

      sir in this contest also we are reading and writing in file i am beginner not able to understand how to write opening files in ICPC contest please help me out

      wrp = fopen("output.txt","r");

      wrp = fopen("output.txt","w");

»
13 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

As I can see you managed to read from stdin.

Sometimes in gym you need read from file.
The easiest way to do it in C++ is to use freopen

freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);

And then read, as from stdin

»
13 лет назад, скрыть # |
 
Проголосовать: нравится +9 Проголосовать: не нравится

In some contests, you have to read the input from "input.txt" and write your output in "output.txt", e.g. Codeforces Round 155 (Div. 2). In these situations you can use #include <fstream>as easy as cin/cout (#include <iostream>).

Just write #include <fstream> at the top of your code and define some iofstream variables such as fin and fout:

ifstream fin("input.txt");
ofstream fout("output.txt");

Now you can use them like cin/cout:

fin >> n;
fout << n;
»
6 лет назад, скрыть # |
 
Проголосовать: нравится -10 Проголосовать: не нравится

I think you can use freopen

Code