Thread: Problem writing ASCII characters in file

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    1

    Problem writing ASCII characters in file

    Hi,
    I wrote a program to convert decimal numbers 0 -254 to characters and write it in a file. The program reads the characters back and gives the corresponding decimal numbers. But when I write corresponding characters of numbers 10,11,12, and 13, the program reads them back as 204. I cannot figure out how to get 10,11, 12 and 13 back. Any help will be appreciated. Here's the code:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main ()
    {	int x = 11;
    	ofstream outfile ("output.dat", ios::out);
    
        if (!outfile)
    	{	  	
    		exit (1);
    	}
          		 outfile<<( unsigned char)(x);
    		 outfile.close();
    
        int p;
        char ch;
    	unsigned char ch1;
    	ifstream infile;
    
    	infile.open("output.dat");
    
    	if (!infile)
    	{	
    		exit (1);
    	}
    
    	while ((ch= infile.peek()) !=EOF )
    	{
    		infile >> ch1;
    
        p = int (ch1);
    
    		cout<<p<<endl;
    	}
    
    	infile.close();
    	
    	return 0;
    }

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Note that peek() returns an int, not a char.

    Code:
    int ch;
    ch= infile.peek();
    Also
    Code:
    infile >> ch;
    Won't put anything in ch1 if there is a problem with the stream. You could check this by putting a value in ch1 before that line and outputting the value afterwards.

    Code:
    ch1 = '@';
    infile >> ch1;
    cout << "ch1=" << ch1 << endl;

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Four things:
    > ofstream outfile ("output.dat", ios::out);
    Open the file in binary mode:
    Code:
    	ofstream outfile ("output.dat", ios::binary);
    > outfile<<( unsigned char)(x);
    Use put here:
    Code:
          		 outfile.put(x);
    > infile.open("output.dat");

    Again open the file in binary mode:
    Code:
    	infile.open("output.dat", ios::binary);
    > infile >> ch1;
    Use get here:
    Code:
    		ch1 = infile.get();

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Or

    change
    Code:
    infile >> ch1;
    To

    Code:
    infile >> noskipws >> ch1;
    As ASCII characters 10,11, 12 and 13 are whitespace.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    sho1,

    Here's why you have to use binary mode to read/write these values:

    When you read/write a file in text mode (the default mode) the data is changed. Lines of text on the disk are stored differently from the way strings are stored in C++.

    A line of text on a disk is terminated with a carriage return (13) and/or line feed (10) depending on your operating system.

    When you save a string (in text mode) these values are added to the file. When you read a line from a file, these characters are not included in the string.

    With C-style strings, the null-termination is thrown-away and replaced by carriage return/line feed when you save a file (in text mode).

    When you read a line from a file (in text mode) into a C-style string, the carriage return/line feed is replaced with the null-termination.

    I'm not exactly sure why you are having trouble with 11 and 12, except that maybe you are reading the numbers in sequence, and it gets fouled-up as soon as it reads 10.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I'm not exactly sure why you are having trouble with 11 and 12
    Maybe because 11=VT (Vertical Tab) and 12=FF (Form Feed).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Replies: 3
    Last Post: 11-21-2006, 07:26 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM