ignore()

This is a discussion on ignore() within the C++ Programming forums, part of the General Programming Boards category; Code: //File i/o.. //Practice22.cpp #include <iostream> #include <fstream> using namespace std; int main() { char line[100]; fstream File("map.tbw",ios::in|ios::out); File.ignore(10000,'2'); File.getline(line,100,'\n'); ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    ignore()

    Code:
    //File i/o..
    //Practice22.cpp
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	char line[100]; 
    	fstream File("map.tbw",ios::in|ios::out);
    
    	File.ignore(10000,'2');
    
    	File.getline(line,100,'\n');
    
    	cout<<line<<endl;
    
    	File.ignore(10000,'5');
    
    	File.getline(line,100,'\n');
    
    	cout<<line<<endl;
    
    	File.close();
    
    	return 0;
    }
    
    -----------"map.tbw"------------
    
    g g g g g g g g g g g g g g g g g g g g g g g d w w w w w w w w w w w w w w w w 
    g g g g g g g g g g g g g g g g g g g g g g g g d d w w w w w w w w w w w w w w 
    g g g g g g g g g g g g g g g g g g g g g g g g g d d d d w w w w w w w w w w w
    g g g g g g g g g g g g g g g g g g g g g g g g g g d t t d d w w w w w w w w w
    g g g g g g g g g g g g g g g g g g g g g g g g g g g t t d d d w w w w w w w w 
    g g g g g g g g g g g g g g g g g g g g g g g g g g t g t d d d w w w w w w w w
    g g g g g g g g g g g g g g g g g g g g g g g g g t t t g t d d d d w w w w w w 
    g g g g g g g g g g g g g g g g g g g g g g g g g t t t t t g g d d w w w w w w	
    g g g g g g g t g g g g g g g g g g g g g g g g g t t t t t g g g g g d w w w w
    g g g g g g g g t g g g g g g g g g g g g g g g g t t t t g g g g g g g d d w w
    g g g g t g g g g g g t g g g g g g g g g g g g g t t t g g g g g g g g g g d w
    g g g g g g g g g g g g g g g g g g g g g g g g t t t g g g g g g g g g g g g w
    g g g g g g g g g g t g g g g g g g g g g g g g t g g g g g g g g g g g g g g w
    g g g g g g g g g g g g g g g g g g g g g g g g g g g g g g g g g g g g g d w w
    g g g g g g g g t g g g g g g g g g g g g g g g g g g g g g g g g g g g d w w w
    g g g g t g g g g g g g g g g g g g g g g g g g g g g g g g g g g g g d w w w w 
    g g g g g g g g g g o o o o g g g g g o o o o g g g g g g g g g g g d w w w w w
    g g g g g g t g t g o o o g g g g g g g o o o g g g g g g g g g g g w w d w w w 
    g g g g g g g g g g o o o g g g g g g g o o o g g g g g g g g g d w w d d w w w
    g g g g g g g g g g o o o g g g g g g g o o o g g g g g g g g d w w w w w w d w
    
    
    1Fiorey
    2Hasina
    3Ilobar
    4Celeste
    5Ghaal

    is this an acceptable way to pull things from a file?
    or is there some better more effective way?
    MSVC++~

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    What's with all the ignores? I don't see a need. Also, you're only reading from the file... why not use an ifstream?
    I perfer a string object over a char * buffer.

    This is what i'd do:

    Code:
    int main()
    {
    	string line; 
    	ifstream File("map.tbw",ios::in);
    
    	while (getline(File,line))
    		cout<<line<<endl;
    
    	File.close();
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    uhm the ignores are so i can bypass all the things i dont need...and just pull the 2 names from both number 2 and number 5...try out the code...put all of the stuff at the bottom in a txt file...and try it -_-
    MSVC++~

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    hey thx...yea that is exactly what i had figured...because most indefinately the project that im attempting...will have rather large files...definately larger then 10000...
    MSVC++~

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    >> ifstream File("map.tbw",ios::in);

    If you specify an ifstream object, it's all ready for reading from a file no need for the ios::in

    Code:
    ifstream File("map.tbw");
    -Futura
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    If you specify an ifstream object, it's all ready for reading from a file no need for the ios::in
    yea but if you check my code its made for writing and reading...
    so i dont have to define 2 different fstreams...just one -_-

    Code:
    fstream File("map.tbw",ios::in|ios::out);
    MSVC++~

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Originally posted by Supar
    yea but if you check my code its made for writing and reading...
    so i dont have to define 2 different fstreams...just one -_-

    Code:
    fstream File("map.tbw",ios::in|ios::out);
    I was directing my comment at Eibro's code, which I quoted:
    >> ifstream File("map.tbw",ios::in);

    He asked, "why not just use ifstream", and then, (unnecessarily) specified ios::in

    -Futura

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    oh sorry...my bad ;P
    MSVC++~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is (ignore) valid code?
    By Bassquake in forum C Programming
    Replies: 7
    Last Post: 06-10-2008, 09:16 AM
  2. question about ignore()????
    By newbie02 in forum C++ Programming
    Replies: 4
    Last Post: 08-11-2003, 09:27 AM
  3. ignore functions
    By cheez in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-02-2002, 11:54 AM

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