Thread: Regarding fstream

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    Regarding fstream

    CR - carriage return
    LF - line feed
    In "cpp.properties", any new line is set as CRLF, but when the text document has been written to "bbb.txt", CRCRLF appears in "bbb.txt" instead of CRLF. Can anyone please tell me where the problem lies in?


    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    
    typedef unsigned short int usint;
    
    int main(usint arg, char *parameter[]) {
    	if (arg == 2) {
    		using namespace std;
    		ifstream::pos_type size;
    		char *memblock;
    		const char *convert;
    		string search = "command.build.*.cpp=M:/Dev-Cpp/bin/g++.exe -o \"$(FileName)\" \"$(FileName).$(FileExt)\"";
    		string link_file = " -Xlinker ";
    		link_file += parameter[1];
    		string filebuffer;
    		
    		ifstream readfile ("M:/SciTE/cpp.properties", ios::in|ios::binary|ios::ate);
    		if (readfile.is_open()) {
    			size = readfile.tellg();
    			memblock = new char [size];
    			readfile.seekg (0, ios::beg);
    			readfile.read (memblock, size);
    			readfile.close();
    			filebuffer = memblock;
    			delete []memblock;
    			
    			filebuffer.insert((filebuffer.find(search, 0) + 93), link_file);
    			ofstream writefile("M:/bbb.txt");
    			if (writefile.is_open()) {
    				writefile << filebuffer;
    				writefile.close();
    				system("M:/bbb.txt");
    			}
    			else {
    				MessageBox (NULL, "Unable to WRITE file.", "ERROR", MB_OK);
    				goto end;
    			}
    			
    		}
    		else {
    			MessageBox (NULL, "Unable to READ file.", "ERROR", MB_OK);
    			goto end;
    		}
    		
    		
    		end: return 0;
    	}
    }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You're opening the file in binary mode for reading (meaning that CRLF will be read as CRLF) and in text mode for writing (meaning that CR will stay CR and LF will become CRLF, thus CRLF becomes CRCRLF).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    You're opening the file in binary mode for reading (meaning that CRLF will be read as CRLF) and in text mode for writing (meaning that CR will stay CR and LF will become CRLF, thus CRLF becomes CRCRLF).
    May I know where the extra CR came from?

    Sorry, but I'm rather new to programming.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In text mode, iostreams convert the newline character (the linefeed in this case) to the appropriate combination of characters for the specific platform. On Windows, CRLF is used as the end of line character, so the linefeed is converted to a carriage return/linefeed combination. That is of course written out after the first carriage return that you output, making two carriage returns.

    If you read in binary and write out in text mode, you'll have to convert carriage return linefeed pairs to single linefeeds yourself.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Quote Originally Posted by Daved
    In text mode, iostreams convert the newline character (the linefeed in this case) to the appropriate combination of characters for the specific platform. On Windows, CRLF is used as the end of line character, so the linefeed is converted to a carriage return/linefeed combination. That is of course written out after the first carriage return that you output, making two carriage returns.

    If you read in binary and write out in text mode, you'll have to convert carriage return linefeed pairs to single linefeeds yourself.
    Yes, I understand that Windows uses CRLF as the end of line character, but may I know exactly which part of the code that does that?

    -----------

    If anyone have any method that you feel that it's the most efficient manner of making this code possible, please do enlighten me on how to do so ^^

    Yes. I could use char all the way, but if you were to build this code, how would you get it done? I would like to know your method if you don't mind.

    Thank you for any replies in advance and to the previous people who posted ^^
    Last edited by genyue; 01-03-2006 at 12:20 AM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> may I know exactly which part of the code that does that?
    Somewhere in the fstream code that implements operator<< is where "\n" is converted to "\r\n".

    If you must read in binary and write out in text, you could just use the remove algorithm to remove all '\r' occurrences from filebuffer before outputting it.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    Post

    Code:
    ofstream writefile("M:/bbb.txt");
    if (writefile.is_open()) {
    writefile << filebuffer;
    writefile.close();
    system("M:/bbb.txt");
    }
    If its the operator<< that added CR to CRLF, then removing CR wouldn't work, ya? Since the removing of CR is to be implemented before using the operator<<.

    Yes. Removing CR from the string filebuffer will do the trick. Though it does the trick, does anyone know exactly which part that adds CR to every new line?

    I'm really sorry to bother you guys, but I really wish to know exactly which part of the code add in an extra CR.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    writefile << filebuffer;
    This line will add carriage returns as it goes along. But the real culprit is this line:
    Code:
    ofstream writefile("M:/bbb.txt");
    This line sets the stream in a state where it will add the carriage returns.

    Or, in my opinion, the real culprit is actually this line:
    Code:
    ifstream readfile ("M:/SciTE/cpp.properties", ios::in|ios::binary|ios::ate);
    because that's what prevents the stream from converting it back to a single newline on reading. Remove ios::binary:
    Code:
    ifstream readfile ("M:/SciTE/cpp.properties", ios::in|ios::ate);
    Now the only problem remaining is that you can't reliably use tellg on a stream in text mode. This problem is not, however, solved by opening it in binary mode. The file IS a text file, and you can't change that, since it's not your file (apparently it belongs to SciTE). Which means you'll have to find another way to work through the file. (You could read it line by line using fgets, for example.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Quote Originally Posted by CornedBee
    Now the only problem remaining is that you can't reliably use tellg on a stream in text mode. This problem is not, however, solved by opening it in binary mode. The file IS a text file, and you can't change that, since it's not your file (apparently it belongs to SciTE). Which means you'll have to find another way to work through the file. (You could read it line by line using fgets, for example.)
    Let's say the file has a 10 MB filesize. Using ifstream or fgets will be faster in your opinion?

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, probably not faster. But correct, which is more important.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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, 10: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, 12:21 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM