Thread: parsing text using getline

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    30

    parsing text using getline

    I've got this code which parses a text file by reading through and assigning each individual string to a variable (an element in the 'holder' array). It is based mostly off of some code I found on youtube for reading text files on the newboston.com channel.

    How could I assign an entire line to a variable as I read a text file?

    I know that I need to use getline, but I can't figure out how to get it to work. Never programmed in C++ before, nor have I programmed much in general.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        string holder[200];
        int i = 0;
        char filename[50];
        char word[50];
        ifstream inputobject;
        
        cout << "filename equals: ";
        
        cin.getline(filename, 50);
        inputobject.open(filename);
        inputobject >> word;
        holder[i] = word;
    
        while(inputobject.good()){
             holder[i] = word;
             inputobject >> word;
             i++;
        }
        
    system("pause");
    return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well..., that's what you do:
    Code:
    cin.getline(filename, 50);
    gets one line of data (assuming it's no more than 50 characters -- you can put a much larger value here) and puts it in the variable filename.

    Looks like we're done here.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Let's fix that code, so that you don't fall into the "C+" darkness, as well:
    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    
    int main()
    {
    	std::vector<std::string> holder;
    	std::string filename, word;
    
    	std::cout << "filename equals: ";
    	std::getline(filename, std::cin);
    
    	std::ifstream inputobject(filename.c_str());
    	inputobject >> word;
    	holder.push_back(word);
    
    	while(inputobject.good())
    	{
    		holder.push_back(word);
    		inputobject >> word;
    	}
    
    	return 0;
    }
    This fixes the code to be real C++ code and not C-mix.
    I cannot speak for any bugs in the code, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    	inputobject >> word;
    	//holder.push_back(word);
    
    	while(inputobject.good())
    	{
    		holder.push_back(word);
    		inputobject >> word;
    	}
    The whole point of testing the stream is so you won't push data from a failed read into holder.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help parsing text file
    By dudeomanodude in forum C++ Programming
    Replies: 7
    Last Post: 07-16-2008, 10:21 AM
  2. Parsing Text
    By flaran in forum C++ Programming
    Replies: 13
    Last Post: 10-19-2005, 12:08 PM
  3. Parsing specific data from one text file to another..
    By chops11 in forum C Programming
    Replies: 2
    Last Post: 09-13-2005, 10:50 AM
  4. Replies: 1
    Last Post: 07-13-2002, 05:45 PM
  5. 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