hello ..
i just need a lil guideline... m making a program where i have to apply counting sort on the data which is comma seperated and it is written in a text file like this
59,54,40,79,38
28,98,77,71,74
24,91,56,82,51
74,36,98,29,41
39,10,52,3,24
30,5,4,70,15
29,55,40,56,23
8,94,69,13,37
79,96,54,53,29
12,29,30,99,35
63,87,64,6,6
65,56,80,37,89

total no. of lines are 20 and each line contains 5 elements.
problem is i cant get it how to store all the numbers in an array
i have made the function which will take the data from the file and it will covert it into an int but i want the whole data in an array so that i can apply the counting sort in it..

#include<iostream>
#include<cstdlib>
#include<string>
#include<fstream>
using namespace std;

string getdata(string line,int n)

    int recNo=0, prvComma=0, nxtComma;
    string out;
    for(int i=0; i<line.length(); i++)
    {
        nxtComma=line.find(',',prvComma);
        out=line.substr(prvComma,nxtComma-prvComma);
        prvComma=nxtComma+1;

        if(recNo++==n)
        return out;
    }
}


void main()
{
    string line;
    int i;
    int num;
    int num1,num2,num3,num4;
    double m,n;
    int data[5];
    ifstream myfile("CS DataSet.txt");

    if(! myfile.is_open())
    {
        cout<<"unable to open"<<endl;
        return;
    }

    for(i=0; i<1; i++)
    {
        getline(myfile,line);
        num=strtod(getdata(line,0).c_str(),NULL);
        num1=strtod(getdata(line,1).c_str(),NULL);
        num2=strtod(getdata(line,2).c_str(),NULL);
        num3=strtod(getdata(line,3).c_str(),NULL);
        num4=strtod(getdata(line,4).c_str(),NULL);



    }

        data[0]=num;
        data[1]=num1;
        data[2]=num2;
        data[3]=num3;
        data[4]=num4;

    for(i=0;i<5;i++)       
    {
        cout<<data[i]<<endl;
    }
    myfile.close();
    system("PAUSE");
}

I would think you should be able to do this. I have not tested this.

