Reading new lines in a file

This is a discussion on Reading new lines in a file within the C++ Programming forums, part of the General Programming Boards category; For some reason new lines are disregarded when I output the contents of "map.dat". Take a look: Code: #include <iostream> ...

  1. #1
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369

    Reading new lines in a file

    For some reason new lines are disregarded when I output the contents of "map.dat". Take a look:

    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    using namespace std;
    
    int main()
    {
    	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    
    	ofstream fout;
    	ifstream fin;
    
    	char map;
    
    	fout.open("map.dat");
    
    	fout << "!@#\n";
    	fout << "#@!\n";
    
    	fout.close();
    
    	fin.open("map.dat");
    
    	while(fin>>map)
    	{
    		switch(map)
    		{
    		case '!':
    			SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_RED);
    			break;
    		case '@':
    			SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
    			break;
    		case '#':
    			SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_BLUE);
    			break;
    		}
    
    		cout << map;
    	}
    
    	fin.close();
    
    	SetConsoleTextAttribute(hOutput, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    
    	cout << endl;
    
    	return 0;
    }
    Any solutions?
    Last edited by abrege; 12-16-2002 at 06:36 PM.
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    hmm
    very strange...even doing this doesn't help:
    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    using namespace std;
    
    int main()
    {
    	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    
    	ofstream fout;
    	ifstream fin;
    
    	char map;
    
    	fout.open("map.dat");
    
    	fout << "!@#\n";
    	fout << "#@!\n";
    
    	fout.close();
    
    	fin.open("map.dat");
    
    	while(fin>>map)
    	{
    		switch(map)
    		{
    		case '!':
    			SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_RED);
    			break;
    		case '@':
    			SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
    			break;
    		case '#':
    			SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_BLUE);
    			break;
    		case '\n':
    			cout<<endl;
    			break;
    		}
    
    		cout << map;
    	}
    
    	fin.close();
    
    	SetConsoleTextAttribute(hOutput, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    
    	cout << endl;
    
    	return 0;
    }
    PHP and XML
    Let's talk about SAX

  3. #3
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    strange indeed
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    use binary i/o with read and write and see what happens.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    binary i/o? Please explain!
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    [snippet]
    Code:
    ofstream file("file.dat",ios::binary)
    if (!file) {// deal with file cant be opened here}
    //so if we get here file opened
    // lets make an object and save to disk
    MyObject(Param1,Param2,Param3);
    // object made now lets write to disk with member function write
    file.write( reinterpret_cast<const char*>(&MyObject),sizeof(MyObject));
    // The cast just casts the address of our object (MyObject*) to a const char*. sizeof returns the number of bytes in our object and a char is 1 byte. thats why cast needed.
    // now you look up ifstream::read which can read our data back from disk to memory.
    [/snippet]
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    i'm not sure...but i think it has to do with how he's reading in the file, not neccessarily in what format...it has to do with his while loop im' sure of that, i've been able to do a case default and replace the \n characters with another character...then the breaks are spit out...but i couldn't use \b's to overwrite the letters that were spit out.
    PHP and XML
    Let's talk about SAX

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    maybe its because you are using operator >> which leaves newline in stream and discards trailing newline on next read.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    hmm
    lets try to rethink this shall we?
    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    using namespace std;
    
    int main()
    {
    	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    
    	ofstream fout;
    	ifstream fin;
    
    	char map[100];
    
    	fout.open("map.dat");
    
    	fout << "!@#\n";
    	fout << "#@!\n";
    
    	fout.close();
    
    	fin.open("map.dat");
    
    	fin>>map;
                    for (int i=0;i<sizeof(map);i++)
    	{
    		switch(map[i])
    		{
    		case '!':
    			SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_RED);
                                                    cout<<map[i];
    			break;
    		case '@':
    			SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
                                                    cout<<map[i];
    			break;
    		case '#':
    			SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_BLUE);
                                                    cout<<map[i];
    			break;
                                    default:
                                                    cout<<map[i];
                                                    break;
    		}
    
    	
    	}
    
    	fin.close();
    
    	SetConsoleTextAttribute(hOutput, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    
    	cout << endl;
    
    	return 0;
    }
    let me know how that works out for ya, i haven't tested it yet
    Last edited by Waldo2k2; 12-16-2002 at 08:20 PM.
    PHP and XML
    Let's talk about SAX

  10. #10
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Ahh, with that code only the first three characters show up
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    use one of the get() member function variants to read in every char, even white space such as new line char. >> will ignore all whitespace and getline() must have a terminating char of some sort so that char won't appear in map. If you want ALL char in file to appear in map, use a get()

    Code:
    char ch;
    int i = 0;
    //fin>>map;
    fin.get(ch);//read in first char
    while (!fin.eof())//whlie most recent char read in isn't EOF
    {
       map[i++] = ch;//store current char in map
       fin.get(ch);//read in all char one at a time from file
    }
    map[i] = '\0';//make map a string, not just a char array
    
    //beware! sizeof gives the capacity of map, NOT the actual number of char in map!  If map isn't exactly 100 char long you will end up reading an "empty section" of the array with the switch statement.  I would use strlen() instead.
    
    for ( i=0;i<sizeof(map);i++)
    {
       switch(map[i])
    Last edited by elad; 12-16-2002 at 08:50 PM.

  12. #12
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    lol, I got it to work! Thanks!
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Reading file into edit - losing new lines
    By eam in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2003, 12:07 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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