dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 52

Thread: File I/O help

  1. #1
    Join Date
    Jul 2008
    Posts
    27

    File I/O help

    Hey everone,

    I recently decided to try programming in C++, and so far I have been doing very well on my own. However, I ran into a massive wall in the program I am trying to build: C++ file I/O functions. After weeks of experimenting and searching, I still can't figure out what I am doing. Hopefully someone can help.

    I need to read in 350 characters from file1 as a string, pass it to a function I created to modify the string (it takes in a string and returns a string), and then output the string to file2. This loops until file1 has been completely read in. Then, file2 needs to be read in the same way and output to file3.

    In psuedocode:
    Code:
    file1 = open "test.txt";
    file2 = open "test2.txt";
    
    string mystring
    
    while(!EOF file1){
        mystring = read 350 chars from file1;
        mystring = my_function(mystring);
        write mystring to file2;
    }
    
    close file1;
    close file2;
    
    file1 = open "test2.txt";
    file2 = open "test3.txt";
    
    while(!EOF file1){
        mystring = read 350 chars from file1;
        mystring = another_my_function(mystring);
        write mystring to file2;
    }
    
    close file1;
    close file2;
    Does that make sense?

    I can't use the getline function, because there are few new lines if any in the 100 MB file to be read in and changed, and I only want 350 characters at a time. Also, newlines and spaces need to be preserved.

    Thanks for your time,
    Jason
    Last edited by JasonBB; July 5th, 2008 at 11:56 AM.

  2. #2
    Join Date
    Jun 2008
    Location
    Oregon, USA
    Posts
    63

    Re: File I/O help

    you should be able to use the get() function to get a single character
    (char ch = in.get())

    Code:
    // recommendations, file1 and file2 aren't the best of names for different streams (in and out)
    ifstream file1;    // rename this to in, or input
    ofstream file2;   // rename this to out, or output
    
    // int main(int argc, char** argv) {
    
    file1.open("test.txt");
    file2.open("test2.txt");
    
    string mystring;
    
    char* buffer;
    while(!file1.eof()) {
        for (int i = 0; i < 350; i++) {
            buffer[i] = file1.get();
        }
        delete[] buffer;
        mystring = buffer;
        mystring = my_function(mystring);
        file2 << mystring;
    }
    
    file1.close();
    file2.close();
    
    file1.open("test2.txt");
    file2.open("test3.txt");
    
    while(!file1.eof()) {
        for (int i = 0; i < 350; i++) {
            buffer[i] = file1.get();
        }
        delete[] buffer;
        mystring = buffer;
        mystring = another_my_function(mystring);
        file2 << mystring;
    }
    
    file1.close();
    file2.close();
    
    // }
    I haven't tested the code, but I don't see why it wouldn't work. (also, I think it would be helpful to know that file1.open(char*) works but not file1.open(string) and double quotes in C++ makes a char* not a string, I had a hard time debugging that one time.)
    Last edited by Bluefox815; July 5th, 2008 at 02:05 PM.

  3. #3
    Join Date
    May 2008
    Posts
    96

    Re: File I/O help

    You can also use the STL
    Code:
    #include <algorithm>
    #include <fstream>
    #include <string>
    
    ...
    
    std::string mystring;
    
    ...
    
    file1 >> noskipws;
    while (file1)
      {
      mystring.clear();
      copy_n(
        back_inserter( mystring ),
        350,
        istream_iterator <char> ( file1 )
        );
      mystring = my_function( mystring );
      file2 << mystring;
      }
    If you are working with binary data, make sure to open the file with ios::binary mode.

    You may not have the copy_n() template function. (Apparently it wasn't part of the last standard, alas.) Here it is:
    Code:
      template <
        typename InputIterator,
        typename SizeType,
        typename OutputIterator
        >
      inline OutputIterator copy_n(
        InputIterator  first,
        SizeType       count,
        OutputIterator result
        ) {
        for (; count > 0; --count)
          *result++ = *first++;
        return result;
        }
    Hope this helps.

    [edit] Yeah, that fstream doesn't take std::string thing always gets me too... But you can always say:
    Code:
    ifstream foo( filename.c_str() );
    Heh...

  4. #4
    Join Date
    Jul 2008
    Posts
    27

    Re: File I/O help

    Thanks guys. That give me more information than I have been able to find in weeks of searching! I went with Bluefox815 code (it looks closer to some of my previous attempts), but I get this error when I run the program:

    Run-Time Check Failure #3 - The variable 'buffer' is being used without being initialized.
    I am not sure how to go about fixing it. There are no compiling errors.

    BTW: what does the asteric ("*") do? i.e. char* buffer? I guess it creates an array of chars?

    And, I know this sounds stupid, but how would I know if I am dealing with binary data?
    Last edited by JasonBB; July 5th, 2008 at 08:00 PM.

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

    Re: File I/O help

    Quote Originally Posted by JasonBB
    I get this error when I run the program:
    The error means what it says. You're using a variable that has not been initialized.
    I am not sure how to go about fixing it.
    You fix it by initializing the variable before you use it.
    BTW: what does the asteric ("*") do? i.e. char* buffer?
    The * means pointer. I suggest you get a basic book or tutorial on C++, as this is a very basic topic (pointers).
    I guess it creates an array of chars?
    No it doesn't.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jul 2008
    Posts
    27

    Re: File I/O help

    Er, I figured that much about the error message. But how do I initialize the variable?

    And, yes, I probably should a more serious look at a tutorial, but for now I just want to finish the program.

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

    Re: File I/O help

    Quote Originally Posted by JasonBB
    Er, I figured that much about the error message. But how do I initialize the variable?
    Where is the code?
    And, yes, I probably should a more serious look at a tutorial, but for now I just want to finish the program.
    If you're not aware that char* does not create a buffer, your program will have more problems than you believe it will.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Jul 2008
    Posts
    27

    Re: File I/O help

    Quote Originally Posted by Paul McKenzie
    Where is the code?
    . . . It's the same code Bluefox815 posted, just with different file names, and as suggested I changed file1 to file_in and file2 to file_out.

    If you're not aware that char* does not create a buffer, your program will have more problems than you believe it will.
    You sound like a happy, optimistic guy. I am looking for answers, not criticism.

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

    Re: File I/O help

    Quote Originally Posted by JasonBB
    You sound like a happy, optimistic guy. I am looking for answers, not criticism.
    No, I'm being realistic.

    If you are not familiar with C++ enough to know the basics, you will have problems with your program, if not now, then later. This is especially the case with pointers (which a char * is), and dynamic memory allocation.

    Regards,

    Paul McKenzie

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

    Re: File I/O help

    Quote Originally Posted by JasonBB
    . . . It's the same code Bluefox815 posted, just with different file names, and as suggested I changed file1 to file_in and file2 to file_out.
    And that code is incomplete and buggy. Please post the actual code you're using.
    Code:
    char* buffer;
    while(!file1.eof()) {
        for (int i = 0; i < 350; i++) {
            buffer[i] = file1.get();
        }
    delete[] buffer;
    This is wrong, as buffer was never allocated. If you could get this program to compile, you would more than likely get a crash when you run it.

    You can't just take a random post, not know what it really does, and try to make it do something. You cannot program C++ this way -- you must know each and every step that you're doing.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 5th, 2008 at 09:12 PM.

  11. #11
    Join Date
    Jul 2008
    Posts
    27

    Re: File I/O help

    Then help me understand. What should the code be? So far I have gotten no information from you in four posts.

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

    Re: File I/O help

    Quote Originally Posted by JasonBB
    Then help me understand. What should the code be?So far I have gotten no information from you in four posts.
    So I wasted my time pointing out to you that the code you copied is not correct, and showed you why it isn't correct, that char* is a pointer, your code will crash, etc.??
    Code:
    delete [] buffer;
    This requires that

    1) buffer is a pointer AND

    2) The pointer was initialized using operator new[]

    The code did not do 2).

    All of this is discussed in any C++ book or tutorial (management of the C++ free-store). And as I mentioned in my other posts, managing the free-store is not a trivial task -- it requires that you know every step of what you're doing.

    Also, where is the code that you wrote? You still haven't posted it. Once you post it, then and only then can it be commented on. You stated you have syntax errors and all kind of other errors, so post the code so that you get a proper assessment of what needs to be done.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Jul 2008
    Posts
    27

    Re: File I/O help

    Quote Originally Posted by Paul McKenzie
    So I wasted my time pointing out to you that the code you copied is not correct, and showed you why it isn't correct, that char* is a pointer, your code will crash, etc.??
    *sigh*
    I figured the code wasn't correct because it didn't run. I still don't know why, so I don't see what your talking about on that point. What is a pointer? I tried googling it and can't figure it out. Telling me that my code will crash isn't at all helpful unless I know why. I am used to forum posts where users say "here is the problem, here is what is causing it, here is a suggestion on fixing it." To me (keeping in mind that I am very new to C++), it just sounds like "here is the problem."

    I don't want to argue, I just want some answers. I have been working on this for program for a month, and this is all I need to do to finish it. If you are getting annoyed, then it might be better to let someone else answer instead.

    Code:
    delete [] buffer;
    This requires that

    1) buffer is a pointer AND

    2) The pointer was initialized using operator new[]

    The code did not do 2).
    Now we may be getting somewhere. How would that fit into the code that Bluefox815 provided?

    All of this is discussed in any C++ book or tutorial (management of the C++ free-store). And as I mentioned in my other posts, managing the free-store is not a trivial task -- it requires that you know every step of what you're doing.
    I will look into that stuff more when I finish this program. I will be leaving town soon, so I don't have much time.

    Also, where is the code that you wrote? You still haven't posted it. Once you post it, then and only then can it be commented on. You stated you have syntax errors and all kind of other errors, so post the code so that you get a proper assessment of what needs to be done.
    I told you, my code is only what Bluefox815 posted. If it makes you happy, then here:

    Code:
    #include<string>
    
    #include<iostream>
    
    #include<fstream>
    
    #include<stdio.h>
    
    usingnamespace std;
    
    /*More stuff here*/
     
    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;
    
    char* buffer;
    
    while(!file_in.eof()) {
    
    for (int i = 0; i < 350; i++) {
    
    buffer[i] = file_in.get();
    
    }
    
    delete[] buffer;
    
    mystring = buffer;
    
    mystring = my_function(mystring);
    
    file_out << mystring;
    
    }
    
    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();
    
    }
    
    delete[] buffer;
    
    mystring = buffer;
    
    mystring = my_function(mystring);
    
    file_out << mystring;
    
    }
    
    file_in.close();
    
    file_out.close();
    
    return 0;
    
    }
    
    

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

    Re: File I/O help

    Quote Originally Posted by JasonBB
    *sigh*
    I figured the code wasn't correct because it didn't run. I still don't know why, so I don't see what your talking about on that point. What is a pointer? I tried googling it and can't figure it out.
    A pointer is a variable that holds a memory location. This is a basic C++ topic -- pointers.
    I am used to forum posts where users say "here is the problem, here is what is causing it, here is a suggestion on fixing it." To me (keeping in mind that I am very new to C++), it just sounds like "here is the problem."
    If you don't understand the problem, what good is the fix? Also, if you're told what the problem is, it isn't difficult to research the fix yourself. You also (up until now) didn't post the code that *you* wrote. No one can work with an incomplete post by someone else.
    I told you, my code is only what Bluefox815 posted.
    The code you posted is *not* the same code as what Bluefox815 posted. I already mentioned to you that the code that Bluefox815 posted was incomplete, and complete code is what needs to be posted. Notice that your code has headers and a main() program. All of this needs to be present to diagnose compiler and runtime errors.

    May I suggest you read the C++ FAQ here:

    http://www.parashift.com
    http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

    Code:
    char * buffer = new char [350];
    //...
    delete [] buffer;
    That is how you properly use new[] and delete[]. But this is only the surface, as mismanaging the memory that is returned will cause other issues.

    The code also calls delete[] on a buffer that was already deleted. This is incorrect.

    Instead of all of this, use std::vector<>
    Code:
    #include <vector>
    //...
    std::vector<char> buffer(350);
    //...
    mystring = &buffer[0];
    Then there is no need for delete[] (or new[]).

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 5th, 2008 at 10:21 PM.

  15. #15
    Join Date
    Jul 2008
    Posts
    27

    Re: File I/O help

    Quote Originally Posted by Paul McKenzie
    A pointer is a variable that holds a memory location. This is a basic C++ topic -- pointers.
    Vague, but thank you.
    If you don't understand the problem, what good is the fix? Also, if you're told what the problem is, it isn't difficult to research the fix yourself.
    . . . Isn't the point of a forum to help you learn? I understand that one needs to learn to find answers, but at the stage I am at right now, I need someone to help me find answers.

    I am not new to programming, only to compiled languages. Starting is the hardest, slowest part, and it helps a lot when people can show you solutions and how they arrived at them. Don't asume that I haven't looked myself.
    You also (up until now) didn't post the code that *you* wrote. No one can work with an incomplete post by someone else.
    The code you posted is *not* the same code as what Bluefox815 posted. I already mentioned to you that the code that Bluefox815 posted was incomplete, and complete code is what needs to be posted. Notice that your code has headers and a main() program. All of this needs to be present to diagnose compiler and runtime errors.
    Unless you though that I forgot headers and the main method, I don't see why you needed my version of the code.
    Slightly insulting, but okay.
    Code:
    char * buffer = new char [350];
    //...
    delete [] buffer;
    That is how you properly use new[] and delete[]. But this is only the surface, as mismanaging the memory that is returned will cause other issues.

    The code also calls delete[] on a buffer that was already deleted. This is incorrect.

    Instead of all of this, use std::vector<>
    Code:
    #include <vector>
    //...
    std::vector<char> buffer(350);
    Then there is no need for delete[] (or new[]).
    Thank you! Now that is helpful! However, I get the following error:
    error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)
    Code:
    #include<string>
     
    #include<iostream>
     
    #include<fstream>
     
    #include<stdio.h>
    #include<vector>
     
    
     
    usingnamespace std;
     
    /*More stuff here*/
     
    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;
     
    mystring = my_function(mystring);
     
    file_out << mystring;
     
    }
     
    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;
     
    mystring = my_function(mystring);
     
    file_out << mystring;
     
    }
     
    file_in.close();
     
    file_out.close();
     
    return 0;
     
    }
    
    The line in question is:

    mystring = buffer;

    Last edited by JasonBB; July 5th, 2008 at 10:35 PM.

Page 1 of 4 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)