Please, try EDU on Codeforces! New educational section with videos, subtitles, texts, and problems. ×

Fk_Viper's blog

By Fk_Viper, 7 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

»
7 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.

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

    thanks.

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

    Can you tell me how the contribution is calculated here?

    • »
      »
      »
      7 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

      • »
        »
        »
        »
        7 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 months 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

  • »
    »
    7 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    sir said that we dont read any file then what is this sir

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

  • »
    »
    7 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    .

  • »
    »
    7 months 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

    • »
      »
      »
      7 months 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");

      • »
        »
        »
        »
        7 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        help me out anyone with helpful resources

      • »
        »
        »
        »
        7 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Why not use c++? Its so short and simple.

        C++ snippet for input/output from/to file
        • »
          »
          »
          »
          »
          7 months ago, # ^ |
          Rev. 5   Vote: I like it 0 Vote: I do not like it

          got it thx

          include <bits/stdc++.h>

          include

          using namespace std;

          int main() {

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

          int x,k,i,a,b; while(cin>>x){

          k = x;
          for(int i=0;i<3;i++){
             cin>>a>>b;
          
            if(k == a)
            k = b;
          else if(k == b)
          k = a;  }
          
           cout<<k<<endl;

          }

          return 0; }

          • »
            »
            »
            »
            »
            »
            7 months ago, # ^ |
              Vote: I like it 0 Vote: I do not like it
            #include<bits/stdc++.h>
            using namespace std;
             
            int main(){
                freopen("input.txt","r",stdin);
                freopen("output.txt","w",stdout);
             
                int x;cin >> x;
                int c[4];
                c[1] = 1; c[2] = 2; c[3] = 3;
                for(int i = 0; i < 3 ; ++i){
                    int a, b; cin >> a >> b;
                    swap(c[a], c[b]);
                }
                cout << c[x];
                return 0;
            }
            
        • »
          »
          »
          »
          »
          4 months ago, # ^ |
          Rev. 2   Vote: I like it 0 Vote: I do not like it

          (Reply to this comment)

          freopen is a C function incorporated into C++, which doesn't make it "C++" like you mention "Why not use C++?". To do so in "C++", you should use std::fstream.

      • »
        »
        »
        »
        4 months ago, # ^ |
          Vote: I like it +8 Vote: I do not like it
        I suggest you
»
7 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

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

I think you can use freopen

Code
»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

def main(): a=''; a = str(input()); a = a.lower(); i = 0; a = a.replace('a',''); a=a.replace('e',''); a=a.replace('y',''); a=a.replace('u',''); a=a.replace('o',''); a=a.replace('i',''); l=len(a); ls=list(); for i in range(l): ls.append('.'); ls.append(a[i]); lks=''.join(ls); return(lks);

what is wrong in this code?It says EOF unexpected when I submitted this code as the answer for 118A.