open input file twice?

This is a discussion on open input file twice? within the C++ Programming forums, part of the General Programming Boards category; if anyone can help, it would be appreciated. i have opened up a file for input. this input file is ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    27

    open input file twice?

    if anyone can help, it would be appreciated.

    i have opened up a file for input. this input file is then read from byte 1000 til the end, and then a byte count is calculated (x). After this, i have calculated a few other things using the final byte count. i.e. (x - 44).

    Then, i have opened up output and written a file header there using the calculations (x - 44).

    how do i go about reading the input file again after already having written to output ? I want to start at byte 1000 again, and write from byte 1000 til the end AFTER the header i just wrote in output. I have tried this:

    fout.put(char);

    but it only seems to work BEFORE i have written to the output file. I can write each byte of the input file before the header, but have had no luck after the header, which i need. is it possible to open the input file twice and get what i need?

    any ideas/suggestions?

    Thanks,
    Keith

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    load it into memory

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Your post is about as clear as mud. Maybe this example will help:

    Code:
    #include<string>
    #include <fstream>
    using namespace std;
    
    int main()
    { 
    	ifstream inFile("C:\\TestData\\input.txt");
    	string text;
    	inFile>>text;
    	
    	ofstream outFile("C:\\TestData\\output.txt");
    	outFile<<text<<endl;
    
    	inFile.seekg(0);//sets the input file pointer to 0
    	
    	inFile>>text;//reads input file again
    	outFile<<text;	
    
    	return 0;
    }
    Last edited by 7stud; 07-30-2003 at 02:12 PM.

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    i meant load it into memory, character array or what ever and manipulate/play with it there
    make a string/array read the file in. Then instead of going through the file again. Go through the string/array as many times as you wish. Parsing/etc.
    If you allocated memory remember to free() it err since this is the C++ board... delete it.
    std::string would be the best bet.

    Or if you don't like dealing with it in memory (for me its usually easier to do in memory or the on just the first run-through) you can use 7stud's pretty good method.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    27
    Thanks, all. this helped mucho!

    keith

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Open file from user input?
    By p0gig in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2004, 08:09 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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