Jump to content

Problema Deschidere Si Scriere In Fisier (C++)


Ovidiu29

Recommended Posts

Salutare tuturor. Nu-mi afiseaza in fisier nimica. Nu stiu unde am gresit.

#include<stdio.h>
#include<stdlib.h>
using namespace std;
long int n, s;
int main()
{
    FILE * f; FILE * g; f=fopen("numere.in","r"); g=fopen("numere.out","w");
    while(feof(f))
    {
        s+=n;
    }
    fclose(f);
    fclose(g);

    return 0;
}

Respecta si vei fi respectat.

Link to comment
Share on other sites

while(feof(f)) asta nu cred ca e bun. ia uitate inca o data ce face functia aia, nar trebui sa fie o negare pe acolo? ia gandestete ce face feof ala mai exact.

 

insa sunt sigur ca nu ai afisat niciunde. ai deschis niste fisiere, dar nu citesti si nu scrii in niciunu. Cum scrii sau citesti in/din fisier?

love is a verb
Love is a doing word

Link to comment
Share on other sites

Tot nu merge.

#include<stdio.h>
#include<stdlib.h>
using namespace std;
long int n, s=0;
int main()
{
    FILE * f; FILE * g; f=fopen("numere.in","r"); g=fopen("numere.out","w");
    while(fscanf(f,"%d",&n))
    {
        s+=n;
    }
    fprintf(g,"%d",s);
    fclose(f);
    fclose(g);

    return 0;
}

Respecta si vei fi respectat.

Link to comment
Share on other sites

  • 1 month later...

#include<stdio.h>
#include<stdlib.h>
using namespace std;
 int n, s=0;
int main()
{
    FILE * f; FILE * g;
    f=fopen("numere.in","r");
    g=fopen("numere.out","w");

    while(fscanf(f,"%d",&n) == 1){  //daca citirea a reusit
        s+=n;
    }
    fprintf(g,"%d",s);
    fclose(f);
    fclose(g);

    return 0;
}

 

Userful:

http://www.cplusplus.com/reference/cstdio/fscanf/

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.