load file to structure

This is a discussion on load file to structure within the C Programming forums, part of the General Programming Boards category; ftell() returns the position of the file stream. In this case (since you just opened the file), it will return ...

  1. #16
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    ftell() returns the position of the file stream. In this case (since you just opened the file), it will return zero. if(0 == NULL) is evaluating to true, so your program is exiting.
    bit∙hub [bit-huhb] n. A source and destination for information.

  2. #17
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Quote Originally Posted by bithub View Post
    ftell() returns the position of the file stream. In this case (since you just opened the file), it will return zero. if(0 == NULL) is evaluating to true, so your program is exiting.
    Thank you. I used fseek() before ftell()

    THis how I changed it , I decided to use do while instead of for loop, but it still not working, it loops though the loop, but I don't see any data that in the file to be stored into linked list

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include"keytype.h"
    
    void load(KEYS k,char *fn)
    {
    	KEYS curr;
    	FILE* fp;
    	
    	k=(KEYS*)malloc(sizeof(KEYS));
    	curr=k;
    
    	if((fp=fopen(fn, "r"))== NULL)
    	{
    		printf("Error, the file does not exist");
    		exit(1);
    	}
    	
    	fseek (fp, 0, SEEK_END);
    
    	if(ftell(fp)==0)
    	{
    		printf("IIIII\n");
    		exit(2);
    	}
    
    	do
    	{
    		curr->next=(KEYS*)malloc(sizeof(KEYS));
    
    		fscanf(fp, "%s", curr->next->addressee);
    		fscanf(fp, "%s", curr->next->sender);
    		fscanf(fp, "%s", curr->next->regarding);
    		fscanf(fp, "%d%*c%d%*c%d", &curr->next->date.month, &curr->next->date.day, &curr->next->date.year);
    		fscanf(fp, "%d", &curr->next->id);
    		fscanf(fp, "%s", curr->next->fname);
    		
    	}while(!feof(fp));
    
    	curr->next=NULL;
    
    	fclose(fp);
    	/*
    	curr=k;
    	 while(curr!=NULL)
    	{
    		 printf( "%s\n", curr->addressee);
    		 curr=curr->next;
    	}
    	*/
    }

  3. #18
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    You seek to the end of the file, then try and read data from the file. There's nothing to read since you are at the end of the file. You need to rewind() to the beginning of the file.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #19
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Quote Originally Posted by bithub View Post
    You seek to the end of the file, then try and read data from the file. There's nothing to read since you are at the end of the file. You need to rewind() to the beginning of the file.
    thank you, and I just understood I also don't need to malloc hte space for KEYS k, as a head node already passed to the function, I only need to malloc for k->next
    Last edited by nynicue; 08-06-2009 at 07:24 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. File send app - md5 checksum different when load is put on server.
    By (Slith++) in forum Networking/Device Communication
    Replies: 5
    Last Post: 12-31-2007, 01:23 PM
  3. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 06:13 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21