File Handling... Greetings everyone!

File Handling... Greetings everyone!

Page 1 of 1

11 Replies - 761 Views - Last Post: 18 November 2010 - 06:46 AM Rate Topic: -----

#1 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

File Handling... Greetings everyone!

Posted 18 November 2010 - 04:25 AM

Good day everyone!

I have my program here for adding, deleting, displaying and searching... The problem that I was facing is how to edit.... I'm thinking of such a way.. And I tried the r+... is it using r+ and using remove and rename? Kindly formulate the idea in such that I will be able to create hehehe....
Is This A Good Question/Topic? 0
  • +

Replies To: File Handling... Greetings everyone!

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: File Handling... Greetings everyone!

Posted 18 November 2010 - 04:35 AM

Incomprehensible!
Was This Post Helpful? 0
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: File Handling... Greetings everyone!

Posted 18 November 2010 - 04:37 AM

How about you write some code that is your best try at doing what ever it is you are trying to do (your description is useless) and share it with us along with a serious attempt to describe whatever it is your program requirements are.

If you take 15 minutes to formulate a question that strangers will be able to understand you will get useful help far quicker than wasting your energy on silliness like "hehehe....".

Time to step up and deal with your programming issues with a serious attitude and approach. Kiddy time won't work if you want to be treated seriously. Kiddy time will result in you being treated as a silly little kid who will be ignored. You are better than that so time to live up to your potential.
Was This Post Helpful? 0
  • +
  • -

#4 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: File Handling... Greetings everyone!

Posted 18 November 2010 - 04:40 AM

Sorry.

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>

char Name[40];
int ID = 0;
char Gender;
float Amt;
FILE *sfile;
char Target[40];
int Found = 0;
FILE *temp;


void adding(void)
{
	if (( sfile = fopen("f:\\stud.dat", "a+")) == NULL)
		ID = 1;
	else
	{
		do{
			fscanf(sfile, "%i%[^/]%*c%c%f%*c", &ID, Name, &Gender, &Amt);
		}while(!feof(sfile));
		ID += 1;
	}

	printf("Student ID: %04i", ID);
	printf("\nName:       ");
	fflush(stdin);
	gets(Name);
	printf("Gender:     ");
	scanf(" %c", &Gender);
	printf("Amount:     ");
	scanf("%f", &Amt);
	printf("\nRecord saved!");
	fprintf(sfile, "%i%s/%c%.2f\n", ID, Name, Gender, Amt);
	fclose(sfile);
}

void displaying(void)
{
	if((sfile = fopen("f:\\stud.dat", "a+")) == NULL)
		printf("File Empty!");
	else
	{
		while(!feof(sfile))
		{
			fscanf(sfile, "%i%[^/]%*c%c%f%*c", &ID, Name, &Gender, &Amt);
			if(feof(sfile))
				break;
			printf("%04i %s %c %.2f\n", ID, Name, Gender, Amt);
		}
	}
	printf("\nEnd of file, press any key to exit.");
	fclose(sfile);
}

void searching(void)
{


	if ((sfile = fopen("f:\\stud.dat", "r")) == NULL)
		printf("File Empty!");
	else
	{
		printf("Enter Name to search: ");
		fflush(stdin);
		gets(Target);
		while(!feof(sfile) && Found == 0)
		{
			fscanf(sfile, "%i%[^/]%*c%c%f%*c", &ID, Name, &Gender, &Amt);
			if(strcmp(Target, Name) == 0)
			Found = 1;
		}
		if (Found)
		{
			printf("ID:\t%04i\n", ID);
			printf("Name: %s\n", Name);
			if(toupper(Gender) == 'F')
				printf("Gender: Female\n");
			else
				printf("Gender: Male\n");
			printf("Amount: %.2f\n", Amt);
		}
	}
		fclose(sfile);
}

void deleting(void)
{
	temp = fopen("f:\\temp.dat", "w");

	if((sfile = fopen("f:\\stud.dat", "r")) == NULL)
		printf("File Empty!");
	else
	{
		printf("Enter Name to delete: ");
		fflush(stdin);
		gets(Target);
		while(!feof(sfile))
		{
			fscanf(sfile, "%i%[^/]%*c%c%f%*c", &ID, Name, &Gender, &Amt);
			if(feof(sfile))
				break;
			if(strcmp(Target, Name) != 0)
				fprintf(temp, "%i%s/%c%f\n", ID, Name, Gender, Amt);
			else
			{
				Found = 1;
				printf("ID:\t%04i\n", ID);
				printf("Name: %s\n", Name);
				if(toupper(Gender) == 'F')
					printf("Gender:\tFemale\n");
				else
					printf("Gender:\tMale\n");
				printf("Amount: %.2f\n", Amt);
			}
		}

		if (!Found)
			printf("Record not found!\n");
	}
		printf("\nRecord deleted.");
		fclose(sfile);
		fclose(temp);
		remove("f:\\stud.dat");
		rename("f:\\temp.dat", "f:\\stud.dat");
}
void main(void)
{
	char choice;
do{
	clrscr();

	printf("Menu\n\n");
	printf("A - Adding\n");
	printf("B - Display\n");
	printf("C - Search\n");
	printf("D - Delete\n");
	printf("E - Exit\n\n\n");
	printf("Enter your choice: ");
	scanf(" %c", &choice);

	switch(toupper(choice))
	{
	case 'A':
		clrscr();
		adding();
		getch();
		break;

	case 'B':
		clrscr();
		displaying();
		getch();
		break;
	case 'C':
		clrscr();
		searching();
		getch();
		break;
	case 'D':
		clrscr();
		deleting();
		getch();
		break;

	}
}while(toupper(choice) != 'E');

}


i made a menu for the add, delete, search and display... any clues for editing?
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,395
  • Joined: 23-August 08

Re: File Handling... Greetings everyone!

Posted 18 November 2010 - 04:49 AM

fflush(stdin);
gets(Target);


What school in the United States is teaching you THAT crap? I want to send them a strongly-worded letter.
Was This Post Helpful? 0
  • +
  • -

#6 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: File Handling... Greetings everyone!

Posted 18 November 2010 - 04:53 AM

Im sorry sir. But I cannot do anything about it. Pls. consider my posts. The posts that I usually post is made up of these codes. Sorry.
Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: File Handling... Greetings everyone!

Posted 18 November 2010 - 04:53 AM

What are the requirements for editing?

Do you need to keep the record in its current location in the file or can you move it?

Assuming you can change the position then an edit() function could use the search() function to find a record and delete() to remove it from the file. Then change whatever needs to be changed and use a version of the adding() function to add it to the end of the file.

Since you have all those functions written you should make as much use of them as you can. As you can see you may need to generalise those functions a bit more to make them multi-purpose by getting some of the user input stuff out of there and holding it elsewhere.

Sound achieveable?

EDIT
As I thought about my posting a bit I realised I probably wasn't being very clear.
If you are allowed to change the signatures of your functions then here's an example of what you could change to make them more generally useful.
You could change search so that it accepted a C-string of the name to search for.
That would mean you can use it to search for any string you get anywhere in the program.
If you wanted to maintain the ability to call it as you currently are then you could test to see if the passed in string was empty and if it was empty you could use your existing user entry code. If the passed in string was not empty then you skip past the user entry stuff and go straight to the search.

I would remove the code that prints the details you found in the search and put that in a different function.
printRecord() maybe.

Anyway, I am not sure how much you are allowed to change so maybe what I am saying is a pointless waste of time. Don't be afraid to say so if that's the case. I am guessing without knowing the requirements and won't be at all offended to be told my guesses are way off target.

This post has been edited by janotte: 18 November 2010 - 05:12 AM

Was This Post Helpful? 1
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,395
  • Joined: 23-August 08

Re: File Handling... Greetings everyone!

Posted 18 November 2010 - 05:04 AM

Three words: Array of Structs
Was This Post Helpful? 0
  • +
  • -

#9 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: File Handling... Greetings everyone!

Posted 18 November 2010 - 05:15 AM

im looking forward for your second post. im trying to solve it based on your instructions. I hope. Thanks.
Was This Post Helpful? 1
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: File Handling... Greetings everyone!

Posted 18 November 2010 - 05:20 AM

View PostJackOfAllTrades, on 18 November 2010 - 09:04 PM, said:

Three words: Array of Structs


Well yes that would be the real answer.
But a teacher who teaches students to use 'gets()' probably doesn't know what a struct is, wouldn't be surprised if arrays are a mystery to this teacher.

issyl
Make me happily surprised by saying I am wrong and you have done some work on arrays and structs in class.
If you have then JackOfAllTrades is completely right and a major re-write to use them is a far better idea than what you have been doing.

The big issue, as I am sure you can tell, is that we aren't completely sure what you are and aren't allowed to do and what languages features you can and can't use. You don't need to tell us everything but do give us feedback on ideas put forward that you can't use so we don't suggest them again.

This post has been edited by janotte: 18 November 2010 - 05:21 AM

Was This Post Helpful? 0
  • +
  • -

#11 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: File Handling... Greetings everyone!

Posted 18 November 2010 - 05:35 AM

Thank you for your deep consideration. We only encountered functions, looping, conditionals, arrays, strings. We are in a Turbo C++ environment with a little bit cout, cin as introduction to c+. But still focusing in c language for this semester. Thank you for your time. Anyway, sorry for wasting your time. Thank you for visiting my post.
Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: File Handling... Greetings everyone!

Posted 18 November 2010 - 06:46 AM

No time wasting here that I can see. Don't be too hard on yourself.

So it seems structs are off the table. That's fine. Forget that for the moment.
You have that joy to come in your learning journey.

Have you something from the thread so far?

Do you feel like you can move forward or are you still stuck for some ideas that you can try?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1