getline function

This is a discussion on getline function within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make a program that reads a file (manfile.txt) and then displays to screen certain lines. The below ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    44

    getline function

    I'm trying to make a program that reads a file (manfile.txt) and then displays to screen certain lines.

    The below example doesn't seem to work, but i'm trying to get the program to display the 10th line of the file, any ideas why it doesn't seem to work?

    Also I have a few query's by the side of the code.

    void Main()
    {
    const char * managerfile = "c:/manfile.txt";
    clrscr();
    char line[100]; //is the number in square brackets to total number of lines the program reads??
    ifstream managerfile (managerfile, ios::in);
    managerfile.getline( line, 10 , '\n' ); //not 100% sure if the code in the brackets is correct.
    cout << line;
    getch();
    managerfile.close(); //is this correct and necessary??
    }
    I'm using Bloodsheds Dev-C++ Compiler.

    JamMan..

    Curious if I can live forever? CLICK HERE

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    
    
    void main() 
    { 
    char filename[50];
    unsigned char info[255];
    
    
    cout << "Enter file name: ";
    cin >> filename;
    
    ifstream fin(filename);
    while(fin != NULL){
    for(int x = 0; x < 10 ;x++){
    	fin.getline(info,255,'\n'); }
    if(info == NULL)cout << "Error";
    else cout << info; }
    fin.close(); 
    
    }
    Try that for size... I think it works ok...

    What I have done is read a line that ends in '\n' (enter) 9 times. I then do it again nd spit out the result.... hope it helps....

  3. #3
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    managerfile.close(); //is this correct and necessary??
    Not unless you're opening another file with the managerfile object, the ofstream destructor will close the file.
    zen

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    30

    wrong syntax?

    const char * managerfile = "c:/manfile.txt";
    shouldn't this be "c:\\manfile.txt"; ?
    Homer

    D'OH!

    mmmmmmmm... iterations

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You can normally use either.
    zen

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    44

    Question ???????

    Cheers but the program Fordy gave me doesn't seem to work, I put a getch(); at the end but the screen just flashes up and then disappears??

    Could someone be so kind as to explain what every line of the program does as I'm not 100% sure.

    What I would like the program to do is, from a text file is display the lines to screen 10 at a time, depending on what the user inputs.
    For example is the user inputs 1 it displays lines 1-10, inputs 2 and 11-20, ....

    Thanks in advance JamMan..
    I'm using Bloodsheds Dev-C++ Compiler.

    JamMan..

    Curious if I can live forever? CLICK HERE

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    I got rid of the getch func cuz I was just using C++ stream IO.....

    To work it, open it at the command prompt.

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    44
    Sorry.. You lost me, Open it at the Command Prompt??
    I'm using Bloodsheds Dev-C++ Compiler.

    JamMan..

    Curious if I can live forever? CLICK HERE

  9. #9
    Unregistered
    Guest
    The command prompt refers to DOS programming where the screen is often blank and there is a flashing C:> at the left hand side of the screen. The flashing C:> is often called the c prompt and the flashing > is often called command prompt because they are flashing indicating you need to do something here like put in a command to tell the computer what you want it to do. If you use a console you may not see a command prompt.

    If you are going to allow user to display various segments of a file then you need to adjust the basic code loop so it reads all the lines but only displays the ones you want.

    int choice;
    int i = 0;
    char input[255];

    //open a stream to read your file
    ifstream fin("yourfile.txt");

    //find out which group of 10 lines the user wants to view
    cout << "if you want to see first 10 lines of file enter 1 " << endl;
    cout << "if you want to see second 10 lines of file enter 2 " << end;
    cout << "etc." << endl;
    cin >> choice;

    //read the entire file
    while(fin)
    {
    //one line at a time
    fin.getline(input, 255, '\n');

    //display only this group of 10 lines and ignore the rest;
    if(i >= ((choice - 1) * 10) && i < (10 + ((choice - 1) * 10)))
    {
    cout << input << endl;
    }

    //use this variable to keep track of which line you are on
    i++;
    }

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    44
    Cheers people!! I'm almost there to understanding this..

    Originally posted by Unregistered


    ...

    char input[255];
    ...
    fin.getline(input, 255, '\n');
    ...
    I'm unsure about the two above lins of code. What is the nmber 255 representing??

    Thanks JamMan..
    I'm using Bloodsheds Dev-C++ Compiler.

    JamMan..

    Curious if I can live forever? CLICK HERE

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    255 was an arbitry number.......could have been 256, 254 123456 etc............Its just ti get a reasonally big buffer.


    The getline() is getting up to 255 char or how many exist before '\n' (this is what's produced when you press the enter key on a text file editor or whatever...) and storing it in input which is a char array....

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    44

    Thumbs up Cheers for all the replies!!!

    Now I'm trying to get the program to display the lines of the text file on different xy positions.


    Code:
    if (i = (choice) * 10)       
    {
    gotoxy(15,3);
    cout << input << endl;
    }
    It only works when choice is 1, can anyone tell me where i'm going wrong??
    I'm using Bloodsheds Dev-C++ Compiler.

    JamMan..

    Curious if I can live forever? CLICK HERE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. getline function for ansi c
    By crash88 in forum C Programming
    Replies: 3
    Last Post: 05-07-2006, 05:29 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. getline function to read 1 line from a text file
    By kes103 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 06:21 PM

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