fstream

This is a discussion on fstream within the C++ Programming forums, part of the General Programming Boards category; im quite confident reading and writing to files but i was wandering how it would be possible to lift records ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    14

    fstream

    im quite confident reading and writing to files but i was wandering how it would be possible to lift records from one file and put it into another file

    for example two text files and i want to copy a character or string from file one and put it into file 2

  2. #2
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    >> for example two text files and i want to copy a character or string from file one and put it into file 2

    uhm. would it not follow some logic similar to

    open fileA
    open fileB
    read stringX from fileA
    write stringX to fileB
    close fileA and fileB

    or is it more complicated..

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    14
    yeah i got the psuedo...im just struggling with the actual program

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You can't simply pick up a piece of data from one file and insert it in another. The logic required is similar to what xhi described.

    Open fileA
    Read in data from fileA
    Extract the data you want copied.
    Close fileA
    Open fileB
    Read in data from fileB
    Modify the data how you want in memory (including adding a string from fileA's data)
    Write the modified data to fileB
    Close fileB

    If you want the data to simply be added to the end of fileB, then you can open the file in "append write" mode and just specify what you want added onto the end.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    yeah i got the psuedo...im just struggling with the actual program
    Then you need to post the code you have so far and show us where specifically you need help.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    14
    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main () {
    
      char buffer[];
      int size;
    
      ifstream file_in ("new.txt",ios::in);
      ofstream file_out ("test.txt",ios::out);
      file_in.read (buffer,size);
      file_out.write (buffer,size);
      delete[] buffer;
      file_in.close();
      file_out.close();
      return 0;
    }
    Hopefully this gives a better idea..... its not full and that hopefully with tinkering would copy the entire contents of one file to another

  7. #7
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    >> Hopefully this gives a better idea..... its not full and that hopefully with tinkering would copy the entire contents of one file to another

    well a start would be some type of structure that would make the program read and write as many times as it takes until the file is copied...

    there is a new thing that i saw last month.. it is hidded under keyword
    Code:
    loop
    so with a "loop" and my original pseudo

    open fileA
    open fileB
    start loop_until_EOF
    read stringX from fileA
    write stringX to fileB
    end loop_until_EOF
    close fileA and fileB

    ** not trying to be an @ss, but this smells of homework that not a serious attempt has been made at..

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    - You need to define size
    - You shouldn't delete[] stuff that wasn't created with new[]

    I recommend going over the tutorials on this site about file I/O so you can get a better understanding of what's actually going on.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    14
    no im 18 and finished education last year....this is purely just trying to learn C++ by setting myself little projects. this is just a small function of wat i need to do i have already written all my pseudo... as i was brought up using VB (which smells of wee)
    Last edited by Flip; 12-30-2005 at 02:01 PM.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Use the C++ string class instead of character arrays and use operator>> or getline to read in strings instead of read(). Then use operator<< instead of write to write out the strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 09:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 11:21 AM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21