Thread: Is there a way to skip lines with file input?

  1. #1
    Shadow12345
    Guest

    Is there a way to skip lines with file input?

    How do you skip over spaces while inputting from a text file?
    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <conio.h>
    
    using namespace std;
    
     struct Triangle {
    	float a, b, c, d, e, f, g, h, i;
    };
    vector<Triangle*> TriangleVector;
    
    int main(void) {
    	ifstream fin;
    	fin.open("Coordinates.txt");
    	if(fin.fail()) {
    		cout << "Could not load coordinates" << endl;
    		cout << "Hit a key to escape" << endl;
    		getch();
    		return 0;
    	}
    
    	int x = 0;
    
    	while(!fin.eof()) {
    	TriangleVector.push_back(new Triangle);
    //THIS IS WHERE I NEED HELP WITH SCANNING AHEAD
    	//Scan ahead two spaces, input TriangleVector[x]->a, tab ahead another space, input TriangleVector[x]->b, and so on
    	}
    
    	getch();
    	fin.close();
    		
    	return 0;
    }
    Last edited by Shadow12345; 09-29-2002 at 12:37 PM.

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    This is a function to randomly read a chemical formula for a text file, it should have what you want.

    Code:
    //reads a single random Chemical object from file
    void MoleculeCreator::generateChemical( )
    {
    	ifstream fileIn( "chemlist.txt", ios::in );
    
    	srand( time( 0 ) );		//might be better placed in the constructor
    
    	int ignoredLines = rand( ) % countLines( "chemList.txt" );
    
    	for( int i = 0; i < ignoredLines; i++ )
    	{
    		fileIn.ignore( 80, '\n' );
    	}
    
    	fileIn >> currentChemical;
    
    	fileIn.close( );
    }
    Couldn't think of anything interesting, cool or funny - sorry.

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Here is the countLines function in case you want it, its always handy
    Code:
    int MoleculeCreator::countLines( char* file )
    {
    	ifstream fileIn( file, ios::in );
    	int result = 0;
    
    	while( ! fileIn.eof( ) )
    	{
    		char* buffer = 0;
    		fileIn.getline( buffer, 80,'\n' );
    		++result;
    	}
    	return result;
    }
    Couldn't think of anything interesting, cool or funny - sorry.

  4. #4
    Shadow12345
    Guest
    Ohh so I just ignore it, sweet, now is there a function that just skips to the next line?

  5. #5
    Shadow12345
    Guest
    Oh yeah DUH!!! Of course it will skip that!!! It's declared as a float anyways so it won't input any other crap *blush*

    Exactly how do I skip to the next line? ignore only ignores a space, it doesn't tab to the next line
    Last edited by Shadow12345; 09-29-2002 at 12:46 PM.

  6. #6
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    should do, depends if you've got other stuff in the file. ignore( ) works great for stuff like this, just ignore up to the '\n' and you should be reading from the next line with your next fin operation.
    Couldn't think of anything interesting, cool or funny - sorry.

  7. #7
    Shadow12345
    Guest
    Hmm this code almost works except that it isn't read the variables correct.
    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <conio.h>
    
    using namespace std;
    
     struct Triangle {
    	float a, b, c, d, e, f, g, h, i;
    };
    vector<Triangle*> TriangleVector;
    
    int main(void) {
    	ofstream fout;
    	ifstream fin;
    	fout.open("Formatted coords.txt");
    	fin.open("Coordinates.txt");
    	if(fout.fail()) {
    		cout << "Could not load coordinates" << endl;
    		cout << "Hit a key to escape" << endl;
    		getch();
    		return 0;
    	}
    
    	if(fin.fail()) {
    		cout << "Could not load coordinates" << endl;
    		cout << "Hit a key to escape" << endl;
    		getch();
    		return 0;
    	}
    
    	int x = 0;
    
    	while(!fin.eof()) {
    	TriangleVector.push_back(new Triangle);
    	fin >> TriangleVector[x]->a;
    	fin >> TriangleVector[x]->b;
    	fin >> TriangleVector[x]->c;
    	fin >> TriangleVector[x]->d;
    	fin >> TriangleVector[x]->e;
    	fin >> TriangleVector[x]->f;
    	fin >> TriangleVector[x]->g;
    	fin >> TriangleVector[x]->h;
    	fin >> TriangleVector[x]->i;
    
    	fout << TriangleVector[x]->a;
    	fout << TriangleVector[x]->b;
    	fout << TriangleVector[x]->c;
    	fout << TriangleVector[x]->d;
    	fout << TriangleVector[x]->e;
    	fout << TriangleVector[x]->f;
    	fout << TriangleVector[x]->g;
    	fout << TriangleVector[x]->h;
    	fout << TriangleVector[x]->i;
    
    	cout << TriangleVector[x]->a << endl;
    	cout << TriangleVector[x]->b << endl;
    	cout << TriangleVector[x]->c << endl;
    	cout << TriangleVector[x]->d << endl;
    	cout << TriangleVector[x]->e << endl;
    	cout << TriangleVector[x]->f << endl;
    	cout << TriangleVector[x]->g << endl;
    	cout << TriangleVector[x]->h << endl;
    	cout << TriangleVector[x]->i << endl;
    	fin.ignore('\n');
    	getch();
    	x++;
    
    	}
    
    	getch();
    	fin.close();
    	fout.close();
    	cout << "Hit a key to escape" << endl;
    	getch();
    		
    	return 0;
    }
    I wasn't sure what to put for the first argument for fin.ignore(), and it has a default value of 1 so i just omitted it, what exactly is the first argument for?
    Last edited by Shadow12345; 09-29-2002 at 12:59 PM.

  8. #8
    Shadow12345
    Guest
    Here is the entire project folder with all of the necessary files(under 10KB download)

    It seems as though you have to skip the lines, it just outputted jibberish until I had it ignore lines
    Last edited by Shadow12345; 09-29-2002 at 04:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM