Thread: how can I load a file into a stuct

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    11

    how can I load a file into a stuct

    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?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    11
    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.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by timgao
    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.
    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.

    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

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    11
    the text file looks like:

    Name telephone seetnumber \n
    Name telephone seetnumber \n
    Name telephone seetnumber \n

    code for loading file

    Code:
    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;
    }
    the struct

    Code:
    	struct node {
    		char *Name;
    		int teleNr;
    		int seetNr;
    		struct node *next;
    	};
    I want to load the file into the list before I can insert any new.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    Name telephone seetnumber \n
    Name telephone seetnumber \n
    Name telephone seetnumber \n
    If this was your text file you could tokensize ity very easily. Cos you a space as a delimiator in the while line. Follow this

    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.

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    11
    so look for space every time but how can I move to the next node? look for \n?

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by timgao
    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 example
    Code:
    while(fgets(buf,80,fp) != NULL)
         printf("%s\n",buf);
    You got remove the \n before the NULL char in the buf. Use strchr function. More details look through FAQ

    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

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  11. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    11
    Thank both of you I will give a try. See if I have any more questions later.

  12. #12
    Registered User
    Join Date
    Jan 2007
    Posts
    11
    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

    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);
        }
    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.
    Last edited by timgao; 01-20-2007 at 12:25 PM.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  14. #14
    Registered User
    Join Date
    Jan 2007
    Posts
    11
    strcspn(token, " \n"); works fine.
    that is my question what's wrong with " "

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM