dimanche 20 mars 2016

Random numbers, Symbols, Characters, in a stream C/C++

Hi guys I'm new here and a little new in c/c++, I dont usually practise streams but when I tried to make one, opening my file created with this code I always get random numbers, chars, symbols and trash in the .txt file created. Like this: Hellothere.txt. I see random symbols, character instead of numbers entered, the name is OK because it shows well, but sometimes when I write long names it will write trash.

Can you help me to solve this?, Here's the code:


struct sample {

   int code; 

   char name [20]; 

              } sampl;

int main () {

cout<<" ENTER CODE : "<<endl;
cin>>sampl.code;

cout<<" ENTER NAME :"<<endl;
gets(sampl.name);

fflush(stdin);


  FILE *fp;
    fp=fopen("Hellothere.txt", "rb+");

       if(fp==NULL){
            fp=fopen("Hellothere.txt","wb+");

              if(fp==NULL){
                puts("CANNOT OPEN");
                return 0;
                }
                }

fwrite(&sampl,sizeof(sampl),1,fp);
fclose(fp);

getchar();

 }




Aucun commentaire:

Enregistrer un commentaire