Problem in File writing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sanketbarot
    New Member
    • Sep 2006
    • 30

    Problem in File writing

    Hi, Friends

    When I am trying to make a file which of .txt type. it is not generating it perfactly.

    Code:
     #include<iostream.h> 
    #include<fstream.h>
    #include<conio.h>
     
    void main()
    {
     
    clrscr();
     
    char *name;
     
    fstream test;
    test.open("try.txt",ios::binary|ios::out);
    cout<<"Enter Name";
    cin>>name;
    cout<<endl<<name;
     
    test.write((char*)name,sizeof(name));
    test.close();
     
    getch();
     
    }
    this is my program.. The thing is it is creating a .txt file but copy only first two letters. Why?

    and how can can I make it perfact?
    Last edited by Niheel; Oct 6 '06, 06:15 PM.
  • gasfusion
    New Member
    • Sep 2006
    • 59

    #2
    what does the file contain? is there just one name in that file?

    Comment

    • gasfusion
      New Member
      • Sep 2006
      • 59

      #3
      errr. didn't read it right...let me compile it. it has to do with your name variable i think.

      Comment

      • gasfusion
        New Member
        • Sep 2006
        • 59

        #4
        first of all, sizeof(ptr) means:

        the amount of space the pointer itself takes up. Not the data pointed
        to, but the pointer itself. Hence, sizeof is giving the wrong return value, in your case 2 bytes, and writes only 2 characters from your name input to the file.

        Comment

        • sanketbarot
          New Member
          • Sep 2006
          • 30

          #5
          hey ppl thanks a lot i really appreciate it, it helped a lot guys.

          Comment

          Working...