Thread: a weird problem with my code on writing & reading from file

  1. #1
    goldfish
    Guest

    Red face a weird problem with my code on writing & reading from file

    this program of mine is supposed to read input from the user via the keyboard. each sentence he types in, or each time he press enter, the sentence is stored into a node in the list.

    Code:
    struct list{
    	char text[255];
    	int num;
    	struct list *next;
    	struct list *prior;
    } list_entry;
    
    //code on reading inputing
    
    printf("Enter file content (CTRL-Z to end) : \n");
    	while(gets(s)){
    		info = (struct list *)malloc(sizeof(list_entry));
    		if(!info){
    			printf("\nnot enough memory");
    			return;
    		}
    		strcpy(info->text,s);
    		printf("info : %s",info);
    		listStore(info, &start, &last);
    	}
    	printf("\n");
                store();
    
    //code on writing to a file
           if((cfPtr=fopen(file,"w"))==NULL)
    	printf("File could not be opened\n");
           else{
    	while(info){
    		printf("info2:%s",info->text);
    		fwrite(info,sizeof(struct list),1,cfPtr);
    		fflush(cfPtr);
    		info = info->next;
    	}
    	fclose(cfPtr);
    	info = start;
    	clearList(&info);
    	printf("File saved\n");
             }
    
    //methods
    void listStore(struct list *i, struct list **start, struct list **last)
    {
    	struct list *p;
    
    	p = *last;
    
    	if(*last ==NULL){		//first element in list
    		i->next = NULL;
    		i->prior = NULL;
    		i->num = 0;
    		*last = i;
    		*start = i;
    		return;
    	}
    	else{
    		p->next = i;
    		i->next = NULL;
    		i->prior = p;
    		i->num = (p->num)+1;
    		*last = i;
    	}
    
    }
    
    //to view
    
    clearList(&info);
    printf("\nLoading File...\n");
    	system("cls");
    	int count=0;
    	while(!feof(fp)){
    		info = (struct list *)malloc(sizeof(list_entry));
    		if(!info){
    			printf("\nnot enough memory");
    				return 0;
    		}
    		if(1!=fread(info, sizeof(struct list), 1, fp)break;
    			printf("info3 :%s",info);
    			listStore(info,&start,&last);
    			num++;
    			count++;
    	}
    	fclose(fp);
    everything works fine, except one THING.....every time the user types more than 11 lines, that is 11 sentences, pressing ENTER 11 times, the lines after the 10 line doesn't show at ALL! What's weird is that the lines after 10 lines do get stored. when i open it, i find it there. But the >10 lines are separated by a huge GAP (pure spaces only) which i think could had caused this problem.

    CAn anyone help me pls???
    [code][/code]tagged by Salem

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    I doubt the CodeTagerator, Hammer, or anyone else is out at this hour.

    Could you _please_ use code tags?
    The world is waiting. I must leave you now.

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    *ignoring thread due to lack of code tags* haha you didn't see that one coming¿

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. make sure the start and last pointers really are NULL when you have an empty list

    Use a debug function like this to make sure your linked list has all the correct pointers.
    Code:
    void dump_list ( struct list *start struct list *last ) {
      printf( "start=%p, last=%p\n", start, last );
      while ( start ) {
        printf( "prior=%p, this=%p, next=%p\n", start->prior, start, start->next );
        start = start->next;
      }
    }
    You can add printing of num and the first few chars of text if you want

    > while(gets(s)){
    Use fgets, it's much safer
    Search the board for how to remove the newline at the end of an fgets() buffer if thats really important to you

    > if(1!=fread(info, sizeof(struct list), 1, fp)break;
    This doesn't even compile - missing )

    > info = (struct list *)malloc(sizeof(list_entry));
    The cast is not necessary in ANSI-C
    The cast does however mask a warning if you fail to include stdlib.h, which may in turn lead to incorrect code.

    > if((cfPtr=fopen(file,"w"))==NULL)
    You're writing binary data (a struct using fwrite), so this should be
    if((cfPtr=fopen(file,"wb"))==NULL)

    Ditto "rb" on the open for reading

    > fflush(cfPtr);
    The file will be flushed when you close it anyway, so doing this each time seems overkill to me.

  5. #5
    goldfish
    Guest

    Talking solved

    thanks a million salem

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    9
    Hmm found this old thread, and this code is more or less what I need for what I want to make(with some adaptation here and there)
    But I can't seem to find out what types the variables should be, could someone help me out please?

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Please dont bump old threads. Read the guidlines before posting.

    http://cboard.cprogramming.com/annou...ouncementid=51

    If you have a question, please start a new thread and link to this one.

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, 04:27 PM
  2. Problem with reading in file...
    By Bizmark in forum C Programming
    Replies: 3
    Last Post: 03-27-2008, 01:11 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM