getline() help

This is a discussion on getline() help within the C++ Programming forums, part of the General Programming Boards category; ok guys ive got the output and input all down well to a degree, but my problem now is that ...

  1. #1
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52

    Exclamation getline() help

    ok guys ive got the output and input all down well to a degree, but my problem now is that when im trying to use getline and fstream to bring in from the file im getting the first full line, and i know thats what getline is saposed to do and all, but now im just wondering if theres a way to bring the entire file or change getline so it brings in line 2, then 3 so on. anyways thanks guys

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Post some code and let us see what you've got. Letting us tear it apart is a lot easier than having us guess at what you've written and give you suggestions that may or may not be helpful.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    > give you suggestions that may or may not be helpful

    Like this one: use getline in a loop and just add to the data each time.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1) You need to work on your terminology:

    "...im trying to use getline and fstream to bring in from the file..."

    It's known as 'reading' from the file.

    "..im just wondering if theres a way to bring the entire file..."

    In that case, you would say: "I want to 'read in' the whole file.

    2) getline() has a third parameter, which is called a 'delimiter'. The default delimiter is '\n', which is an invisible character at the end of the lines in your file that is put there when you hit the Enter key. You can change that delimiter to anything you want:

    getline(inFile, str, '#');

    In that case, getline will read from your input file until it encounters a '#' character. You can either put one of those at the end of the file(or omit it altogether) and getline will read in your whole file.

    3)
    bring the entire file or change getline so it brings in line 2, then 3 so on
    You can use the getline() statement you are currently using and create a loop to read in one line at a time until you hit the end of the file. You can detect the end of the file like this:

    while(! inFile.eof())

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
       string filename = "filename.txt";
    
       ifstream in( filename.c_str() );
       if ( !in.is_open() )
       {
          cout << "Unable to open file: " << filename;
          return 1;
       }
    
       string line;
       while ( getline ( in, line ) )
       {
          //Process line
       }
    }

  6. #6
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52
    ok, im still stuck i wanna use the this code to set the delimiter at the end of my file

    Code:
    getline(inFile, str, '#');
    i think it would be the fastest way to get the results im looking for

    ok so basicly this is ALL i want my program to do right now

    1.open a .txt file on c:
    2.enter in my full name and address on two different lines
    3.read in all text on the file that it just made

    and like he said im trying to set the delimiter to "#" so that i can slap that on the end of any text file and getline will be able to read the entire file in..

    so if you guys can help me out on this or how to use that line of code right that would help alot!

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Post what you have. If you are writing and reading to your file in the same program, you have to remember to close() the file after you write to it.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by AndyBomstad
    now im just wondering if theres a way to bring the entire file
    I posted something in this post that reads the entire file into either a string or stringstream container.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52
    ok heres what im writing

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    char file[100];
    char name[50];
    char addr[50];
    
    void main()
    {
        ofstream fileout;
        ifstream filein;
        fileout.open("h:/text.txt",ios::app);
        cin.getline(name,sizeof(name));
        cin.getline(addr,sizeof(addr));
        fileout<<name<<endl;
        fileout<<addr<<endl;
        fileout<<"#";
                    fileout.close();
        filein.open("h:/text.txt",ios::app);
        //LOST AFTER HERE
        
    }
    now at the end i want to use the delimiter thing he was talking about so that it will read in all that the file has in it up to the #

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1)sizeof(name) will give you the number of bytes for the whole array. Since each element will be occupy several bytes, the total number of bytes is not the same as the number of elements. Typically whats done is to divide the total number of bytes by the number of bytes for one element:

    sizeof(name)/sizeof(name[0])

    EDIT***
    Sorry, the way you used sizeof() will give you the number of elements for a char array.

    2)//LOST AFTER HERE
    Create a variable to hold the data. A string variable is an easy one to use since it doesn't require that you list a size. Then use filein to read data into the variable:

    filein>>

    EDIT***
    That will read one word into the file, so if you want to read in the whole file, you can use a form of getline() for strings. If you were getting input from the console you would do this:

    string str;
    getline(cin, str, '#');

    Since you want to get your input from the file, just substitute filein for cin.
    Last edited by 7stud; 02-17-2005 at 01:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 10:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 01:59 PM

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