read txt file into array please help me !

Jan 3, 2013 at 3:34pm
Hi sir !
My assignment is : I have a text file and I have to read in that text file to get every 4 symbol after the symbol “=” of each element (ex : 1= -87 --> I have to get (-87) then put it into an array so I can calculate the average value of each 24 elements in each cols respectively .
Woa… I’m so sorry because I’m a newbie in C++, … I struggled two day with my assignment so I hope you can help me to find the way to do it …what I can do now is getting the whole of each element with the “=” included , I don’t know the way to scan and get the data after ‘=’ ….
Thanks so muchhhhhhhhhhhhhhhhhhhhhh for your help… ! ps: my txt file look like this :
1=-100 2=-100 3=-80 4=-85 5=-62 6=-86 7=-100 8=-89
1=-98 2=-100 3=-80 4=-85 5=-62 6=-86 7=-100 8=-89
1=-100 2=-86 3=-77 4=-89 5=-66 6=-100 7=-92 8=-86
1=-100 2=-86 3=-77 4=-89 5=-66 6=-100 7=-92 8=-86
1=-100 2=-77 3=-79 4=-100 5=-77 6=-100 7=-100 8=-87
1=-100 2=-77 3=-79 4=-100 5=-77 6=-100 7=-100 8=-87
1=-100 2=-89 3=-82 4=-80 5=-67 6=-100 7=-100 8=-83
1=-100 2=-89 3=-82 4=-80 5=-67 6=-100 7=-100 8=-83
1=-100 2=-89 3=-82 4=-80 5=-67 6=-100 7=-100 8=-83
1=-100 2=-89 3=-82 4=-80 5=-67 6=-100 7=-100 8=-83
1=-100 2=-100 3=-78 4=-83 5=-66 6=-100 7=-100 8=-88
1=-100 2=-100 3=-78 4=-83 5=-66 6=-100 7=-100 8=-88
1=-100 2=-100 3=-78 4=-83 5=-66 6=-100 7=-100 8=-88
1=-100 2=-100 3=-78 4=-83 5=-66 6=-100 7=-100 8=-88
1=-87 2=-84 3=-78 4=-93 5=-61 6=-100 7=-100 8=-56
1=-87 2=-84 3=-78 4=-93 5=-61 6=-100 7=-100 8=-56
1=-87 2=-84 3=-78 4=-93 5=-61 6=-100 7=-100 8=-92
1=-87 2=-84 3=-78 4=-93 5=-61 6=-100 7=-100 8=-92
1=-89 2=-100 3=-73 4=-80 5=-63 6=-100 7=-100 8=-90
1=-89 2=-100 3=-73 4=-80 5=-63 6=-100 7=-100 8=-90
1=-89 2=-100 3=-74 4=-100 5=-75 6=-100 7=-100 8=-92
1=-89 2=-100 3=-74 4=-100 5=-75 6=-100 7=-100 8=-92
1=-89 2=-100 3=-74 4=-100 5=-75 6=-100 7=-100 8=-92
1=-89 2=-100 3=-74 4=-100 5=-75 6=-100 7=-100 8=-92
Jan 3, 2013 at 3:51pm
Taking the first line:
1=-100 2=-100 3=-80 4=-85 5=-62 6=-86 7=-100 8=-89
In this line for the first entry do you want to extract 100 or -100?

Please show your code where you are trying to extract the information.

Jan 3, 2013 at 3:59pm
thanks so much for ur reply: i want to extrat -100 . ^^
here is my code : i just can get the whole of each element included : "=" symbol .


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

int main()
{
int i = 0, j = 0;
string line;
string array[24][8];
ifstream myfile("temple.txt");
if(myfile.is_open())

cout << "File Open\n";
else
cout << "File not open\n";

for(int i = 0; i < 24; ++i)
{
for(int j = 0; j < 8; ++j)
{

if(getline(myfile, line, '\n'))

{
array[i][j] = line;

cout << array[i][j] << " " <<endl;
}
}
}

myfile.close();

cin.get();

return 0;
}
Jan 3, 2013 at 6:07pm
Is there any reason you are using strings to hold these numbers?

This would much easier if you were using an array of int to hold the numbers.

Are you allowed to use stringstreams?

Also place your code between cod tags, this will make reading your code much easier.


Last edited on Jan 3, 2013 at 6:08pm
Topic archived. No new replies allowed.