By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

splitting a string and putting it into an array?

P: n/a
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

Nov 14 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Kai Jaensch" <ka*********@gmx.de> wrote in message
news:bu************@ID-189932.news.uni-berlin.de...
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
Nov 14 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.