Thread: lines of a text file

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Thumbs up lines of a text file

    hi, say I had a text file that was read by my program, how would I be able to get it read each line seperately, storing each line in a seperate variable and if the line had nothing in it, the program would ignore that line. Is this possible?

    Thanks
    -Chris

  2. #2
    Unregistered
    Guest
    Offhand, try first counting the lines (count "\n"s). Then use that count with new to create an array with size of number of lines. Read the lines into the array. Check for empty lines before reading the lines into the array, and skip those lines.
    Haven't tried it, but that's an outline, anyway

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    anybody got some code to demonstratre this becuase i am confused about what your talking about

  4. #4
    Unregistered
    Guest

  5. #5
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    i've looked at this already but i don't understand it and when i compile it it doesn't work

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    You would have to use something like fgetc. Check for leading whitespace, nelwines, and end of file characters. This is getting on the edge of having to fully understand the implimentation of a string class.

  7. #7
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    could you please explain this to me?

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        ifstream        file("myfile.txt");    // or whatever
        vector<string>  Lines;
        string          CurrentLine;
    
        while(!file.eof())
        {
            getline(file, CurrentLine);
            if(!CurrentLine.empty())    // store it if not empty
                Lines.push_back(CurrentLine);
        }
    
        // show the stored lines
        for(int i = 0; i < Lines.size(); i++)
            cout << Lines[i] << endl;
    
        file.close();
    
        return 0;
    }
    - lmov

  9. #9
    Registered User WayTooHigh's Avatar
    Join Date
    Aug 2001
    Posts
    101
    or try this...
    Code:
    #include<iostream.h>
    #include<string.h>
    #include<fstream.h>
    
    int main()
    {
    	ifstream infile;
    	
    	char str[255] = "",
    		str2[255] = "";
    
    	cout<<"enter filename\n";
    	cin>>str;
    	cout<<"\n";
    	
    	infile.open(str, ios::nocreate);
    
    	if(infile.fail())
    	{
    		cout<<str<<" not found.";
    
    		return 0;
    	}
    
    	do
    	{
    		infile.getline(str2, 255, '\n');
    		
    		if(strlen(str2) > 0)
    		{
    			cout<<str2<<"\n";
    		}
    	}
    	while(!infile.eof());
    
    	infile.close();
    	
    	return 0;
    }
    Sometimes, the farthest point from the center is the center itself.

    Your life is your canvas, it's only as beautiful as you paint it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Replies: 15
    Last Post: 10-31-2005, 08:29 AM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. How to erase duplicate lines in a text file?
    By miketv in forum C Programming
    Replies: 0
    Last Post: 02-25-2002, 11:18 PM