Jump to content

Pls help me, urgent very urgent. no joke.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
salvate_me

salvate_me

    Newbie

  • Members
  • Pip
  • 8 posts
Ok the story goes when i receive my assignment in school, it's a assignment of key lock C programming.. and it due on 15 of Oct 2009, someone plssss help me with it, i do half, and i stuck in reading the text file, i can read it but i can't use loop to read all of the data in the text file, SOMEONE pls help me, i wil be REALLY REALLY APPRECIATE IT, Plsss....

(sori i just wan to show my zip file so that someone can help me ;( plz dont ban me..... )

this is no joke, pls help me.. i wil be waiting...

Pls reply me if u think u can help me...

Edited by salvate_me, 10 October 2009 - 04:23 PM.


#2
davidthefat

davidthefat

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
ok did you try checking if its end of file?

End of File Function --C++

#3
salvate_me

salvate_me

    Newbie

  • Members
  • Pip
  • 8 posts
thanks for replying? i did not end the file, bt i think thats not the problem cause its the loop not working.. i wan it store 10 row data, but it just show me 1 T_T~

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
let's look at one function to start with:
key * ky_read(FILE * file)
{
	int k;
	int id;
	char str[100];
	
		key * newKey = (key *) malloc(sizeof(key));
		if(file==NULL)
		{
			printf("error opening file!!\n");
			getchar();
			exit(1);
		}
		else
		{
		for(k = 0; k < 10; k++)
		{
		fscanf(file, "%d %s", &(newKey->id), &str);
		
		newKey->code = (char*)malloc(strlen(str));
		
		strcpy(newKey->code, str);
		}
		return newKey;
		
		}
}
 
To start with, you are assuming 10 keys in the file. That is incorrect, based on the problem statement. The number of keys will be in the first line of the text file.

More importantly, you read ALL the keys, but only return one! The one you return is the last one, not the first! You need to read the first line of the file, allocate enough memory for an ARRAY of keys, and return a pointer to the first element in the array.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
salvate_me

salvate_me

    Newbie

  • Members
  • Pip
  • 8 posts
thanks sir for the reply, really appreciate it, actually the ky_read is okay, what i need to change is at my keylock.c, cause i need to use read all the row of the data using loop and store into a keylock array. this ky_read is use for reading each row of data only.

Edited by salvate_me, 10 October 2009 - 05:26 PM.


#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Then you'll need to remove the for loop from ky_read and move that into keylock.c.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
salvate_me

salvate_me

    Newbie

  • Members
  • Pip
  • 8 posts
Alright! i manage to done it ! thanks wingedpanther! =D well, but i have 1 more question to ask, how to allocate the memory so that the data wont leak? i m quite confuse on the memory allocation things.

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
http://forum.codecal...oc-realloc.html
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog