-1

I wrote this code. in this program i read a file then pour part of file into the char* temp. Finally char *temp written in the file.I have a problem. When i write temp in file only 4 character written in it.What do i do?

fstream file;
 file.open("mary.txt",ios::in);
 file.seekg(-1,ios::end);
 int pos=file.tellg();
 char ch;
 char c;
 int i=0;
 char * temp=new char[100];
 file.seekg(0,ios::beg);
 while(pos >=0)
 {

     file.read(&ch,sizeof(char));
     if(ch=='a'||ch=='o'||ch=='u'||ch=='e'||ch=='i'||ch=='A'||ch=='O'||ch=='U'||ch=='E'||ch=='I')
     {
         pos--;
         continue;
     }
     else if(ch>='a' && ch<='z')
     {
         c=ch-32;
         temp[i]=c;
         i++;
     }
     else
     {
         temp[i]=ch;
         i++;
     }
     pos--;
 }
 temp[i]=NULL;
 cout<<temp<<endl;
 cout<<" sizeof temp:"<<sizeof(temp)<<endl;//out put is 4 while temp has longer size!! why?
 fstream f("test.txt",ios::trunc);
 f.write(temp,sizeof(temp));//if the file contains "abcdeifjle" only written "abcd"
raha
  • 1
  • 1
    Use `strlen(temp)`, not `sizeof(temp)` – 001 Jun 06 '14 at 11:05
  • `sizeof(temp)` gives you the size of the pointer variable, which actually seems to be 4 bytes for your current environment (32-bit). – πάντα ῥεῖ Jun 06 '14 at 11:06
  • Use strlen instead of sizeof and also don't forget to call flush and close methods at the end. – Sudhakar B Jun 06 '14 at 11:09
  • 1
    @SudhakarB: There's no need to explicitly flush and close the `fstream`; the destructor will take care of that. You do need to `delete [] temp` though (or, better still, use a C++ string instead). – Mike Seymour Jun 06 '14 at 11:11
  • @MikeSeymour, yes but its a good practice (look into this http://stackoverflow.com/questions/5036878/why-does-ofstream-require-a-flush) – Sudhakar B Jun 06 '14 at 11:25
  • @SudhakarB: That question describes a bug in some dodgy library implementation. Unless you're actually using a buggy implementation, adding weird workarounds only serves to clutter the code, which isn't a good practice in my book. – Mike Seymour Jun 06 '14 at 11:32

2 Answers2

3

temp is a pointer; sizeof(temp) is the size of a pointer, which is 4 bytes on your 32-bit platform.

To get the length of the C-style string it points to, use std::strlen(temp)

Better still, use std::string rather than all this weird pointer-juggling. That will fix the memory leak and buffer-overrun vulnerability in your code; and probably some less obvious bugs too. There's rarely a good reason to write C++ as if it were C.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

you can use strlen(temp)

sizeof(temp)

will give you size of pointer

Pointers generally have a fixed size, for ex. on a 32-bit executable they're usually 32-bit. There are some exceptions, like on old 16-bit windows when you had to distinguish between 32-bit pointers and 16-bit... It's usually pretty safe to assume they're going to be uniform within a given executable on modern desktop OS's.

Muhammad Zaighum
  • 560
  • 4
  • 21