Read SPECIFIC line from text file

This is a discussion on Read SPECIFIC line from text file within the C++ Programming forums, part of the General Programming Boards category; hi, i have JUST started cpp and im working on something. i want my prog to read a specific line ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    3

    Read SPECIFIC line from text file

    hi, i have JUST started cpp and im working on something. i want my prog to read a specific line from a text file and then store that to an array. for example:
    Code:
    #include <fstream.h>
    #include <iostream.h>
    
    int main();
    {
    char username1[20];
    char username2[20];
    char username3[20];
    char username4[20];
    ofstream savefile ("users.txt");
    ifstream OpenFile("users.txt");
    //this is what i dont know what to do, i want it to read each line 
    //of users.txt and store each line to its own variable
    //i.e store line 1 of users.txt to username1 and so on.
    any help is apprieciated

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    OpenFile.getline(username1, 20);
    OpenFile.getline(username2, 20);
    OpenFile.getline(username3, 20);
    OpenFile.getline(username4, 20);
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    3
    but the lines it reads arent necesarily going to be 20 chars. i wil be running the variables throu an ifstatement so i dont want blank character in the variable. e.g

    text file:
    username
    username2

    variables read:
    'username '
    etc,

    i only want to read the username on line one of the text file and not specify 20 chars because then it reads the next line aswell and therefor the second line is also in the variable

    that probably doesnt make alot of sense, if it does then please reply.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    Code:
    #include <string>
    using std::string;
    using std::getline;
    ...
    string username1;
    getline(OpenFile, username1);

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but the lines it reads arent necesarily going to be 20 chars
    They all should be less than 20 or the code you showed me is wrong. getline doesn't read exactly the amount you give it, it reads at most that amount - 1 for the null character and stops reading at a newline. Generally, if getline fills the buffer then your code is wrong.

    >and not specify 20 chars because then it reads the next line aswell
    No, it doesn't. Did you try the code I gave you or just assume that it reads 20 characters?
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    3
    i assumed

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. Position FILE pointer to a specified line of Text file
    By jessica_xie in forum C Programming
    Replies: 2
    Last Post: 02-04-2005, 02:52 PM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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