splitting a string and putting it into an array?

K

Kai Jaensch

Hello,

i am an newbie and i have to to solve this problem as fast as i can. But
at this time i don´t have a lot of success.
Can anybody help me (and understand my english :))?

I have a .txt-file in which the data is structured in that way:
Project-Nr. ID name lastname
33 9 Lars Lundel
33 12 Emil Korla
34 19 Lara Keuler
33 13 Thorsten Lammert

These data have to be read out row by row.
Every row has to be splitted (delimiter is TAB) and has to be saved in
an two-dimensional array.

The background is that in the next step i have to search for an ID in
that array and after that this part of an array is to be used, that has
the content of data (of the row) which belongs to the ID.
The data are used later in the program.

I tried different ways, but without success.
And here is the code i´ve written til now just to open the file and to
show the data:

FILE *importFile;
char row[100] = {0};
char test_array[100];
int anzahl=0;
importFile = fopen("datei.txt","r");
if(importFile == NULL) printf("Datei geschlossen.\n");
else printf("Datei offen.\n");

while(fgets(row, sizeof row, importFile) != NULL)
{
puts(row);
anzahl++;
}
if(EOF) printf("%d\n", anzahl);
fclose(importFile);


I hope, anybody can help me.
Thanx a lot.

Kai Jaensch
 
C

Chris Theis

Kai Jaensch said:
Hello,

i am an newbie and i have to to solve this problem as fast as i can. But
at this time i don´t have a lot of success.
Can anybody help me (and understand my english :))?

I have a .txt-file in which the data is structured in that way:
Project-Nr. ID name lastname
33 9 Lars Lundel
33 12 Emil Korla
34 19 Lara Keuler
33 13 Thorsten Lammert

These data have to be read out row by row.
Every row has to be splitted (delimiter is TAB) and has to be saved in
an two-dimensional array.

The background is that in the next step i have to search for an ID in
that array and after that this part of an array is to be used, that has
the content of data (of the row) which belongs to the ID.
The data are used later in the program.

I tried different ways, but without success.
And here is the code i´ve written til now just to open the file and to
show the data:

FILE *importFile;
char row[100] = {0};
char test_array[100];
int anzahl=0;
importFile = fopen("datei.txt","r");
if(importFile == NULL) printf("Datei geschlossen.\n");
else printf("Datei offen.\n");

while(fgets(row, sizeof row, importFile) != NULL)
{
puts(row);
anzahl++;
}
if(EOF) printf("%d\n", anzahl);
fclose(importFile);


I hope, anybody can help me.
Thanx a lot.

Kai Jaensch

This looks very much like a homework assignment :)

Some tips:

1.) Create a class/structure for your data.
e.g.
class CData {
// add access functions and so on...

friend istream& >>( std::istream& is, CData& rhs ) {
return is >> m_ProjectNr >> m_ID >> m_FirstName >> m_LastName;
}
protected:
int m_ID;
int m_ProjectNr;
std::string m_FirstName;
std::string m_LastName;
};

2.) Use streams to read your data:

ifstream InputFile("myfile.txt");
if( !InputFile )
return false;

CData MyData;
while( InputFile >> MyData ) {
// store data
}

3.) Store your data in a map with the ID being the key


This should be enough to get you going. Good luck!
Chris
 
S

Sean Kenwrick

Kai Jaensch said:
Hello,

i am an newbie and i have to to solve this problem as fast as i can. But
at this time i don´t have a lot of success.
Can anybody help me (and understand my english :))?

I have a .txt-file in which the data is structured in that way:
Project-Nr. ID name lastname
33 9 Lars Lundel
33 12 Emil Korla
34 19 Lara Keuler
33 13 Thorsten Lammert

These data have to be read out row by row.
Every row has to be splitted (delimiter is TAB) and has to be saved in
an two-dimensional array.

The background is that in the next step i have to search for an ID in
that array and after that this part of an array is to be used, that has
the content of data (of the row) which belongs to the ID.
The data are used later in the program.

I tried different ways, but without success.
And here is the code i´ve written til now just to open the file and to
show the data:

FILE *importFile;
char row[100] = {0};
char test_array[100];
int anzahl=0;
importFile = fopen("datei.txt","r");
if(importFile == NULL) printf("Datei geschlossen.\n");
else printf("Datei offen.\n");

Don't forget to quit your program if the file is geschlossen ;-)

while(fgets(row, sizeof row, importFile) != NULL)
{
puts(row);
anzahl++;
}
if(EOF) printf("%d\n", anzahl);
fclose(importFile);

It's not altogether clear from your description what you are going to index
the array on (ID or Proect/ID combination). I will assume you will use
the Project and ID as your index in which case you need an array something
like:

char MyArray[MAX_PROJECTS][MAX_IDS][MAX_NAMELEN+1];

Where you #define the MAX values for all the above.

After you have read the string from the file you need to extract the tokens
in the string. the easiest way to do this is to use strtok() in your main
loop as follows:

main()..
...
char * Proj, * ID, *Name;
...
while(fgets(row, sizeof row, importFile) != NULL)
{
Proj=strtok(row,"\t");
ID=strtok(NULL,"\t");
Name=strtok(NULL,"\t"); // In your real program check that none of
these are NULL (indicates bad data)

strcpy(MyArray[atoi(Proj)][atoi(ID],Name);
}

....

Note: atoi() converts ascii to integer..

Hope this helps...

Sean
 
K

Kajoka

Chris Theis said:
Kai Jaensch said:
Hello,

i am an newbie and i have to to solve this problem as fast as i can. But
at this time i don´t have a lot of success.
Can anybody help me (and understand my english :))?

I have a .txt-file in which the data is structured in that way:
Project-Nr. ID name lastname
33 9 Lars Lundel
33 12 Emil Korla
34 19 Lara Keuler
33 13 Thorsten Lammert

These data have to be read out row by row.
Every row has to be splitted (delimiter is TAB) and has to be saved in
an two-dimensional array.

The background is that in the next step i have to search for an ID in
that array and after that this part of an array is to be used, that has
the content of data (of the row) which belongs to the ID.
The data are used later in the program.

I tried different ways, but without success.
And here is the code i´ve written til now just to open the file and to
show the data:

FILE *importFile;
char row[100] = {0};
char test_array[100];
int anzahl=0;
importFile = fopen("datei.txt","r");
if(importFile == NULL) printf("Datei geschlossen.\n");
else printf("Datei offen.\n");

while(fgets(row, sizeof row, importFile) != NULL)
{
puts(row);
anzahl++;
}
if(EOF) printf("%d\n", anzahl);
fclose(importFile);


I hope, anybody can help me.
Thanx a lot.

Kai Jaensch

This looks very much like a homework assignment :)

Some tips:

1.) Create a class/structure for your data.
e.g.
class CData {
// add access functions and so on...

friend istream& >>( std::istream& is, CData& rhs ) {
return is >> m_ProjectNr >> m_ID >> m_FirstName >> m_LastName;
}
protected:
int m_ID;
int m_ProjectNr;
std::string m_FirstName;
std::string m_LastName;
};

2.) Use streams to read your data:

ifstream InputFile("myfile.txt");
if( !InputFile )
return false;

CData MyData;
while( InputFile >> MyData ) {
// store data
}

3.) Store your data in a map with the ID being the key


This should be enough to get you going. Good luck!
Chris


Thanks for all the help!
I´ll try my very best that it will work :).

Kai
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top