i would like to insert my data from txt file to array.
but each line of my txt file contains more than one data.
eg : "Data1","Data2","Data3","Data4"
how do i sepreate each data to array[0][0],array[0][1],array[0][2] and array[0][3]??
i only know how to use getline function to take a whole line of data into an variable.
can sum1 teach me? thanks

Post some code so that we can see what you have attempted until now.

If you have the same number of values on each line in your file then you can do something like this

ifstream myFile;
  // declare your data variables;

  // open your file

  // read the contents
  myFile >> data1 >> data2 >> data3 >> data4;

If you don't have the same number of values on each line, then you will need to use getline() and parse it to get your values. You can use stringstream to do that.

i don get this myFile >> data1 >> data2 >> data3 >> data4;
can u explain in more detail? this is my code. thanks.

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

int main () {
  char data1[50];
  ifstream myfile ("C:\\Users\\Chern\\example.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      myfile.getline (data1, 100);
      cout << data1 << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  system("pause");
  return 0;
}

i don get this myFile >> data1 >> data2 >> data3 >> data4;
can u explain in more detail?

What don't you get about it? It's the same as:

myFile >> data1;
myFile >> data2;
myFile >> data3;
myFile >> data4;

okay i get it but how do i set a delimiter to seperate my data?

example of my txt file : hi hi hi hi
data1 will be hi
data2 will be hi
data3 will be hi
data4 will be hi
the data seperated by the spaces
but i want my data1 to be "hi hi" instead of only "hi"

Perheps while you read the file you can use another stringvariable like:

string Addd1d2;

Addd1d2 = data1 + " " + data2;

example of my txt file : hi hi hi hi
data1 will be hi
data2 will be hi
data3 will be hi
data4 will be hi
the data seperated by the spaces
but i want my data1 to be "hi hi" instead of only "hi"

Perheps while you read the file you can use another stringvariable like:

string Addd1d2;

Addd1d2 = data1 + " " + data2;

if i use this way i have to create alot of temp variable?
cuz my data will be containing alot of words

If you use a variable of the type ifstream to read your data like I suggested, it will use a space as a separator for each variable.

If you want your variables to store list of words and not just one word, then you will need to read in the whole line using getline and then parse it. Or you will need to do as Liszt suggested and concatenate the values you read in.

can u teach me how to parse??

example of my txt file : hi hi hi hi
data1 will be hi
data2 will be hi
data3 will be hi
data4 will be hi
the data seperated by the spaces
but i want my data1 to be "hi hi" instead of only "hi"

I'd say you need to be more explicit on what you are looking for. For the following text file:

hi bye hello howdy

What should the values of data1, data2, data3, data4 store? Also, where's the array? Do you want data[1], data[2], data[3], data[4] instead of data1, data2, data3, data4?

Also, keep in mind that array indexes start at 0, not 1, so you may want data[0], data[1], data[2], data[3] instead. Finally, post your updated code since the last time you posted you had a data1[] array, no array[][] array, and no data2, data3, data4 variables or arrays. It's getting confusing about what you are defining, how, and where.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.