Parse text from file

Hi folks,

my file content is looks like this:

group,input1,input2,input3,
groupA,1,2,3,

so my target is to read data from file and save into array so that i can call the array to display the data anytime. so my code is looks like this:

//================================
void read(char* p);
void print(char* q);

void read(char* p)
{
char file[FILENAME_MAX];
char name[FILENAME_MAX];
ifstream inFile;
int cnt=0;

do{
cout<<"\nPlease enter a file name.";
cin.getline(file, FILENAME_MAX, '\n');
}while(file[0]==NULL);

cout<<"\nOpening file: "<<file;

for(int i=0; i<8; i++)
{
inFile.getline(name,FILENAME_MAX, ',');
p[cnt]=name;
cnt++;
}

}

void bar(char* q)
{
cout <<"\n"<< q[0];
cout <<"\n"<< q[2];
cout <<"\n"<< q[3];
cout <<"\n"<< q[4];
cout <<"\n"<< q[5];
cout <<"\n"<< q[6];
cout <<"\n"<< q[7];

cout <<"\n"<< q[0];
cout <<"\n"<< q[4];
cout <<"\n"<< q[5];
cout <<"\n"<< q[6];

}

int main() {
char* ptr = new char[10];
foo(ptr);
bar(ptr);
}

//======================
expected display is:
group
input1
input2
input3
groupA
1
2
3
group
groupA
1
2

//=======================

above is my idea or any suggestion to do this? Please advide. I have been try for many method but it still cant display the output. Thanks.

The following code listing demonstrates your intent using an array of character pointers ( ... hence the char **)

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
void read(char * p[]);
void print(char * q[]);



void main() 
{
   char ** ptr = new char *[10];

   // allocate c-strings for each element in ptr
   for (int i=0; i<10; i++)
      ptr[i] = new char[FILENAME_MAX];

   read(ptr);
   print(ptr);

   // delete ptr array items
   for (i=0; i<10; i++)
      delete ptr[i];

   // delete ptr array
   delete []ptr;
}



void read(char * p[]) 
{
   char file[FILENAME_MAX];
   char name[FILENAME_MAX];
   ifstream inFile;
   int cnt=0;

   do
   {
      cout<<"\nPlease enter a file name.";
      cin.getline(file, FILENAME_MAX, '\n');
   }while(file[0]==NULL);

   cout<<"\nOpening file: "<<file;
   inFile.open(file);
   
   for(int i=0; i<8; i++)
   {
      inFile.getline(name,FILENAME_MAX, ',');
      strncpy(p[cnt], name, FILENAME_MAX); 
      cnt++;
   }

}

void print(char * q[]) 
{
   cout <<"\n"<< q[0];
   cout <<"\n"<< q[2];
   cout <<"\n"<< q[3];
   cout <<"\n"<< q[4];
   cout <<"\n"<< q[5];
   cout <<"\n"<< q[6];
   cout <<"\n"<< q[7];

   cout <<"\n"<< q[0];
   cout <<"\n"<< q[4];
   cout <<"\n"<< q[5];
   cout <<"\n"<< q[6];
}


This method requires using a pointer to a pointer (double pointer) in an ineffective way - note the allocation of the array of char pointers of size 10. This allocated an array for 10 character pointers. This array is actually 40 bytes in size, since each element is 4 bytes (size of pointer)

We then iterate through all 10 items and allocate a 260 byte char array to each item.

Note the change of parsing to your read and print functions. We could also have parsed a double pointer (char **) instead of (char * q[]) ...

Also note the deletion of each array item and also the array itself.

This adds alot of coding overhead and would personally use a vector of strings instead of the array of character arrays.
Thanks. i will try to use vector of string in the coding. Thanks.
Topic archived. No new replies allowed.