Thread: help setting an array please.

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    22

    help setting an array please.

    ok, i've got a simple program thats supposed to join a bunch of files together into one big dat file.

    #include <fstream.h>
    #include <iostream.h>
    #include "vars.h"


    void main(void)
    {
    ifstream inFiles;
    ofstream datFile;
    char *buffer;
    datFile.open("thps2.dat", ios::binary | ios::ate);
    int test;
    for(int i = 0; i < 67; i ++)
    {

    inFiles.open(fileNames[i], ios::binary | ios::ate);
    inFiles.seekg(0, ios::end);
    test = inFiles.tellg();
    buffer = new char[test];
    inFiles.seekg(0, ios::beg);
    inFiles.read(buffer, sizeof(buffer));
    cout<<sizeof(buffer);
    datFile.write(buffer, sizeof(buffer));
    delete[] buffer;
    inFiles.close();
    }
    }



    the problem is that each time throught the for loop my buffer only gets set to four instead of the result of inFile.tellg(); or for some reason inFile.tellg(); is only returning four.

    can anyone tell me why?
    Last edited by axlton; 07-11-2003 at 10:20 AM.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    Re: help setting an array please.

    Code:
    #include <fstream>
    #include <iostream>
    #include "vars.h"
    using namespace std;
    
    
    int main(void)
    {
    	ifstream inFiles;
    	ofstream datFile;
    	char *buffer;
    	datFile.open("thps2.dat", ios::binary | ios::ate);
    	int test;
    	for(int i = 0; i < 67; i ++)
    	{
                      
    		inFiles.open(fileNames[i], ios::binary | ios::ate);
    		inFiles.seekg(0, ios::end);
    
    		test = inFiles.tellg();
    		buffer = new char[test];
    		inFiles.seekg(0, ios::beg);
    		inFiles.read(buffer, test);
    		cout<<test;
    		datFile.write(buffer, test);
    		delete[] buffer;
    		inFiles.close();
    	}
    }
    sizeof(buffer) is NOT the same as test. Buffer is not an array, it's a character pointer to the beginning of an array; sizeof(buffer) is the same as sizeof(char *) which is often 4 on 32-bit computer systems.

    You also should seek to the file's end *after* you open it, not before.

    The rest was just making your code proper C++.
    Last edited by Cat; 07-11-2003 at 10:22 AM.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    22
    hey, thanks for the help. I'm kickng myself for not realizing what was wrong.

    one thing i'm curious about is the lines of code you changed:

    #include <fstream>
    #include <iostream>
    using namespace std;

    just wandering what the advantage is in that.

    "You also should seek to the file's end *after* you open it, not before"

    yeah, that one was just a mistake i made when i pasted in my code
    Last edited by axlton; 07-11-2003 at 10:37 AM.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    It's legal C++, the other is not.

    <fstream.h>, <iostream.h>, etc. haven't been legal ANSI-C++ since before the 1998 ANSI-C++ standards came out. The new headers <fstream>, <iostream>, etc. have similar functionality but they take advantage of namespaces. They put everything into namespace "std". So, cout is now std::cout, ifstream is now std::ifstream, etc.

    Namespaces mean you avoid any possibility of name collisions. With the addition of the STL to the standard library, that is a very real danger; commonly used class names like vector, list, etc. are C++ standard containers now. So to prevent name collisions, they define all of the C++ standard classes and functions in the std namespace (and place very strict rules on what third-party functions may be included in the std namespace).

    Now, you could just go through your code and use std::cout instead of cout, etc. However, if you don't want to do this, you can bring the entire standard namespace into the current scope with the "using namespace std;" line. You can also bring only desired elements into the current namespace, e.g. "using std::cout;"

    It is technically better form to not use "using", or to bring in only the functions and classes you plan to use, but often people will do the "using namespace std;" because it's easy. In small programs it's no problem. I never use "using" myself in code; I would write the program like so, personally:

    Code:
    #include <fstream>
    #include <iostream>
    #include "vars.h"
    
    
    int main(void)
    {
    	std::ifstream inFiles;
    	std::ofstream datFile;
    	char *buffer;
    	datFile.open("thps2.dat", ios::binary | ios::ate);
    	int test;
    	for(int i = 0; i < 67; i ++)
    	{
                      
    		inFiles.open(fileNames[i], ios::binary | ios::ate);
    		inFiles.seekg(0, ios::end);
    
    		test = inFiles.tellg();
    		buffer = new char[test];
    		inFiles.seekg(0, ios::beg);
    		inFiles.read(buffer, test);
    		std::cout<<test;
    		datFile.write(buffer, test);
    		delete[] buffer;
    		inFiles.close();
    	}
    }
    This way, I avoid any possibility of name collisions or ambiguity by explicitly specifying the full namespace and identifier.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    22
    thanks again. I'd been wandering about that one for some time now because I've seen both exaples in books, but never an explainaion as to why one would be prefered over the other.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. setting structure char array with string
    By Tom Bombadil in forum C Programming
    Replies: 6
    Last Post: 04-03-2009, 07:10 AM
  2. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  3. Setting all elements of an array to zero
    By kolistivra in forum C Programming
    Replies: 9
    Last Post: 08-29-2006, 02:42 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM