GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Feb-2005, 11:54
technickel technickel is offline
New Member
 
Join Date: Feb 2005
Location: Kalifornia
Posts: 11
technickel is on a distinguished road

Made program in Java, trying C++ now, file i/o problems


I'm basically practicing programming in C++. I have made this program in Java, but I'm having problems writing it again in C++. This is the program assignement I have given myself: http://www.cse.ucsc.edu/classes/cmps109/Winter02/hw/hw1.html

Note that this programming assignment is from 2002, and it is now 2005, so no, I'm not trying to cheat on some programming assignment.

My problem:
I found it easy to open a file in Java and copy its contents to a buffer where I could edit the file's contents and later dump the modified buffer to a new file. This is how I opened the file and put its contents into a buffer in Java:

JAVA Code:
static StringBuffer inputTheFile(String fileName) throws IOException {
        StringBuffer buf = new StringBuffer();
        
        // we first read the characters into a char array
        char[] cbuf = new char[1000];
        
        // FileReader is a standard Java class in package java.io.
        FileReader in = new FileReader(fileName);
        
        // read() is an operation implemented by the class FileReader
        // it takes as a parameter a char array. It fills the array
        // and returns how many characters it put into the array.
        int count = in.read(cbuf);
        
        System.out.println("FileReader read in " + count + " characters");
        
        while (count >= 0) {
            // append() is an operation implemented by the class StringBuffer
            // This call appends the characters from index 0 to index count-1.
            buf.append(cbuf, 0, count);
            count = in.read(cbuf);
        }
        in.close();
        return buf;
    }

I have tried to do the same in C++, but as a preliminary step I thought I should just make a function that will dump the file contents to the console (implement the 'p' command from the assignment). The problem is that it only dumps the contents once. Successive calls to DumpFileContents() will only print the last 'endl'. Here is a portion of my C++ code:

CPP / C++ / C Code:

SimpleFileBuffer::SimpleFileBuffer( char* inFileName, char* outFileName )
{	
	//open the given files  
        in_file.open(inFileName,ios::in);                     // an ifstream object
	out_file.open(outFileName, ios::out);             // an ofstream object
}

void SimpleFileBuffer::DumpFileContents()
{

	char str[2000];

        // make sure the get pointer is at the beginning of the file
	in_file.seekg(0, ios::beg);

        // dump the file contents to the console
        while(!in_file.eof())
        {
            in_file.getline(str,2000);
            cout << str << endl;
        }         	
        
        // put the get pointer back at the beginning of the file
	in_file.seekg(0, ios::beg);
	
	cout <<endl;
}


Am I basically doing the first half of the work here, taking the file contents from the file and putting them into a char[] without then putting the contents of the char[] into a buffer? If so, what sort of buffer should I be transferring the char[] contents to? Why can I only see the contents of the file once using this function?
  #2  
Old 10-Feb-2005, 13:20
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
C++ STL stream objects contain status flags to keep track of things such as errors or end-of-file. Once you reach EOF while reading from the input stream, the status flag is changed to indicate this. The result is that further operations on that stream may require that the EOF flag be set to false/off. So, even though you tried to rewind the get-pointer, it had no effect.

Solution: Reset the status flags before rewinding the stream.
CPP / C++ / C Code:
        // put the get pointer back at the beginning of the file
  in_file.clear();
  in_file.seekg(0, ios::beg);

Matthew
  #3  
Old 10-Feb-2005, 18:44
technickel technickel is offline
New Member
 
Join Date: Feb 2005
Location: Kalifornia
Posts: 11
technickel is on a distinguished road
Quote:
Originally Posted by QED
Solution: Reset the status flags before rewinding the stream.
CPP / C++ / C Code:
        // put the get pointer back at the beginning of the file
  in_file.clear();
  in_file.seekg(0, ios::beg);

Matthew

Yup, you were right. I thought I had read everything about file i/o and was doing it right, looks like I was a little too confident. I added the clear() and it worked fine.

Now say I wanted to edit the contents of the file. Do I want to take the contents of the file and put them into some other object, or would using the ifstream object be the right object for the task?
  #4  
Old 10-Feb-2005, 21:12
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
It really depends on what you mean by "edit". There is a structure called "streambuf" underlying the iostream classes, which you can edit the contents of (see links "iostream library" and then "streambuf" at this page ). However, what this buffer contains at any given time I am not sure, and the interface is not really designed for your needs.

I would recommend copying the input to a string buffer, and editing it there, since std::string has a richer interface (see this page for the details).

Or, better yet, edit the input on the fly as you read it from the stream, so your string buffer contains only the final, edited contents. Then write that to file as desired.

If you can be more specific about your editing requirements, I might be able to suggest other ideas.

Matthew
  #5  
Old 19-Feb-2005, 00:32
technickel technickel is offline
New Member
 
Join Date: Feb 2005
Location: Kalifornia
Posts: 11
technickel is on a distinguished road
Thanks Matthew!

I used an ifstream to take the contents of a file and put them into a string. The string is acting as the temporary work space I needed. I'm just making a basic line editor. Thanks for giving me a push in the right direction, sometimes I think I just over think this stuff because it is C++ and not Java.
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
Re: Programming Techniques WaltP C Programming Language 0 09-Mar-2004 23:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:52.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.