Click here to Skip to main content
12,818,181 members (38,132 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++
have a string array which keeps data which read from a file. it consists 70 lines.

what i want to do is store those in different arrays. like from 21st line to 31st line in 1 array. and 31st to 41st one array. how can i do it... plz help

i want to split 70 lines into 7 arrays each containing 10 lines of it. and do it without using vectors
Posted 1-Jan-13 22:59pm
Zoltán Zörgő 2-Jan-13 4:04am
1) "and do it without using vectors" - why exactly?
2) This looks very simple, what have you tried so far?
3) The number of rows and arrays is constant?
Graham Breach 2-Jan-13 4:16am
You already asked this yesterday:
CPallini 2-Jan-13 4:24am
Why do you need that? Please elaborate.
Richard MacCutchan 2-Jan-13 4:57am
Why not create the individual arrays as you read the file?
Sith Indunil 2-Jan-13 7:39am
well @Cpallini cz nothing worked for me..... i tried several ways but nothing worked..

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Something similar to this might work for you;

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main (int argc, const char* argv[])
    // Read the lines
    string lines[70];
    fstream file("C:\\Temp\\file.txt");
    string line;
    int index = 0;
    while(getline(file, line) && index < 70)
        lines[index++] = line;

    // Split the lines across arrays of ten lines each
    string splitLines[10][10];
    for(int i = 0; i < index; ++i)
        splitLines[i / 10][i % 10] = lines[i];

    return 0;

Hope this helps,
jibesh 2-Jan-13 6:23am
nice Answer!!
Sith Indunil 2-Jan-13 7:38am
thanks Fredrik :D
Fredrik Bornander 2-Jan-13 8:00am
Glad I could help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170308.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100