Thread: I/O Question

  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,366
    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, 05:15 PM
  2. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-13-2007, 12:48 AM
  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, 03:03 PM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM