Thread: Splitting a .txt file by line

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    1

    Splitting a .txt file by line

    Hello

    I'm not very familiar with C++, but I am trying to make a quick program for a very specific task. I know its not usually good to ask for a plug and chug solution, but thats what I am in need of.

    I have a txt file with 10,000 lines, each with a single number ranging from 2 to 14. I simply want to throw all the numbers into an array so they can be called in a loop.

    I've looked at the strtok() function but I'm not sure what I'd break by (what signifies a line break) and how I'd put each piece in an array.

    I'm coming from PHP and C++ is a totally different world (obviously) so please be patient with me.

    I am using Microsoft Visual C++ Express Edition.

    Thanks for your help!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The easiest way I see, if all lines contain just a single number, is to read line-by-line with getline and then use strtol (or tricky string streams) to convert to a number and then store in an array:
    Code:
    std::istream ifile("myfile");
    std::string line;
    long num;
    std::vector<long> vNumbers;
    getline(line, ifile);
    vNumbers.push_back( strtol(line) );
    You could work from that code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    or something like
    Code:
    std::ifstream fin("file.txt");
    std::vector<int> nums;
    while ( !fin.eof() ) {
         int x;
         fin >> x;
         nums.push_back(x);
    }
    ps. what you need here, I believe, is a vector instead of an array. A vector in C++ is more or less equivalent to an array in PHP. An array in C++ cannot change size (not easily at least), and does not know its own size (you cannot do something like "int a[5]; for (int i = 0; i < a.size(); ++i) { ... }" ).
    Last edited by cyberfish; 01-01-2008 at 07:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM