problem with getting data from file

hi sweet friends !

i'm a newbie in C++ and i have some trouble with my code in order to get some data from a text file.
i have a .txt file which is looked like this : (3 rows, 8 cols)

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

in that matrix : 1-8 is index of wifi access points and the value after '=' is the signal strength recorded from 8 access point.. i have to get the value of each element in that matrix and put it into an array (without index and '='- only value ) i tried to write a code to get these value and print them in the screen to test whether i get the value i want or not, but it display each value with some strange symbol after ... could somebody help me figure it out please... thanks so mucccchhhh for any reply !
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdlib.h>
using namespace std;

int main()
{
unsigned int len;
int i = 0, j = 0;
string line;
int  lengh;
char buffer[4];
string array[3][8];

ifstream myfile("temple.txt");
if(myfile.is_open())

std::cout << "File Open\n";
else
{cout << "File not open\n";}

for(int i = 0; i < 3; ++i)
{
	  for (int j = 0; j < 7; ++j)
            { 
               if(getline(myfile, line, '\t')) 
					   {
						array[i][j] = line;
						cout << array[i][j] << ' ' ;
			                   }

	    }
					getline(myfile,line, '\n');
					array[i][7]=line;
					cout << array[i][7] << endl ;
				
}
cout << endl << array[0][7] << endl << array[1][7] << endl << array[2][7];// just for test 


for(int i = 0; i < 3; ++i)
{
	  for (int j = 0; j < 8; ++j)
             { 
				 len = array[i][j].length(); 
				 cout << endl << len << '\n' ; 
						
				 for (int k=0; k < len; k++ )
						  {
							if ( array[i][j][k] == '=' )  
					                               {
										
										lengh = array[i][j].copy(buffer, len - k, k+1);
										buffer[lengh]='\t';
										cout << ' ' << buffer  ;
									}
						  }
	        }
}
	
myfile.close();
cout<<endl<<"love"<< endl;
cin.get();

return 0;
}
Last edited on
My first thought was the code looks more complex than it needs to be. but that was a gut feeling, rather than a proper analysis.

Anyway, the main problem is the size of the buffer.
 
char buffer[4];

This is required to hold these characters:
'-', '1', '0', '0' - this makes four
in addition the char '\t' is tacked onto the end, that makes five. Properly there should also be a null terminator, which makes a total of 6.

So, increase the buffer size to 6, and add the null terminator '\0' after line 56.

Alternative version, based upon the above code, but slightly modified:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdlib.h>

using namespace std;

int main()
{
    string line;
    string word;
    string array[3][8];

    ifstream myfile("..\\..\\data.txt");
    if (myfile.is_open())
        cout << "File Open\n";
    else
        cout << "File not open\n";

    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 8; ++j)
        {
            if (myfile>>word) {
                if (word[1] == '=')
                {
                    array[i][j] = word.substr(2);
                    cout << array[i][j] << ' ' ;
                }
            }
        }
        cout << endl;
    }

    myfile.close();
    cout<<endl<<"love"<< endl;
    cin.get();

    return 0;
}
Last edited on
Topic archived. No new replies allowed.