Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: c++ I/O problems

  1. #11
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default Functions, eh?

    Well, if I were to code this all, I would make a .dev file, first of all to compile the code together so that everything has easy access to eachother. Some functions I would use from other C++ libraries might be printf()/scanf() or cout/cin (of course). As for the other files retrieving whatever you said they would, I am stuck. I don't really know much about transporting information through files. If you are running the codes with MS-DOS, I do. Simply in the parameter section of the main() function include int argc for integers that will be collected from MS-DOS and char argv[]* (i think that is correct syntax, but not sure) to collect characters that will be used through other files. As for direct correspondence between C++ and C files, I do not know. As for direct correspondence between HTML, ASP, PHP, and XML files I do know. Unluckily, I cannot help you with the whole project. I may be able to give you small hints to other functions that you would know about, but I am a pure novice in C++. I am a GENIUS in C, as you may have seen in my signature below and/or previously. Right now I am taking classes from a professional C programmer and learning C++ from a mere book. Books can help a lot, and one book, C++ Unleashed by Jesse Liberty (can be bought over Amazon.com) would really help. Although you would rather stick to C++, it is easier to understand and define things in C. Just a suggestion. I'm more of an HTML, XML, AJAX, JavaScript kind of guy, cause everything in those languages are predefined. No compiling to go through, either. When you have a pre-defined language like C++ and you are compiling it, and all of the sudden an error comes up, it takes FOREVER to find the source of that error, even if it tells you what and where the error is (that is the compiler). And finally I am ending this long post. Even though I cannot help you, I will care to take a look at your script one last time to see how it is. Now for the FUN stuff. Once again, I am just a child who likes to program and types quickly, unlike OTHER children. Good luck with your code by the way. I will get back to you on your code as soon as I can. Hopefully I will find a solution.

    -magicyte

  2. #12
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    Jas, try this solution and see if it fits your needs:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    /* Prototype for the callback function.
     *
     * 1st param - a data chunk
     * 2nd param - size of the data chunk
     * 
     * returns - new transformed data, the WalkFile function automatically deletes
     * it with the delete[] operator, so make sure you allocate it with the new[]
     * operator also the second parameter must be set to the size of this data
     */
    typedef char* (*walk_func_t)(const char*, int&);
    
    /* Reads chunks of a given size from the input file and lets the given callback
     * function transform the data, then writes the result to the output file.
     *
     * aInFilename - input file name
     * aOutFilename - output file name
     * aChunkSize - size of a single data chunk to read from the input file
     * aWalkFunc - callback function which will be called with a data chunk passed to it
     */
    void WalkFile(const char* aInFilename, const char* aOutFilename, int aChunkSize, walk_func_t aWalkFunc)
    {
    	ifstream fin(aInFilename);
    	ofstream fout(aOutFilename);
    	
    	char* bufferIn = new char[aChunkSize];
    	
    	while (fin.good())
    	{
    		fin.read(bufferIn, aChunkSize);
    		
    		int bufSize = fin.gcount();
    		char* bufferOut = (*aWalkFunc)(bufferIn, bufSize);
    		
    		if (bufferOut != NULL && bufSize > 0)
    			fout.write(bufferOut, bufSize);
    		
    		delete[] bufferOut;
    	}
    	
    	delete[] bufferIn;
    	fout.close();
    	fin.close();
    }
    
    // Sample transform function (turns data uppercase)
    char* Encode(const char* aData, int& aSize)
    {
    	// If the size of the output buffer is different than the size of the input
    	// buffer, the aSize variable has to be modified!
    	char* dataOut = new char[aSize];
    	
    	for (int i = 0; i < aSize; ++i)
    		dataOut[i] = toupper(aData[i]);
    	
    	return dataOut;
    }
    
    // Sample transform function (turns data lowercase)
    char* Unencode(const char* aData, int& aSize)
    {
    	// If the size of the output buffer is different than the size of the input
    	// buffer, the aSize variable has to be modified!
    	char* dataOut = new char[aSize];
    	
    	for (int i = 0; i < aSize; ++i)
    		dataOut[i] = tolower(aData[i]);
    	
    	return dataOut;
    }
    
    int main(int argc, char* args[])
    {
    	WalkFile("test.txt", "enc.txt", 350, &Encode);
    	WalkFile("enc.txt", "unenc.txt", 350, &Unencode);
    
    	cout << "Press ENTER to quit . . .";
    	cin.ignore();
    	return 0;
    }
    Basically, you have to call the WalkFile function and pass the names of the in/output files, size of the data to read at a time and the callback function. The latter will receive a chunk of data and will have to return a new one with the modified contents. See comments for more info.

    The problem you may encounter is that you may want to call a non-static member function of your class, if that's the case then the code has to be modified, ask if you need further details.

  3. The Following User Says Thank You to DimX For This Useful Post:

    Jas (07-13-2008)

  4. #13
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Thank you, DimX. I already managed to solve the problem, but thanks for the code.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  5. #14
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Yeah. Um, I wuz gunner post that script too...

    Sorry I couldn't get to you earlier (actually, I feel more sorry for myself cuz I didern't get thankt). It was after this last post you gave that I studied my C book a little bit. I then found out about File I/O!! Heh, now I know how to I/O between files!

    -magicyte

  6. #15
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Heh. That's life, isn't it? You always get answers when their no longer needed. (I'm talking about myself here)

    Well, thanks for trying to help Nice avatar, BTW.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  7. #16
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Thanks and you too!

    -magicyte

Bookmarks

Posting Permissions

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