LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-27-2015, 06:35 AM   #1
NoWeDoR
Member
 
Registered: Jun 2015
Posts: 32

Rep: Reputation: Disabled
String Assigment Issue


Hello friends,

At the following program I enter "1" from main menu and after run the program I get a record from user.
Later, I want to assign NULL or 0 instead of this name, surname and number using 5th function (entering 5 from main menu). However I can't succeed.
I've tried a lot of syntax type but they haven't worked. (You can see the comment lines what I have written in the deletE() function).
How can I solve this problem, How can I assign or show "NULL or 0" instead of this record (instead of name, surname, number) which is got from user ?


Code:
#include <stdlib.h>      // "stdlib" library contains of exit() function
#include <malloc.h>     // "malloc" library contains of malloc() function
#include <Windows.h>   // "Windows" library contains of Sleep() function which waits the system as you want
#include <io.h>       // "io" library contains of filelength() function
#include <string.h>  // "string" library contains of strlen() function
#include <stdio.h>  // "stdio" library contains of other functions which hasn't been mentioned

typedef struct personKnowledge   // Structe and it's elements are being defined
{
	char name[32];
	char surname[32];
	char number[32];
};

FILE *ptrFILE, *ptrFILE1;   // Variables are being defined
long int recordLength, totalRecordLength, location;
int choice, number, totalRecordNumber, i;
static int counter = 0;

int menu();   // Functions are being defined
void newRecord();
void display();
void update();
void add();
void deletE();

int main()   // Program is being initialiezed 
{
	menu();
	return 0;
}

int menu()   // Options are being presented to user 
{
	do
	{
		printf("\n\t\t --- Phone Book Program ---");
		printf("\n\n\t\t 1) Open file and record someone");
		printf("\n\n\t\t 2) Display person knowledge");
		printf("\n\n\t\t 3) Update person knowledge");
		printf("\n\n\t\t 4) Add person to list");
		printf("\n\n\t\t 5) Delete someone");
		printf("\n\n\t\t 6) Exit");
		printf("\n\n\nEnter your choice: ");
		choice = 0;
		scanf("d", &choice);
		fflush(stdin);
		switch (choice)
		{
		case 1:
		{
			newRecord();
			break;
		}
		case 2:
		{
			display();
			break;
		}
		case 3:
		{
			break;
		}
		case 4:
		{
			break;
		}
		case 5:
		{
			deletE();
			break;
		}
		case 6:
		{
			printf("\nWorking has been completed.\n");
			return 0;
		}
		default:
		{
			printf("\nWrong entry! The program has been terminated.\n");
			break;
		}
		}
	} while (choice >= 1 && choice <= 6);
}

void newRecord()   // This function opens a file and records one person in file
{
	if (counter > 0)
	{
		system("cls");   // Screen is being cleaned
		Sleep(350);     // Sleep functions waits user for a while
		printf("\nYou have already entered '1' and opened a file. To add a person please enter '4'\n");
		return;
	}
	else
	{
		if ((ptrFILE = fopen("Phone Book.dat", "wb")) == NULL)   // wb is binary writing mode
		{
			printf("The file couldn't be opened\n");
			exit(EXIT_SUCCESS);
		}
		system("cls");
		Sleep(350);
		personKnowledge *p;   // p = (Person)
		p = (personKnowledge *)malloc(sizeof(personKnowledge));   // Memory is being allocated
		fflush(stdin);   // Cache memory is being cleaned
		recordLength = sizeof(*p);
		printf("\n\Express person name: ");   // User is entering the person's knowledge and they are being saved in file
		fgets(p->name, sizeof(p->name), stdin);
		size_t wordLength = strlen(p->name);  // "size_t is unsigned integer type"
		if (wordLength > 0 && p->name[wordLength - 1] == '\n')   // This if idiom has been used for sentence seperation
		{
			p->name[--wordLength] = '\0';
		}
		printf("Express %s's surname: ", p->name);
		fgets(p->surname, sizeof(p->surname), stdin);
		printf("Express %s's number: ", p->name);
		fgets(p->number, sizeof(p->number), stdin);
		fwrite(&(*p), recordLength, 1, ptrFILE);   // Knowledge which has been got from user is being saved in file processionally
		printf("\nPlease wait, information is saving to file..\n");
		Sleep(750);
		printf("*-* Saving operation has been completed succesfully. *-*\n");
		free(p);   // Allocated part of memory is being released
	}
	fclose(ptrFILE);   // File is being closed
	counter++;
}

