Advanced Search

Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: c++ I/O problems

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

    Default c++ I/O problems

    Hey, everyone! I need to read in a file, modify it, and then output it to another file. Then that file needs to be read in, modified, and then output to yet another file. The problem is that I cannot use the getline() function, because there may or may not be new lines in the 100 MB file. Here is what I have, but it does not work:

    Code:
    char * text;
    int len, pos, ending;
    
    ifstream file_in ("test.txt");
    ofstream file_out ("enc.txt");
    
    	pos = 0;
    	file_in.seekg(0, ios::end);
    	len = file_in.tellg();
    	ending = len;
    	file_in.seekg(pos, ios::beg);
    
    while(len > 0){
    	if(len > 350){len = 350;}
    	text = new char[len];
    	file_in.read(text, len);
    	//modify text
    	file_out << text;
    	pos = file_in.tellg();
    	file_in.seekg(pos+len, ios::beg);
    	len = ending - pos;
    }
    file_in.close();
    file_out.close();
    
    file_in.open("enc.txt", ios_base::in);
    file_out.open("unenc.txt", ios_base::out);
    
    	pos = 0;
    	file_in.seekg(0, ios::end);
    	len = file_in.tellg();
    	ending = len;
    	file_in.seekg(pos, ios::beg);
    
    while(len > 0){
    	if(len > 350){len = 350;}
    	text = new char[len];
    	file_in.read(text, len);
    	//modify text
    	file_out << text;
    	pos = file_in.tellg();
    	file_in.seekg(pos+len, ios::beg);
    	len = ending - pos;
    }
    file_in.close();
    file_out.close();
    The first I/O section adds some characters to the end of the file, and the second ends up being an infinate loop because file_in.tellg() always equals -1. I am really lost, and I have been trying to do this for a few weeks, so any help would be great. Thanks.
    Last edited by Jas; 06-30-2008 at 02:08 AM.
    --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

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

    Default stdlib and iostream...

    First of all, why don't you include a library of some sort that your compiler provides? Such as iostream? It would be much easier to understand, for me, and maybe that will actually fix your problem. Try learning C. It is easier that C++ and you get to define everything on your own. By the way, use cout and cin for iostream. For cstdlib or stdlib, use printf("%s",char something="") and scanf("%s",char something=""). This may help you clarify the I/O part so that it is not self defined. As for the other part of your code, I do not understand what you are saying. Perhaps you could clarify your meaning.

    -magicyte

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

    Default

    Quote Originally Posted by magicyte View Post
    First of all, why don't you include a library of some sort that your compiler provides? Such as iostream? It would be much easier to understand, for me, and maybe that will actually fix your problem.
    I included iostream and fstream. The reason the rest of the code it not posted is for security purposes (I would like to share as little code as posible that does not directly pertain to the problem, but I should have included that information, so I apologise).

    Try learning C. It is easier that C++ and you get to define everything on your own.
    Thanks, but I think I'll stick with C++, since the program is done except for this problem.

    By the way, use cout and cin for iostream. For cstdlib or stdlib, use printf("%s",char something="") and scanf("%s",char something=""). This may help you clarify the I/O part so that it is not self defined.
    Sorry for my ignorance, but what? Since I am not really familiar with C or C++, that makes no sense.

    As for the other part of your code, I do not understand what you are saying. Perhaps you could clarify your meaning.
    What part are we talking about? I would be happy to clear anything up for you.

    If it is easier than sorting through my horrible chunk of code, maybe you can give me an example of what I am trying to do? (beleive it or not, the rest of the looks good and readable )

    Thanks for the reply, as well. I was wondering if anyone on DD knew anything about C++, lol.
    --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

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

    Default

    I have a question. Where are the included libraries that will allow the I/O operations? If you have the COMPLETE CODE, please, let me see it. In order to actually help you with flying colors, I will need the whole code. Other than that, I do not know how the I/O operations work because there are no included libraries. Once again, in order to help you, I must see the complete code. Also, the getline() function may not work because it is included in a non-included file. Hopefully you reply.

    -magicyte

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

    Default

    Thanks for the reply.

    Quote Originally Posted by magicyte View Post
    I have a question. Where are the included libraries that will allow the I/O operations?
    Isn't there only one place they can be? But here is as much of the code as I can show.

    Code:
    #include <string>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    /*
     * There is a class here I will not include
     */
    
    int main(int arg_num, char* args[]){
    
    char * text;
    int len, pos, ending;
    
    ifstream file_in ("test.txt");
    ofstream file_out ("enc.txt");
    
    	pos = 0;
    	file_in.seekg(0, ios::end);
    	len = file_in.tellg();
    	ending = len;
    	file_in.seekg(pos, ios::beg);
    
    while(len > 0){
    	if(len > 350){len = 350;}
    	text = new char[len];
    	file_in.read(text, len);
    	file_out << text;
    	pos = file_in.tellg();
    	file_in.seekg(pos+len, ios::beg);
    	len = ending - pos;
    }
    file_in.close();
    file_out.close();
    
    file_in.open("enc.txt", ios_base::in);
    file_out.open("unenc.txt", ios_base::out);
    
    	pos = 0;
    	file_in.seekg(0, ios::end);
    	len = file_in.tellg();
    	ending = len;
    	file_in.seekg(pos, ios::beg);
    
    while(len > 0){
    	if(len > 350){len = 350;}
    	text = new char[len];
    	file_in.read(text, len);
    	file_out << text;
    	pos = file_in.tellg();
    	file_in.seekg(pos+len, ios::beg);
    	len = ending - pos;
    }
    file_in.close();
    file_out.close();
    
    
    system("pause");
    
    	return 0;
    }
    I am not sure if that helps. Let me know.

    Also, the getline() function may not work because it is included in a non-included file. Hopefully you reply.
    I am not using that function. See the first post again:

    The problem is that I cannot use the getline() function, because there may or may not be new lines in the 100 MB file.
    The file is not likely to have new lines, because of the way it is generated (I won't go into that-- just trust me that there are few new lines, if any at all).

    I am wondering, if it would be better just to ditch this code and start over. What do you suggest I do to accomplish what I need? (I need to read in 350 chars at a time to a string from a file, modify the string, insert it into another file, and to the same thing over again with the second file.)

    Again, thanks for sticking with this.
    --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

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

    Default

    It is actually all too confusing. The only way I can fix the script is if I have every single bit of code there is, just to understand it all (the code, that is). Alternatively, start over. I understand what the code is supposed to do, just not HOW it does it. If you could try to compile the code again and tell me the exact errors the compiler gives you, I may be able to understand what is wrong. All in all, starting over would be easiest. Three last things: One-Did you make the code? Two-Do you not want me to see the whole code because you are afraid of me stealing it (I WILL NOT STEAL THE CODE. HONEST.) Three-If it is VERY IMPORTANT, try to get it copyrighted.

    -magicyte

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

    Default

    And one last thing: Are you a black belt??

    -magicyte

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

    Default

    Quote Originally Posted by magicyte View Post
    It is actually all too confusing. The only way I can fix the script is if I have every single bit of code there is, just to understand it all (the code, that is). Alternatively, start over. I understand what the code is supposed to do, just not HOW it does it. If you could try to compile the code again and tell me the exact errors the compiler gives you, I may be able to understand what is wrong. All in all, starting over would be easiest.
    Well, the rest of the code is only a class to modify the text input. There are also no compiler errors-- the problem is that the first block, namely this:
    Code:
    ifstream file_in ("test.txt");
    ofstream file_out ("enc.txt");
    
    	pos = 0;
    	file_in.seekg(0, ios::end);
    	len = file_in.tellg();
    	ending = len;
    	file_in.seekg(pos, ios::beg);
    
    while(len > 0){
    	if(len > 350){len = 350;}
    	text = new char[len];
    	file_in.read(text, len);
    	file_out << text;
    	pos = file_in.tellg();
    	file_in.seekg(pos+len, ios::beg);
    	len = ending - pos;
    }
    file_in.close();
    file_out.close();
    Somehow reads in some additional giberish after the file ends, and the second block, namely this:
    Code:
    file_in.open("enc.txt", ios_base::in);
    file_out.open("unenc.txt", ios_base::out);
    
    	pos = 0;
    	file_in.seekg(0, ios::end);
    	len = file_in.tellg();
    	ending = len;
    	file_in.seekg(pos, ios::beg);
    
    while(len > 0){
    	if(len > 350){len = 350;}
    	text = new char[len];
    	file_in.read(text, len);
    	file_out << text;
    	pos = file_in.tellg();
    	file_in.seekg(pos+len, ios::beg);
    	len = ending - pos;
    }
    file_in.close();
    file_out.close();
    is an infinate loop for some reason. I agree that starting over is probably the best solution, but I have done that at least five times and I have no idea where to start. Do you have any suggestion?

    Three last things: One-Did you make the code? Two-Do you not want me to see the whole code because you are afraid of me stealing it (I WILL NOT STEAL THE CODE. HONEST.) Three-If it is VERY IMPORTANT, try to get it copyrighted.
    1) Yes, otherwise it would probably work 2) No, it's nothing like that. However, the software is for security purposes so I don't want to show it. And at any rate the class shouldn't be needed-- it's basically irrelevant to the problem. (When text is read in from a file, that class modifies it before sending it to the output file.) Without the file I/O, the class works perfectly. 3) It's not that important.

    And one last thing: Are you a black belt??
    Yes 3rd Dan in Tae Kwon Do.

    Edit: In the second block, the reason for the infanite loop is that this line:
    Code:
    pos = file_in.tellg();
    Always sets pos to-1 (thus pos - the file length is 1, which the loop thinks is another character to read in so the loop starts again), but I don't know why that is.
    Last edited by Jas; 07-03-2008 at 08:09 PM.
    --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

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

    Default

    No, sir. I do not have any suggestion on where to start next. Im just a kid.

    -magicyte

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

    Default

    You know more about C++ than I do.

    Let me put it like this: how would you go about it, if you were to code it? For example, what function(s) would you use?

    Any information you can give me would be fantastic-- I have been working on this for weeks with no success. Trust me, you can't hurt anything worse than it already is.
    --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

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
  •