CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 52

Thread: File I/O help

  1. #16
    Join Date
    Apr 1999
    Posts
    27,449

    Re: File I/O help

    Quote Originally Posted by JasonBB
    Vague, but thank you.

    . . . Isn't the point of a forum to help you learn?
    A line has to be drawn somewhere. A forum is not the place to learn basic C++ language concepts. That is the domain for books, and if available on-line sites dedicated to teaching the language.
    Unless you though that I forgot headers and the main method, I don't see why you needed my version of the code.
    Again, and I won't repeat it -- we need to see what you wrote, not what someone else wrote. We have no idea if you may have changed something, missed a line, added a line, etc. Too many times, we're guessing as to what someone wrote, and then 20 posts later, lo and behold it isn't what they claim they wrote. To circumvent all of these problems, take the time to post the code you wrote so that this doesn't occur.
    Slightly insulting, but okay.
    And what is insulting about it? It is one of the best FAQ's out there for C++ on the internet.

    Note that this is the C++ FAQ that is maintained by comp.lang.c++. It is *the* newsgroup forum for asking C++ questions. They have a protocol, and a darn good one, when someone asks a question concerning code that doesn't work. It is also the FAQ that others here point others to when asking more specific C++ questions.

    This is one fix you must do:
    Code:
    mystring = &buffer[0];
    A std::string requires a char* for it to be initialized, and a pointer to the first element of a vector<T> is a T*.

    Regards,

    Paul McKenzie

  2. #17
    Join Date
    Jul 2008
    Posts
    27

    Re: File I/O help

    The FAQ itself is not insulting, this link is:
    http://www.parashift.com/c++-faq-li...st.html#faq-5.8
    Anyway. . .
    Now there are no errors, but nothing is happening. The console is just blank and sits there. My guess is that there is an infinate loop in there somehow. It looked like the files are not being created, either. (possibly the reason for the infinate loop?)

  3. #18
    Join Date
    Apr 1999
    Posts
    27,449

    Re: File I/O help

    Quote Originally Posted by JasonBB
    The FAQ itself is not insulting, this link is:
    http://www.parashift.com/c++-faq-li...st.html#faq-5.8
    And that is insulting how? It merely describes what should be done if code does not work. What objection do you have to it?
    Now there are no errors, but nothing is happening. The console is just blank and sits there. My guess is that there is an infinate loop in there somehow. It looked like the files are not being created, either. (possibly the reason for the infinate loop?)
    Then it's time to learn to use the debugger that should be available with your compiler suite. Learning to use the debugger is part and parcel of learning C++ programming.

    You should be single-stepping through your program, watching the variables, checking the flow, etc. and seeing what values are being set where.

    Regards,

    Paul McKenzie

  4. #19
    Join Date
    Jul 2008
    Posts
    27

    Re: File I/O help

    I've been using the debugger-- that is what brought up the runtime errors earlier. I tried adding this right after the file_in.open() statement, and it still went throught the loop:
    Code:
    if(!file_in.is_open()){return 0;}
    So I asume the file is open, however when I added a cout to the loop, it echo's out blanks in the for statement and nonsense in the rest of the loop.

  5. #20
    Join Date
    Jul 2008
    Posts
    27

    Re: File I/O help

    Woah! I put this at the end of the loop:
    Code:
     cout << file_in.eof() << "|" << file_in.tellg();
    and it is printing 0|-1 over and over.

    Here is an update on the code:

    Code:
    #include<string>
     
    #include<iostream>
     
    #include<fstream>
     
    #include<stdio.h>
     
    #include<vector>
     
    usingnamespace std;
     
    /*functions */
     
    int main(int arg_num, char* args[]){
    ifstream file_in;
    ofstream file_out;
     
    file_in.open("test.txt");
     
    file_in.open("enc.txt");
     
    string mystring;
     
    std::vector<char> buffer(350);
     
    while(!file_in.eof()) {
     
    for (int i = 0; i < 350; i++) {
     
    buffer[i] = file_in.get();
     
    }
     
    mystring = &buffer[0];
     
    //mystring = my_function(mystring);
     
    file_out << mystring;
     
    cout << file_in.eof() << "|" << file_in.tellg();
     
    }
     
    file_in.close();
     
    file_out.close();
     
    file_in.open("enc.txt");
     
    file_out.open("unenc.txt");
     
    while(!file_in.eof()) {
     
    for (int i = 0; i < 350; i++) {
     
    buffer[i] = file_in.get();
     
    }
     
    mystring = &buffer[0];
     
    //mystring = my_function(mystring);
     
    file_out << mystring;
     
    }
     
    file_in.close();
     
    file_out.close();
     
    return 0;
    
    Last edited by JasonBB; July 6th, 2008 at 12:06 AM.

  6. #21
    Join Date
    Apr 1999
    Posts
    27,449

    Re: File I/O help

    Quote Originally Posted by JasonBB
    I've been using the debugger-- that is what brought up the runtime errors earlier. I tried adding this right after the file_in.open() statement, and it still went throught the loop:
    Code:
    if(!file_in.is_open()){return 0;}
    So I asume the file is open, however when I added a cout to the loop, it echo's out blanks in the for statement and nonsense in the rest of the loop.
    Instead of trying to read 350 characters, read the whole file into a buffer. Then process 350 characters at a time from the buffer. This is much easier than what you are doing now:
    Code:
    #include<iostream>
    #include<fstream>
    #include<cstdio>
    #include<vector>
     
    using namespace std;
     
    int main()
    {
       ifstream file_in;
       file_in.open("test.txt");
     
       std::vector<char> buffer;
       char c;
       while(file_in.get(c)) 
       {
            buffer.push_back(c);
            cout << c;
        }
    }
    What gets outputted? Is it the file contents? If not, work on getting this program to work first.

    Once you get it to work, then you have a buffer that has all of the characters. Then you write a additional code to process 350 characters at a time from the buffer, instead of trying to do this processing while reading a file.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 6th, 2008 at 12:21 AM.

  7. #22
    Join Date
    May 2008
    Posts
    96

    Re: File I/O help

    You have opened file_in twice (lines 20 and 22). Be careful there.

  8. #23
    Join Date
    Jul 2008
    Posts
    27

    Re: File I/O help

    Duoas was right, file_in was opened twice. That got the code kind of working.

    Two things now:
    1) file2 (the first file_out) has extra characters at the end.
    €€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€ээээЂЂЂЂЂЂЂЂоюоюою
    I imagine that it is taking all 350 chars from buffer, regardless of whether or not they have a value, and placing it in the string.

    2) file3 (the second file_out) is created but empty for some reason.

    @Paul McKenzie: I don't think I can do what you suggest, because the file is 100 MB.
    Last edited by JasonBB; July 6th, 2008 at 08:43 AM.

  9. #24
    Join Date
    Jul 2008
    Posts
    27

    Re: File I/O help

    I think I solved problem 2:
    Code:
    #include<fstream>
     
    #include<stdio.h>
     
    #include<vector>
     
    usingnamespace std;
     
    /*functions */
     
    int main(int arg_num, char* args[]){
    ifstream file_in;
    
    ofstream file_out;
    
    file_in.open("test.txt", ios_base::in);
    
    file_out.open("enc.txt", ios_base::out);
    
    string mystring;
    
    std::vector<char> buffer(350);
    
    while(!file_in.eof()) {
    
    for (int i = 0; i < 350; i++) {
    
    buffer[i] = file_in.get();
    
    }
    
    mystring = &buffer[0];
    
    //mystring = my_function(mystring);
    
    file_out << mystring;
    
    }
    
    file_in.close();
    
    file_out.close();
    
    file_in.open("enc.txt", ios_base::in);
    
    file_out.open("unenc.txt", ios_base::out);
    
    file_in.seekg(0,ios_base::beg);
    
    while(!file_in.eof()) {
    
    for (int i = 0; i < 350; i++) {
    
    buffer[i] = file_in.get();
    
    }
    
    mystring = &buffer[0];
    
    //mystring = my_function(mystring);
    
    file_out << mystring;
    
    }
    
    file_in.close();
    
    file_out.close();
     
    return 0;
     
    }
    

  10. #25
    Join Date
    Apr 1999
    Posts
    27,449

    Re: File I/O help

    There are logic issues with your code that have nothing to do with syntax.

    For example, how do you know that the file size is an exact multiple of 350 bytes?
    Code:
    while(!file1.eof()) 
    {
       for (int i = 0; i < 350; i++) 
       {
           buffer[i] = file_in.get();
       }
    }
    What if there are only 100 bytes left when you hit that for loop()? You will read an "extra", non-existant 250 bytes. You cannot hardcode 350 in that for loop. The loop must
    terminate when the EOF has been reached, or 350 bytes have been read, whichever comes first.

    Second, your string is not null-terminated, or you can't assume it's null-terminated. Therefore the proper way to assign to a std::string is using the append() function:
    Code:
    mystring.append(&buffer[0], total_number_of_characters_read_in);
    That is more than likely the reason for the extra characters. You're using functions that require NULL terminated strings, and there is no guarantee that buffer is NULL terminated.

    Regards,

    Paul McKenzie

  11. #26
    Join Date
    Jul 2008
    Posts
    27

    Re: File I/O help

    Thanks, Paul. I did this:

    Code:
    #include<fstream>
     
    #include<stdio.h>
     
    #include<vector>
     
    usingnamespace std;
     
    /*functions */
     
    int main(int arg_num, char* args[]){
    
    ifstream file_in;
    
    ofstream file_out;
    
    file_in.open("test.txt", ios_base::in);
    
    file_out.open("enc.txt", ios_base::out);
    
    string mystring;
    
    std::vector<char> buffer(350);
    
    while(!file_in.eof()) {
    
    for (int i = 0; i < 350 && !file_in.eof(); i++) {
    
    buffer[i] = file_in.get();
    
    }
    
    mystring = &buffer[0];
    
    //mystring = e.encrypt(mystring);
    
    file_out << mystring;
    
    }
    
    file_in.close();
    
    file_out.close();
    
    file_in.open("enc.txt", ios_base::in);
    
    file_out.open("unenc.txt", ios_base::out);
    
    file_in.seekg(0,ios_base::beg);
    
    while(!file_in.eof()) {
    
    for (int i = 0; i < 350 && !file_in.eof(); i++) {
    
    buffer[i] = file_in.get();
    
    }
    
    mystring = &buffer[0];
    
    //mystring = e.unencrypt(mystring);
    
    file_out << mystring;
    
    }
    
    file_in.close();
    
    file_out.close();
     
    return 0;
    }
    
    But there is still 1 extra char on the end. I tried to use the append function as well, but that didn't solve it either, so I took it out again.

  12. #27
    Join Date
    Apr 1999
    Posts
    27,449

    Re: File I/O help

    You may just want to keep a counter and increment it for each character read in. Or do this:
    Code:
    while(!file_in.eof()) 
    {
        unsigned int totalBytes = 0;
        for (int i = 0; i < 350 && !file_in.eof(); i++) 
        {
             buffer[i] = file_in.get();
             ++totalBytes;
        }
        //...
        mystring.append(&buffer[0], totalBytes);
    }
    The cout << mystring, should be replaced with cout.write() since you are not writing a NULL terminated string.
    Code:
    cout.write(&buffer[0], totalBytes);
    Passing just a std::string to cout using << is not going to work if the string is not NULL terminated.

    Also, it's very hard to read your code, since it's not indented, double spaced, and frankly, uses too many colors so it's very difficult to edit. Make it very simple by using the simplest of code tags.

    Regards,

    Paul McKenzie

  13. #28
    Join Date
    Jul 2008
    Posts
    27

    Re: File I/O help

    Quote Originally Posted by Paul McKenzie
    You may just want to keep a counter and increment it for each character read in. Or do this:
    Code:
    while(!file_in.eof()) 
    {
    unsigned int totalBytes = 0;
    for (int i = 0; i < 350 && !file_in.eof(); i++) 
    {
    buffer[i] = file_in.get();
    ++totalBytes;
    }
    //...
    mystring.append(&buffer[0], totalBytes);
    }
    Ahead of you on that. Tried it and it didn't help. I am getting an extra char on each loop, it seems, so even if it reads in 350 chars, it outputs an extra char.

    I am poking around to see if I can find the problem.

    The cout << mystring, should be replaced with cout.write() since you are not writing a NULL terminated string.
    Code:
    cout.write(&buffer[0], totalBytes);
    Passing just a std::string to cout using << is not going to work if the string is not NULL terminated.
    I'll try playing around with that.

    Also, it's very hard to read your code, since it's not indented, double spaced, and frankly, uses too many colors so it's very difficult to edit. Make it very simple by using the simplest of code tags.
    I agree, but that is what happens when I paste in the code. It removes the indenting and adds some odd colors. It annoys me as well.

  14. #29
    Join Date
    Jul 2008
    Posts
    27

    Re: File I/O help


    The cout << mystring, should be replaced with cout.write() since you are not writing a NULL terminated string.


    Code:
    cout.write(&buffer[0], totalBytes);


    Passing just a std::string to cout using << is not going to work if the string is not NULL terminated.
    Wait, then how do I modify the string with my function?

  15. #30
    Join Date
    Apr 1999
    Posts
    27,449

    Re: File I/O help

    Quote Originally Posted by JasonBB
    Ahead of you on that. Tried it and it didn't help. I am getting an extra char on each loop
    You need to inspect the data with the debugger and verify if the data is correct, regardless of what you're trying to output. You need to establish what is wrong -- the reading of the data, or the outputing of the data.

    Regards,

    Paul McKenzie

Page 2 of 4 FirstFirst 1234 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)