void display()   // If there is person knowledge which is searched in file, this function reads it and prints on the screen
{
	if ((ptrFILE = fopen("Phone Book.dat", "rb")) == NULL)   // rb is binary reading mode
	{
		printf("The file couldn't be opened\n");
		exit(EXIT_SUCCESS);
	}
	system("cls");
	Sleep(350);
	personKnowledge *s;   // s = (Searching)
	s = (personKnowledge *)malloc(sizeof(personKnowledge));
	recordLength = sizeof(*s);   // Necessary location calculations are being done and person's knowledge is being displayed
	totalRecordLength = filelength(fileno(ptrFILE));   // The equalities explains what the purpose is from 137th line to 145th line 
	totalRecordNumber = totalRecordLength / recordLength;
	printf("\n\nExpress person record number which you search: ");
	number = -791673918435;
	scanf("%d", &number);
	fflush(stdin);
	printf("\n*-* Person knowledge which you search *-*\n");
	Sleep(750);
	location = (number - 1)*(recordLength);
	fseek(ptrFILE, location, SEEK_SET);   // The cursor locates place which is searched with fseek() function
	if (fread(&(*s), recordLength, 1, ptrFILE) != 0 && number > 0)  // If there is knowledge in that location and numbeer is greater than 0
	{
		printf("Name: %s\n", s->name);
		printf("Surname: %s", s->surname);
		printf("Number: %s", s->number);
	}
	else
	{
		printf("There is no record like this.\n");
		return;
	}
	free(s);
	fclose(ptrFILE);
}


void deletE()
{
	if ((ptrFILE = fopen("Phone Book.dat", "rb+")) == NULL)
	{
		printf("The file couldn't be opened\n");
		exit(EXIT_SUCCESS);
	}
	system("cls");
	Sleep(350);
	personKnowledge *del;   // del = (Deleting)
	del = (personKnowledge *)malloc(sizeof(personKnowledge));
	fflush(stdin);
	recordLength = sizeof(*del);
	totalRecordLength = filelength(fileno(ptrFILE));
	totalRecordNumber = totalRecordLength / recordLength;
	printf("\nExpress person number which you want to delete: ");
	scanf("%d", &number);
	fflush(stdin);
	fseek(ptrFILE, (number-1)*recordLength, SEEK_SET);
	if (fread(&(*del), recordLength, 1, ptrFILE) != 0 && number > 0)  // If there is knowledge in that location and number is greater than 0
	{
		Sleep(350);
		printf("*-* The record which is cleaned *-* \n", number);
		printf("Name: %s\n", del->name);
		printf("Surname: %s", del->surname);
		printf("Number: %s", del->number);
		// del->name = "";         <<<<<<<<<<<<<<<<<< :(      ??   ??   ???   ?
		// del->name = {};          <<<<<<<<<<<<<<<<<< :(             ?  ?  ??   ?? ? 
		// &(*del).name = "";         <<<<<<<<<<<<<<<<<< :(               ?   ? ?   ?? ?  ? ??
		// &(*del).name = {};           <<<<<<<<<<<<<<<<<< :(     ??  ??  ?     ??  
		// strcpy(del->name, "");      <<<<<<<<<<<<<<<<<< :(                 ???   ?              ?? ?? ? 
		// del->name = NULL;           <<<<<<<<<<<<<<<<<< :(                   ???   ?    ?? ?  ? ?
		// strcpy(del->name, NULL);     <<<<<<<<<<<<<<<<<< :(     ??  ??  ?     ?? 
	}
	else
	{
		Sleep(350);
		printf("There is no record like this.\n");
		return;
	}
	fclose(ptrFILE);
	free(del);
}

