Read space from the file

This is a discussion on Read space from the file within the C++ Programming forums, part of the General Programming Boards category; I'm trying to read a file and convert them into ASCII value. This code can read and print all the ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    12

    Read space from the file

    I'm trying to read a file and convert them into ASCII value. This code can read and print all the symbol except the space. Guess when i read the file, it miss out the space already. Anybody know how to read the space in the file?

    Code:
    int main ()
    {
    	char ch;
    	ifstream infile;
    	infile.open("testing.txt");
    	
    	while(infile >> ch) 
    	{
    		int asciiValue = (int)ch; 
    		cout << asciiValue << " ";
    	}
    
    	infile.close( );
    	return 0;
    }
    lots of help
    ken js

  2. #2
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    The >> is text oriented, i.e., it reads until it finds a space or a newline character. Look for the read function, it might be handy.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    Or getline(), which reads a whole line of text.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    The only problem with getline() in a case like this is that after it reads all the data, it will drop the newline character, and he might want to display the ascii value of that as well. If not, then he is good to go with getline().
    My Website

    "Circular logic is good because it is."

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    Code:
    #include <string>   //for std::string
    #include <algorithm>  //for std::for_each
    
    struct print_as_int
    {
        void operator() (const char & ch) { std::cout << (static_cast<int>(ch)) << ' '; }
    };
    
    int main ()
    {
    	char ch;
    	ifstream infile;
    	infile.open("testing.txt");
    	
            std::string buf;
    	while(std::getline(infile, buf)) 
    	{
    		std::for_each(buf.begin(), buf.end(), print_as_int());
    	}
    
    	infile.close( );
    	return 0;
    }
    *edit* Ah, DavidP makes a good point. *edit*
    Code:
    // . . . .
    print_as_int func_ob; 
    std::for_each(buf.begin(), buf.end(), func_ob);
    func_ob('\n');
    //. . . . .
    Except for one extra '\n' at the end, I think that's the idea.
    Last edited by CodeMonkey; 08-05-2007 at 02:28 AM. Reason: typo
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    12
    i need to extract each individual character/ symbol because later part i need to count them.

    My method is to extract each individual character and convert them into ascii value, so that I can use the ascii value to count each individual character occurred how many times.

    If I use the getline, I only can convert them into ascii value all at a time but cannot count each individual character.

    lot of help
    ken js

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    Well, counting occurrences is one of the beauties of std::map.
    Code:
    std::map<char, unsigned long int> table;
    char ch;
    while(infile.get(ch))   ++(table[ch]);
    
    std::cout << "s occurred " << table['s'] << " times." << std::endl;
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    12
    Thanks for your help guys.

    I found a easlier way but there still got a small problem.

    My testing.txt file contain "0 1 a b A B".
    My output is "48 32 49 23 97 32 98 32 65 32 66 -1"

    anybody know what is the "-1"???
    how do i take away the "-1?"

    Code:
    while(!infile.eof()) 
    {
    	ch = infile.get();
    	int asciiValue = (int)ch; 
    	cout << asciiValue << " ";
    }

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    > while(!infile.eof())
    See the FAQ on why using EOF or eof() in a loop is bad - for the very reason you're describing.

    Perhaps
    while ( infile.get(ch) )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    May 2007
    Posts
    12

    Smile

    Thanks salem.

    My problem had been sloved=)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. someone who is good at finding and fixing bugs?
    By elfjuice in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2002, 04:59 PM

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