5 Replies - 364 Views - Last Post: 28 November 2010 - 08:58 PM Rate Topic: -----

#1 issyl  Icon User is offline

  • D.I.C Head

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

Edit File

Posted 28 November 2010 - 06:56 PM

PLSSS! I cannot solve this now!! I've been working for almost 5 straight hours from 1-5am but still do not know what to do! pls guys... whats the problem in my edit program? i try to follow the link that was give by jim, by using infile>> name, tried to use fgets(name, 50, infile) w/ matching /n remover(if(name[strlen(name)-1] = '\n')...

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <iostream.h>
#define input "f:\\tcpp\\bin\\files\\input.txt"
#define tempo "f:\\tcpp\\bin\\files\\tempo.txt"

void menu(void)
{
	cout << "Menu\n\n";
	cout << "A - Add\n";
	cout << "D - Delete\n";
	cout << "I - Display\n";
	cout << "E - Edit\n";
	cout << "S - Search\n";
	cout << "X - Exit\n\n\n";
	cout << "Enter your choice: ";
}

void add(void)
{
	FILE *infile;
	char Name[50], Target[50];
	int ID = 0;
	float Amount;
	char Gender;

	if((infile = fopen(input, "a+")) == NULL)
		ID = 1;
	else
	{
		do{
			fscanf(infile, "%i %s %c %f", &ID, Name, &Gender, &Amount);
		}while(!feof(infile));
		ID += 1;
	}
	cout << "ID  :    " << ID << endl;
	cout << "Name:    ";
	cin >> Name;
	cout << "Gender:  ";
	cin >> Gender;
	cout << "Amount:  ";
	cin >> Amount;

	fprintf(infile, "%i %s %c %f\n", ID, Name, Gender, Amount);
	fclose(infile);
}

void deletes(void)
{
	FILE *infile;
	FILE *tfile;
	char Name[50], Target[50];
	int ID = 0, found = 0;
	float Amount;
	char Gender;
	tfile = fopen(tempo, "w");

	if((infile = fopen(input, "r")) == NULL)
		cout << "File Empty!" << endl;
	else
	{
		cout <<"Enter Name to delete: ";
		cin >> Target;
		while(!feof(infile))
		{
			fscanf(infile, "%i %s %c %f", &ID, Name, &Gender, &Amount);
			if(feof(infile))
				break;
			if(strcmp(Target, Name) != 0)
				fprintf(tfile, "%i %s %c %f\n", ID, Name, Gender, Amount);
			else
			{
				found = 1;
				cout << "ID:   " << ID << endl;
				cout << "Name: " << Name << endl;
				if(toupper(Gender) == 'F')
					cout << "Gender:\tFemale\n";
				else
					cout << "Gender:\tMale\n";
				cout << "Amount: " << Amount << endl;
			}
		}

		if (!found)
			cout << "Record not found!\n";
	}
		cout << "\nRecord deleted.";
		fclose(infile);
		fclose(tfile);
		remove(input);
		rename(tempo, input);
}

void display(void)
{
	FILE *infile;
	char Name[50], Target[50];
	int ID = 0;
	float Amount;
	char Gender;
	if((infile = fopen(input, "a+")) == NULL)
		cout << "File Empty!";
	else
	{
		while(!feof(infile))
		{
			fscanf(infile, "%i %s %c %f", &ID, Name, &Gender, &Amount);
			if(feof(infile))
				break;
			cout << ID << " " << Name << " " << Gender << " " << Amount << endl;
		}
	}
	cout << "\nEnd of file, press any key to exit." << endl;
	fclose(infile);
}

void search(void)
{
	FILE *infile;
	char Name[50], Target[50];
	int ID = 0, found = 0;
	float Amount;
	char Gender;

	if ((infile = fopen(input, "r+")) == NULL)
		cout << "File Empty!";
	else
	{
		cout << "Enter Name to search: ";
		cin >> Target;
		while(found == 0 && !feof(infile))
		{
			fscanf(infile, "%i %s %c %f", &ID, Name, &Gender, &Amount);
			if(strcmp(Target, Name) == 0)
			found = 1;
		}
		if(found)
		{
			cout << "ID:   " << ID << endl;
			cout << "Name: " << Name << endl;
			if(toupper(Gender) == 'F')
				cout << "Gender: Female\n";
			else
				cout << "Gender: Male\n";
			cout << "Amount: " << Amount << endl;
		}
	}
	fclose(infile);
}

void edit(void)
{
	FILE *infile;
	FILE *tfile;
	char Name[50], Target[50];
	int ID = 0, found = 0, back;
	float Amount;
	char Gender;
	tfile = fopen(tempo, "a+");
	if ((infile = fopen(input, "r+")) == NULL)
		cout << "File Empty!";
	else
	{
		cout << "Enter Name to edit: ";
		cin >> Target;
		while(found == 0)
		{
			while(!feof(infile))
/* I tried to replace !feof(infile) as mentioned above */
			{
				fscanf(infile, "%i %s %c %f", &ID, Name, &Gender, &Amount);
				if(strcmp(Target, Name) != 0)
					fprintf(tfile, "%i %s %c %f\n", ID, Name, Gender, Amount);
				else
				{
					back = ID;
					found = 1;
					cout << "ID:   " << ID << endl;
					cout << "Name: " << Name << endl;
					if(toupper(Gender) == 'F')
						cout << "Gender: Female\n";
					else
						cout << "Gender: Male\n";
					cout << "Amount: " << Amount << endl;
				}
			}
		}
		if(found)
		{

			ID = back;
			cout << "Enter new information\n\n";
			cout << "ID  :    " << ID << endl;
			cout << "Name:    ";
			cin >> Name;
			cout << "Gender:  ";
			cin >> Gender;
			cout << "Amount:  ";
			cin >> Amount;
			cout << "Record edited!" << endl;

			fprintf(tfile, "%i %s %c %f\n", ID, Name, Gender, Amount);
		}
	}
	fclose(infile);
	fclose(tfile);
	remove(input);
	rename(tempo, input);

}
int main(void)
{
	char choice;
do{
	clrscr();
	menu();
	scanf("%c", &choice);
	switch(toupper(choice))
	{
	case 'A':
		clrscr();
		add();
		getch();
		break;
	case 'D':
		clrscr();
		deletes();
		getch();
		break;
	case 'I':
		clrscr();
		display();
		getch();
		break;
	case 'E':
		clrscr();
		edit();
		getch();
		break;
	case 'S':
		clrscr();
		search();
		getch();
		break;
	}
}while(toupper(choice) != 'X');
	return 0;
}




Plssss

Is This A Good Question/Topic? 0
  • +

Replies To: Edit File

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,304
  • Joined: 25-December 09

Re: Edit File

Posted 28 November 2010 - 07:08 PM

Please don't start a new post for the same question that is in the previous post.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,397
  • Joined: 19-February 09

Re: Edit File

Posted 28 November 2010 - 07:25 PM

Hi, you have while(found == 0) this is unnecessary, because you want to copy the whole of the input file over to the temp file.

  while(found == 0)
  {
    while(!feof(infile))


Was This Post Helpful? 1
  • +
  • -

#4 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 121
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Edit File

Posted 28 November 2010 - 07:31 PM

What errors are you recieving? What behavior are you expecting from your program, which you are failing to see now? You will

Nevermind, I just realized there is a whole other topic about this same issue.
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,397
  • Joined: 19-February 09

Re: Edit File

Posted 28 November 2010 - 07:41 PM

If a name that is not in the file is entered this will result in an endless loop.
while(found == 0)



The program produces an extra entry, because the last newline is not accounted for :-
  // add \n in format string
  fscanf(infile, "%i %s %c %f\n", &ID, Name, &Gender, &Amount);



If the newline is missing, the file pointer will not be at the end of the file.

This post has been edited by #define: 28 November 2010 - 07:44 PM

Was This Post Helpful? 1
  • +
  • -

#6 issyl  Icon User is offline

  • D.I.C Head

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

Re: Edit File

Posted 28 November 2010 - 08:58 PM

#DEFINE THANK YOU VERY MUCH.... i almost cried as i tried what you have said.... i am happy that you guys are willing to help someone....at last ... #define #define "THANK YOU VERY MUCH!!! "
Was This Post Helpful? -1
  • +
  • -

Page 1 of 1