Last edited by NoWeDoR; 07-27-2015 at 07:43 AM.
 
Old 07-27-2015, 06:55 AM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Provide the shortest possible program that presents your issue because otherwise many people will just not bother read the code since its too long. Furthermore, in the process of trying to find the shortest code that still contains the bug youre experiencing, youre likely to find solution yourself.

PS. When you write Functions are being defined, functions are actually declared, not defined.
 
Old 07-27-2015, 07:48 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Per Mina86's comment I do agree, also offer this link as a guide to assist in asking effective LQ questions.

However, a quick look, but not running the code. Entering 5 will delete a record. OK, you are managing this phone book in a file, and that is to say, not in memory. And that's fine. Just for me it tells me the "style" of programming I'd personally use to design my solution.

What I best can say here is for the file based solution, something I've practiced is to open the original file, usually as read-only, and open a new file, as write only. When you use something like fgets() it reads the input file line by line, until you get NULL which means you've hit the end of file. Therefore when I edit a file to change or delete an entry, I'll read each line, and determine if it's the one I want. If it's not the line I want, I just write it to the output file. If it is the line I want, I either modify that line and write it to the output file, or in your case if it is the line to be deleted, the I just don't write it to the output file and continue my read-write loop. After it's all done I close both files and if I choose to rename the newly written file to be the original input filename, I will do that. In fact the rename is a very typical thing for me to do in this case because you usually want to use the same filename over and over. Just in debugging, it's helpful to leave that step out and just create a new differently named output file so that you validate you have gotten your program working properly.

Note that this becomes more difficult if you have multi line records, or if some lines of a record are prior to the point where you'd identify the record, or if the multi line records could be different numbers of lines, not all the same size. You just have to write more code to manage that, but I'd recommend for now if you can keep it simple, then do so until you have debugged the basics of your file management functions.

I'm proposing something simple. There are ways to read the entire file into memory all at once and much more efficient. It all depends on your use case as well as the file sizes involved and available memory.

Good work creating separate functions for individual operations. That is the right track to follow.

Last edited by rtmistler; 07-27-2015 at 07:49 AM.
 
1 members found this post helpful.
Old 07-27-2015, 07:51 AM   #4
NoWeDoR
Member
 
Registered: Jun 2015
Posts: 32

Original Poster
Rep: Reputation: Disabled
You don't need to read all of codes indeed. I have just a small problem as I said.
If you do what I said you can understand what my problem is. hg firstly you should examine deletE() function firstly.


// del->name = ""; <<<<<<<<<<<<<<<<<< ?? ?? ??? ?
// del->name = {}; <<<<<<<<<<<<<<<<<< ? ? ?? ?? ?
// &(*del).name = ""; <<<<<<<<<<<<<<<<<< ? ? ? ?? ? ? ??
// &(*del).name = {}; <<<<<<<<<<<<<<<<<< ?? ?? ? ??
// strcpy(del->name, ""); <<<<<<<<<<<<<<<<<< ??? ? ?? ?? ?
// del->name = NULL; <<<<<<<<<<<<<<<<<< ??? ? ?? ? ? ?
// strcpy(del->name, NULL);

Instead of this lines, I need a different method which provides me to assign NULL or 0 to name, surname and number which I got from user at first
 
Old 07-27-2015, 08:01 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
memset(array-address, 0, sizeof(array))
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] int to string conversion issue rohaanembedded Programming 5 05-30-2013 06:12 AM
Issue String concatenating in output file daredevil_1981 Linux - Newbie 4 10-14-2011 12:56 PM
[SOLVED] Issue trying to grep a string, but keep a similar string Supp0rtLinux Linux - Software 7 10-04-2011 06:35 PM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
perl string match issue knockout_artist Linux - Newbie 11 01-13-2009 05:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration