I/O Question

This is a discussion on I/O Question within the C++ Programming forums, part of the General Programming Boards category; Lets say i have a file with 20 lines... How do i read just line 14 or any other line ...

  1. #1
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483

    I/O Question

    Lets say i have a file with 20 lines...
    How do i read just line 14 or any other line i want alone with out reading the rest?

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    You read one line at a time. Use a counter if you want a specific number of lines to read.

    Your question seems quite vague because the answer is too simple. How are you reading from the file?

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    if you know the length you can use

    http://www.cplusplus.com/ref/iostrea...eam/seekg.html


    i would jsut read in one line at a time, possibly store each string
    into a vector just incase im going to end up using all the
    lines, and simply call the spot in the vector for the line i wanted

    ex
    Code:
    vector<string> vS;
    string S;
    ifstream file("myfile.txt");
    while(!file.fail())
    {
          getline(file, S, '\n');
          vS.push_back(S);
    }
    file.close();
    cout << vS[13] << endl; //for line 14 of the file
    then again im kind of parcial to vectors, hense the name :/

    of course these will require :

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    Last edited by ILoveVectors; 06-24-2005 at 10:41 AM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Your example code will add an extra entry to the vector you don't want. Use while (getline(file, S, '\n')) instead.

  5. #5
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    lets say i have teh number 123 stored in a string is there a way to put into int?

  6. #6
    Banned
    Join Date
    Jun 2005
    Posts
    594
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    If you mean actual string and not character array,
    if you do mean character array look in the faq, its there
    somewhere to.

  7. #7
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about file I/O.
    By The7thCrest in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2009, 04:15 PM
  2. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-12-2007, 11:48 PM
  3. Question regarding File I/O
    By mhenderson in forum C Programming
    Replies: 4
    Last Post: 08-03-2006, 12:46 PM
  4. File I/O Question
    By 182 in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2005, 02:03 PM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 04:14 PM

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