// main always returns an int so dont forget to change this
int main()
{
    std::stringstream ss;
    std::string line;
    std::fstream fin("somefile.txt");
    std::vector<int> numbers;
    // read each line from the file
    while(std::getline(fin, line)
    {
        std::string temp;
        //load the line into the stringstream
        ss >> line;
        // get each number from the stream using the comma as the delimiter
        while(std::getline(ss, temp, ','))
        {
            // add number to vector
            numbers.push_back(strtod(temp.c_str(), nullptr));
        }
        // dont forget to clear out the stringstream for the next use
        ss.str(std::string());
        ss.clear();

Your program is doing way too much work! There's an easier way to do it. The code below does NO error checking so you might want to add that.

ifstream myfile("CS DataSet.txt");

std::string line;
int nums[20][5];
int row = 0;
int col = 0;
while( myfile(line,',')
{
   nums[row][col] = atoi(line.c_str());
   col++;
   if(col > 5)
   {
      col = 0;
      row++;
   }
}

// now you can sort the array however you wish

Edited 2 Years Ago by Ancient Dragon

@AD What does line 7 do? I have never seen that.

It's an error. Sorry about that. That's what happens to old men who try to think too much :)

while( std::getline(myfile,line,',') )

Edited 2 Years Ago by Ancient Dragon

@AD What does line 7 do? I have never seen that.

He probably intended while (myfile.getline(line,',')).

i can't get your while loop .. its giving an error

'An error' isn't helpful information. Which error?

Edited 2 Years Ago by Hiroshe: formatting

Since the file is formatted like

59,54,40,79,38
28,98,77,71,74

Will that getline work? What happens when it hits the newline between 38 and 28 since you overwrite the default delimiter?

Hmm. If it reads "59,54,40,79,38\n", 38 should still be left in the stream. Then it should read "38". Then "28,98,77,71,74\n" ...

@AD If I run the following code that you said to use I get the following output from the array. This is using the sample set of data the OP provided.

59 54 40 79 38 
77 71 74 91 56 
51 36 98 29 41 
52 3 24 5 4 
15 55 40 56 23 
69 13 37 96 54 
29 29 30 99 35 
64 6 6 56 80 
89 -858993460 -858993460 -858993460 -858993460 
-858993460 -858993460 -858993460 -858993460 -858993460 
-858993460 -858993460 -858993460 -858993460 -858993460 
-858993460 -858993460 -858993460 -858993460 -858993460 

This is the code I used to get that.

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
    ifstream myfile("test.txt");
    ofstream fout("output.txt");

    std::string line;
    int nums[12][5];
    int row = 0;
    int col = 0;
    while(getline(myfile,line,','))
    {
        nums[row][col] = atoi(line.c_str());
        col++;
        if(col > 5)
        {
            col = 0;
            row++;
        }
    }
    for (int i = 0; i < 12; i++)
    {
        for (int j = 0; j < 5; j++)
            fout<< nums[i][j] << " ";
        fout << endl;
    }
    cin.get();
}

If I run my version of the code changed to use a 2d array I get

59 54 40 79 38 
28 98 77 71 74 
24 91 56 82 51 
74 36 98 29 41 
39 10 52 3 24 
30 5 4 70 15 
29 55 40 56 23 
8 94 69 13 37 
79 96 54 53 29 
12 29 30 99 35 
63 87 64 6 6 
65 56 80 37 89 

Using this code

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

// main always returns an int so dont forget to change this
int main()
{
    std::stringstream ss;
    std::string line;
    std::ifstream fin("test.txt");
    std::ofstream fout("output.txt");
    int numbers[12][5];
    int i = 0;
    int j = 0;
    // read each line from the file
    while(std::getline(fin, line))
    {
        std::string temp;
        //load the line into the stringstream
        ss << line;
        // get each number from the stream using the comma as the delimiter
        while(getline(ss, temp, ','))
        {
            // add number to vector
            numbers[i][j] = atoi(temp.c_str());
            j++;
        }
        //reset j
        j = 0;
        // dont forget to clear out the stringstream for the next use
        ss.str(std::string());
        ss.clear();
        i++;
    }
    for (i = 0; i < 12; i++)
    {
        for (j = 0; j < 5; j++)
            fout<< numbers[i][j] << " ";
        fout << endl;
    }
    cin.get();
}

You're right, this doesn't really work

`while (getline(myfile, line, ','))`

because when the last numbe on the line is read getline() does not stop until it reaches the next comma. Your's is a much easier and safer solution.

thank you ppl for helping me now i can easily apply all the sorts on data :)

ummm guys ..
i was converting this 2d array in 1d but now its givng output 0000000....
i cant figure out where is the prob lyng

    #include <iostream>
    #include <fstream>
    #include <sstream>
    using namespace std;

    void ata(int a[][5],int,int,int aa[]);

    // main always returns an int so dont forget to change this
    int main()
    {
    std::stringstream ss;
    std::string line;
    std::ifstream fin("test.txt");
    std::ofstream fout("output.txt");
    int numbers[20][5];
    int i = 0;
    int j = 0;
    // read each line from the file
    while(std::getline(fin, line))
    {
    std::string temp;
    //load the line into the stringstream
    ss << line;
    // get each number from the stream using the comma as the delimiter
    while(getline(ss, temp, ','))
    {
    // add number to vector
    numbers[i][j] = atoi(temp.c_str());
    j++;
    }
    //reset j
    j = 0;
    // dont forget to clear out the stringstream for the next use
    ss.str(std::string());
    ss.clear();
    i++;
    }
    for (i = 0; i < 20; i++)
    {
    for (j = 0; j < 5; j++)
    fout<< numbers[i][j] << " ";
    fout << endl;
    }

    int num[100];
    ata(numbers,i,j,num);
    cin.get();
    }


    void ata(int numbers[20][5],int i,int j, int num[100])
    {
        cout<<"2d array in 1d array"<<endl;
        int l=0;
    int k=0;
    int m=0;
    for(k=0;k<i;k++)
    {
        for(m=0;m<j;m++,l++)
        {
            num[l]=numbers[i][j];

        }
    }
    int a;
    for(a=0;a<100;a++)
    {
        cout<<num[a]<<endl;
    }
    }

now its giving corrct output but still confused what i did .. :/

    #include <iostream>
    #include <fstream>
    #include <sstream>
    using namespace std;

    void ata(int a[][5],int,int,int aa[]);

    // main always returns an int so dont forget to change this
    int main()
    {
    std::stringstream ss;
    std::string line;
    std::ifstream fin("test.txt");
    std::ofstream fout("output.txt");
    int numbers[20][5];
    int i = 0;
    int j = 0;
    // read each line from the file
    while(std::getline(fin, line))
    {
    std::string temp;
    //load the line into the stringstream
    ss << line;
    // get each number from the stream using the comma as the delimiter
    while(getline(ss, temp, ','))
    {
    // add number to vector
    numbers[i][j] = atoi(temp.c_str());
    j++;
    }
    //reset j
    j = 0;
    // dont forget to clear out the stringstream for the next use
    ss.str(std::string());
    ss.clear();
    i++;

    int num[100];
    ata(numbers,i,j,num);
    }


    std::cout<<"2d array in 1d array"<<endl;



    for (i = 0; i < 20; i++)
    {
    for (j = 0; j < 5; j++)
    {
    fout<< numbers[i][j] << " ";
    fout << endl;
    }
    }


    cin.get();
    }


    void ata(int numbers[20][5],int i,int j, int num[100])
    {

        int l=0;
    int k=0;
    int m=0;
    for(k=0;k<i;k++)
    {
        for(m=0;m<j;m++,l++)
        {
            num[l]=numbers[i][j];

        }
    }
    int a;
    for(a=0;a<k*m;a++)
    {
        std::cout<<num[a]<<endl;
    }
    }
This article has been dead for over six months. Start a new discussion instead.