Fk_Viper's blog

By Fk_Viper, 11 years ago, In English

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

  • Vote: I like it
  • -15
  • Vote: I do not like it

| Write comment?
»
11 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    11 years ago, # ^ |
      Vote: I like it -10 Vote: I do not like it

    Can you tell me how the contribution is calculated here?

    • »
      »
      »
      11 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      get more voteups in your comments then your contribution will get higher

      get more votedowns in your comments then your contribution will get lower

      for new users the contribution which have a few comments will change with a large extent when the users become elder thier contribution will change with a low extent

      • »
        »
        »
        »
        11 years ago, # ^ |
          Vote: I like it +7 Vote: I do not like it

        So,People prefer to vote down here as i see....nice...

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Well, if you post something that they dont interested, they usually downvote. Dont worry about that, just keep in mind what you need to know from the other people who are answer your post

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

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

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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");

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it +8 Vote: I do not like it
        I suggest you
»
11 years ago, # |
  Vote: I like it +3 Vote: I do not like it

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

»
11 years ago, # |
  Vote: I like it +9 Vote: I do not like it

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;
»
4 years ago, # |
  Vote: I like it -10 Vote: I do not like it

I think you can use freopen

Code