I have a list of names in the file. I want to load it into a struct list every time before I insert any new names.
I can only load the data into an array how can I put it into list nodes?
I have a list of names in the file. I want to load it into a struct list every time before I insert any new names.
I can only load the data into an array how can I put it into list nodes?
using loop
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I know that. but how can I find out different names the file is just a piece of text. I do have spaces and \n in the text but How to really get them out and put into the right node.
I don't know what is the "right" node.
Show your code reading file into array
decribe the list structure you want to fill
decribe the concrete problem
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Well, if you don't have any deliminator char in your text. It is very difficult to token size the string. You should first work out that. Then writing your program would become more easier.Originally Posted by timgao
Because, a name can be in any length and starts with Caps or small case, Some have first name and last name.
Show us your text file as well.
ssharish2005
the text file looks like:
Name telephone seetnumber \n
Name telephone seetnumber \n
Name telephone seetnumber \n
code for loading file
the structCode:int fileSize(char* filename) { struct stat file; if(!stat(filename, &file)){ return file.st_size; } printf("Incorrect file %s\n", filename); return -1; } char* loadFile(char *filename) { char* file; int size; char c; FILE* fs; int i = 0; size = fileSize(filename); file = (char*) malloc(sizeof(char) * (size + 1)); fs = fopen("Namelist.txt", "r"); while((c = fgetc(fs)) != EOF){ file[i++] = c; } file[size] = '\0'; fclose(fs); return file; }
I want to load the file into the list before I can insert any new.Code:struct node { char *Name; int teleNr; int seetNr; struct node *next; };
If this was your text file you could tokensize ity very easily. Cos you a space as a delimiator in the while line. Follow thisCode:Name telephone seetnumber \n Name telephone seetnumber \n Name telephone seetnumber \n
Keep a whole line till you reach EOF (use fgets() function)
token size the string you will get the name
store that in the struct
repeat the step 1 again and till you reach EOF ( NULL if u use fgets())
And dont cast your malloc
How to tokensize the string
ssharish2005
Last edited by ssharish2005; 01-20-2007 at 05:33 AM.
so look for space every time but how can I move to the next node? look for \n?
fgets read a chars till it reaches \n. So for exampleOriginally Posted by timgao
You got remove the \n before the NULL char in the buf. Use strchr function. More details look through FAQCode:while(fgets(buf,80,fp) != NULL) printf("%s\n",buf);
This will get each line from your text file. Which mean u get data for each node.
After you get a line you can work through tokensizing the string.
ssharish2005
use fgets for reading the file - in this case you will read exactly 1 line containing 1 record, parse this line and will the struct
do it in a loop
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Thank both of you I will give a try. See if I have any more questions later.
I have got some new problems while search for \n in the text
the code works fine but it does not work when I change "\n" to " ".
text file is still the same.
name1 telephone1 1
name2 telephone2 2
name3 telephone3 3
here is the code
I try to print out len but I receive a strange number it's right at the beginning with 5 10 2 then it comes 515 I don't know what's this.Code:FILE *fs; char line[BUFSIZ]; int i = 0; int len = 0; fs = fopen("Namelist.txt", "r"); if ( fs ){ while ( fgets(line, sizeof line, fs) ){ char *token = line; for ( i = 0; *token; ++i ){ len = strcspn(token, " "); printf("%d",len); printf("token[%2d] = \"%*.*s\"\n", i, (int)len, (int)len, token); token += len + 1; } } fclose(fs); }
Last edited by timgao; 01-20-2007 at 12:25 PM.
try
strcspn(token, " \n");
the \n is stored in the line by the fgets
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
strcspn(token, " \n"); works fine.
that is my question what's wrong with " "
there is a \n symbol in your line, instead of treating it as a delimiter, you treat it as a part of the word
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler