read data from text file

i use matlab to get the data.....after that i write the data in text file.......now i want read the data one by one and process the data using c++?

the text file in my dekstop........

anyone can help me.....please.....urgent......:(
Last edited on
when i read the tutorial, i do not understand.........anyone can help me with simple example........

if i have text file name......"test.txt"...........in the text file have data such as:-

0
1
2

may anyone help me to read the data one by one.......i want to add all the data one by one......please help me.........urgent.......:(

1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream input("test.txt"); //put your program together with thsi file in the same folder.

if(input.is_open()){

    while(!input.eof()){
          string number;
          int data;
           getline(input,number); //read number
           data = atoi(number.c_str()); //convert to integer
           cout<<data<<endl; //print it out
    }

}
AT the start, please also include header

1
2
3
4
5
#include<iostream>
#include<fstream>
#include<string>

using namespace std;
i use matlab to get the data.....after that i write the data in text file.......now i want read the data one by one and process the data using c++?


if i have text file name......"test.txt"...........in the text file have data.
may anyone help me to read the data one by one.......i want to process all the data one by one......please help me.........


its that my program correct or not?
if wrong......may anyone help me to correct it........:(



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

using namespace std;

int main ()
{
ifstream baca;
baca.open("test.txt");
double posx[50][1];
for (int i=0;i<2;i++)
{
posx[i][0]=test;
baca>>posx[i][0];
}
cout<<posx<<endl;
}
Last edited on
posx[i][0]=test;
This line is wrong. There is no variable test, and in the next line you overwrite posx[i][0] anyway.

Why is posx a 2D array? You only ever put data in [i][0], so what is [i][1] for?

Looking at your code, it seems that you've never coded in C++. I think you'd be better off doing this some other way.
Last edited on
i get the data from matlab.....afterthat i write the data in the text file name......"test.txt"...........in the text file have data such as:-

0
1
2

in "test.txt", i have 1 row and 3 column.......i want process the data in c++.......i want process the data one by one......if anyone can help me.....:(
im just beginner in this field.....:(

i create the program below to read and process the data.......
its that my program correct or not?
if wrong......may anyone help me to correct it........:(



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

using namespace std;

int main ()
{
ifstream baca;
baca.open("test.txt");
double posx[50][1];
for (int i=0;i<2;i++)
{
posx[i][0]=test[i][1];
baca>>posx[i][0];
}
cout<<posx<<endl;
}
Last edited on

posx[i][0]=test[i][1];

You're trying to set the value of posx[i][0] to be the same as test[i][1] - but test[i][1] does not exist! What is it meant to be? What are you trying to do here?
I want to take data from "test.text" one by one and put the data in the array posx and at the same time I want to process the data.actually that's what I want to do.....anyone can help me.......urgent......:(
Topic archived. No new replies allowed.