how to read data from textfile and push in array

Hi there....

1stly sorry for the novice and beginner question but I really need help since I've been get fiddling to figure out the error but still have no idea whats wrong with it...here are's the code I'm using
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.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MaxSize 100
#define MaxFound 30

class List{

    int element;
    int index;
    int myList[MaxSize];
    char __filename[50];

    public:
    List(char filename[50]){

        strcpy(__filename,filename);

    }

    int readFile(){
        element = 0;
        index = -1;
        FILE *datafile;
        int i=0;

        datafile = fopen(__filename,"r");
        while((!feof(datafile))&&(i<MaxSize)){
                fscanf(datafile,"%d",&myList[i].element);
                myList[i].index = i;
               i++;
        }//end while
        return i;
    }

    //%d --> integer
    //%f -->float
    //%s -->string


    void displayList(int MySize){
        int j;

        cout<<"\n";
        for(j=0;j<MySize;j++){
            cout<<myList[j].element<<"  "<<endl;;
        }
    }


}; //end clas



main(){

      int fileSize;

      List obj1("senarai.txt");
      fileSize = obj1.readFile();
      obj1.displayList(fileSize);

      return 0;
}

So I really need help since I'm still beginner in C++ hoep someone can shed some light to me at least tell me whats wrong with this code... I'm using borlandc++ 4.5

here's the list of error..that keep coming up...

=================================================

Warning BACATEXT.CPP 29:Functions containing while are not expand inline
Warning BACATEXT.CPP 46:Functions containing for are not expand inline
Error BACATEXT.CPP 30:Structure required on left side of . or .*in function LIst::readFile()
Error BACATEXT.CPP 31:Structure required on left side of . or .*in function LIst::readFile()
Error BACATEXT.CPP 47:Structure required on left side of . or .*in function

LIst::displayList(int)
In the above post on lines 31, 32 and 48 you have defined mylist as an int array (line 13) so you can't reference elements with the '.' notation because there are no elements.

It looks like you need an array for the index member variable as well. There are a couple of ways you could go; either create a structure with an index and element called mylist or make index an array, the first one appears to make more sense from my understanding of what you are trying to do.

I've run this through my compiler (not Borland) and it complains about passing a constant string to a char * on line 61.

Finally I had to make the following changes to the top of the file for my compiler to compile the code.

1
2
3
4
5
6
7
8
9
10
11
// #include <iostream.h> not necessary for compilation but good practice
#include <iostream> 
#include <stdio.h>
#include <conio.h>
// #include <string.h> not necessary for compilation but good practice
#include <string>        
#define MaxSize 100
#define MaxFound 30

using namespace std;   // added for cout and endl scope

Last edited on
Okay 1stly thansk for the reply and feedbacks...Okay..actually what I try to done is how to feed array with different datatype in my case...because I read data from txt file into array here's are my texfile format

john 30
matt 40
michael 50

so how to feed the array with this two datatype I mean char and integer.....load from textfile...I've been reading about vector but it seems I cant use it because currently I'm using borland c++ V4.5 therefore I cant include vector.h in my program....so I really need help in how to ahieve it at least tell me the rite path....any help are really appreciated
Topic archived. No new replies allowed.