Thread: reading file from second line

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    reading file from second line

    hey guys I searched for this in the previous posts but couldn't find much. How would I make the program read in a file starting from the second line?

    basically, the first line in the file tells me how many lines there will be two input. I read this in and make an array of that size. Now I want to read in the rest of the file into that array starting from the second line.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How would I make the program read in a file starting from the second line?
    Use getline to read the first line perhaps? Then you'll continue to read starting from the second line.
    My best code is written with the delete key.

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Exactly. Once you read the first line, the file pointer will be positioned at the start of the second line... It sounds like you don't have too much experience with handling files or are perhaps overthinking it, but basically any time you do a read or write, the file pointer is moved and stays put till the next access. A quick and dirty example:
    Code:
    #include <fstream>
    using namespace std;
    
    static const int MAX_LEN = 200;
    
    int main() {
      ifstream fin("filename.goes.here.txt");
      char line[MAX_LEN];
      fin.getline(line, MAX_LEN);
      
      char **detail = new char*[atoi(line)];
    
      int index = 0;  
      while (fin.getline(line, MAX_LEN).good()) {
        detail[index] = new char[strlen(line) + 1];
        strcpy(detail[index], line);
        
        //...
        
        delete [] detail[index];
      }
      
      delete [] detail;
      
      return 0;
    }

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Thanks guys, I figured out a bit different way to do it....now I have another problem...the file has character as well as numbers. The numbers could be anything from ints to floats. I only want to pick out these as I'm reading the file in. I know how to code it in C but I'm not sure how the c++ code would look like.

    my c version:
    Code:
    int main(int argc, char *argv[])
    {
      int line, con1, con2, i;
      char buffer[100];
    
      if (argc != 2) 
      {
        cout << "Commandline Error!"; 
        exit (1);
      }
      else 
      {
        ifstream examplefile(argv[1]); //open a file indicated by the single command
                                       //line argument
        if (! examplefile.is_open())
        { 
           cout << "Error opening file"; exit (1);
        } 
        examplefile.getline (buffer,100);
        sscanf(buffer,"%d", &line);
        cout << line << endl;
        for (i=1; i<=line; i++)
        {
           examplefile.getline (buffer,100);
           sscanf(buffer, "%d, %d", &con1, &con2);
           cout << con1 << "," << con2 << endl;
        }
        return 0;
      }    
    }
    this reads decimals only thought.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    i think I almost figured it out...but now another problem arises. When stepping through the file looking for the ints the loop counter goes up...when I do find an int I want the array to start at 0, if you get my meaning.

    SORRY the cde is awfully dirty.
    Code:
    ifstream inFile;
    		ofstream outFile;
    		string inFileName, outFileName;
    		int numberOfBuildings;
    		float descriptions;
    		char tmp;
    		int i = 0;
    		int j = 0;
    		
    		inFile.open("hey.txt");
            inFile>>numberOfBuildings;
    
    		cout << numberOfBuildings << endl;
    
    		int *B = new int[numberOfBuildings*3];
    		for( i = 0; i < (numberOfBuildings*3); i++ ){
    			inFile>>tmp;
    			if( isdigit(tmp) ) {
    				B[i] = (int)tmp;
    				continue;
    			}
    		}
    EDIT:: the multiplication by three is because my input is in the following way (1, 2, 3) (4, 5, 6)....so if there are 3 more lines following i will have a total of 3*3 inputs.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    MAN AM I RUSTY!! I'm taking a data structures and discrete mathematics class, and this is our first prog of the semester....I really forgot how to do the syntax, yet I could write a any kind of search you would like....lol. As they say practice makes perfect.


    as I was posting my last message it hit me....here is what I did, still dirty thought.
    Code:
    ifstream inFile;
    		ofstream outFile;
    		string inFileName, outFileName;
    		int numberOfBuildings;
    		float descriptions;
    		char tmp[1];
    		int i = 0;
    		int j = 0;
    		
    		inFile.open("hey.txt");
            inFile>>numberOfBuildings;
    
    		cout << numberOfBuildings << endl;
    
    		int *B = new int[numberOfBuildings*3];
    		for( i = 0; i < (numberOfBuildings*7); i++ ){
    			inFile>>tmp[0];
    			if( isdigit(tmp[0]) ) {
    				cout << tmp[0] << " :: " << endl;
    				B[j] = atoi(tmp);
    				j++;
    			}
    		}
    				
    		
    
    		for(  i = 0; i < numberOfBuildings*3; i++ ){
    			cout << B[i] << endl;
    		}

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

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. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. reading file line by line
    By ameet in forum C Programming
    Replies: 7
    Last Post: 09-16-2004, 10:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM