CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2005
    Posts
    5

    pointer to string/ file problem

    Well, I have this problem: I want to read a file, get the size, and read all symbols. Then I do some stuff with the data and I`m trying to write it in file. But the program is adding another 65 symbols at the end of the file. The file is 6151 symbols long. Here is the code:

    long file_size;
    //here it reads the file_size - I`m sure it`s OK

    unsigned char* input=new unsigned char[file_size]; //def. and init. the pointer
    for(i=0; i<size; i++){
    input[i]=in.get();
    }
    //and then do something to the data........
    //then write it in file
    for(int i=0;i<size;i++) out_file<<input[i];

    So...any suggestions. Thanks!

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: pointer to string/ file problem

    Don't read/write character by character, because that's extremely slow. Read/write blocks, like:
    Code:
    long file_size;
    //here it reads the file_size - I`m sure it`s OK
    
    unsigned char* input=new unsigned char[file_size]; //def. and init. the pointer
    in.read(input, file_size);
    
    //and then do something to the data........
    //then write it in file
    out_file.write(input, file_size);
    NOTE: How do you open the file? How do you retrieve the length of the file?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Sep 2005
    Posts
    5

    Re: pointer to string/ file problem

    It`s OK. I`ve decided to use vector and it`s work.Thanks anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured