Thread: I need help with file I/O

  1. #1
    Registered User osolis's Avatar
    Join Date
    Apr 2005
    Posts
    4

    I need help with file I/O

    Hello, Im an amateur C++ programmer and I need help with something im trying to code. Im trying to open a file and edit a certain adress in the file. I figured out the hex offset adress' i need to edit but i dont have the slightest ideas of how to do this.

    Any help would be greatly appreaciated thanks.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    Read in a word
    If word == target
       print modified word
    else
       print word

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    You'll want to open the file in binary mode, and then use the stream's seekg() method to go to the right position in the file, and then use steam's read() method to actually read the data.

    Code:
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	//Create and open an input stream
    	ifstream in_stream;
    	in_stream.open("nameoffile", ios::binary);
    	//Check the stream for good state
    
    	//Var for offset
    	streamoff offset = 0x0000;  //Replace this with the hex offset you need
    	
    	//Seek to position in file
    	in_stream.seekg(offset);
    	//Make sure we haven't surpassed the EOF
    
    	//Read 10 bytes from stream
    	char buffer[10];
    	in_stream.read(buffer, 10);
    	//Check for failure
    }
    If you need to learn more about streams, there's references all over the place

    [EDIT]
    To write to the file you use the exact same process with the seekp() and write() methods of ostream

  4. #4
    Registered User osolis's Avatar
    Join Date
    Apr 2005
    Posts
    4
    Thanks for the help.

    I got to edit the right spot but everytime I run my program it overwrites the file. Im still in the process of figuring out how to stop it from doing this.
    Last edited by osolis; 04-19-2005 at 05:50 PM.

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Code:
    ofstream outfile("file",ios::binary | ios::out);
    I think this will exclude the ios::trunc bit. The file won't be erased.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Registered User osolis's Avatar
    Join Date
    Apr 2005
    Posts
    4
    I just tried it and it didnt work. I wrote a program to edit a text file my source code is:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    ofstream dataFile;
    
    int main()
    {	
    	dataFile.open("hello.dat", ios::binary | ios::out);
    	
    	dataFile.seekp(4L, ios::beg);
    
    	dataFile.write("hell", 5);
    	
    	dataFile.close();
    	return 0;
    }
    The file "hello.dat" contains "123456789abcdefg"
    The word "hell" comes out where it suppose to but it erases everything else.

  7. #7
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I think you need to read up on streams.

    Code:
    /************************************************
        A program to read a file
        find an address and then 
        change that address to 'hell'
        
        Eg.
        Where there is '4L' replace it with 'hell'
        Read file "data.txt"= 12345678abcdefg4Lk23
        Write file "newdata.txt= 12345678abcdefghellk23
        
    *************************************************/
    
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    
    int main()
    {
        fstream file_pointer_in;
        file_pointer_in.open("data.txt",ios::in);
        
        fstream file_pointer_out;
        file_pointer_out.open("newdata.txt",ios::out);
        
        char array[81];
        do{
            file_pointer_in>>array;
            int size=strlen(array);
            int tag;
            for(int a=0; a<size; a++)
            {
                //edit the address here '4'
                if(array[a]=='4')
                {
                    tag=a;
                    //edit the address here 'L'
                    if(array[a+1]=='L')
                    {
                        //edit the word you wish to change it to
                        file_pointer_out<<"hell";
                    }
                    
                }
                if((a!=tag)&&(a!=tag+1))
                {  
                file_pointer_out<<array[a];
                }    
            }
           }while(file_pointer_in.peek()!=EOF);
           
          file_pointer_in.close();
          file_pointer_out.close();
      
      cout<<"Please take a look at the file 'newdata.txt'";  
      int stop;
      cin>>stop;
      return 0;
    }

  8. #8
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    ***edit

    Code:
                 if((a!=tag))
                {  
                file_pointer_out<<array[a];
                } 
                else if((a!=tag+1))
                {  
                file_pointer_out<<array[a];
                }

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I'd try to open the file in ios::app mode rather than the default ios::trunc mode. ios::app mode with an ofstream stream should allow the file to open without overwriting the information currently in the file and place the filepointer to write to the file at the end of the file, or the "append to file" location. Since that isn't where you want to start the writing, call the seekp() method to reposition the file writing pointer.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM