LinuxQuestions.org
Help answer threads with 0 replies.
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 04-24-2015, 01:38 PM   #1
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Rep: Reputation: 10
problem writing over text in file


I have the following program:

Code:
#include "hotel.h"

void getname();

int
addguest(void)
{
	int roomno;
	off_t offset;

	if ((roomno = findfree()) >= 1) {
		getname();
		offset = (roomno - 1) * NAMELENGTH;
		if (lseek(infile, offset, SEEK_SET) == -1) {
			perror("lseek");
			exit(1);
		}
		write(infile,namebuf,NAMELENGTH);
	} else 
		return -1;
	return roomno;
}

void
getname()
{
	int length = NAMELENGTH + 1;


	while (length >= NAMELENGTH) {
		printf("Enter Resident: ");
		scanf("s", namebuf);
		length = strlen(namebuf);
		if (length >= NAMELENGTH) {
			puts("Name is too long");
			sleep(3);
		}
	}

	/* fill buffer w/ zeros */
	for (length; length < NAMELENGTH; length++)
		namebuf[length] = ' ';
	namebuf[NAMELENGTH-1] = '\0';

	return;
}

#include "hotel.h"

const char *empty = "EMPTY               ";
char namebuf[NAMELENGTH];

int
main(int argc, char **argv)
{
	int j;
	char *p;
	infile = openfile();

	
/*	printf("Get Occupier 1:\nFree Room 2:\nAddGuest 3:\nFindFree 4:\n\n\nChoose:	*/

	if ( (j = addguest()) == -1) 
		;
	else
		printf("%s was added to room %d\n", namebuf, j);

	return 0;
}
adduser is supposed to write over the text in a line that reads EMPTY. The problem is it writes the text from namebuf pushing the EMPTY to the right. I wrote a test program that didn't do that; it wrote over the text.

Code:
ateststringralll   b
jjjjjjjjjjjjjjjjjjjj
aldkfjaldkjaldfjalk 
sfkjalkfjalkdfjaj  4
alinetotest         
DLKSJFladksjfklfdjaj
EMPTY
jlkdfldkjfdjfjjjjjjj
ldfjksllllllllllllll
EMPTY
Each line is 20 chars long including the EMPTY which has blanks tacked on the end.

I hope this is enough info to help.

Last edited by doughyi8u; 04-24-2015 at 01:44 PM.
 
Old 04-24-2015, 01:54 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You cannot 'insert' data into a file (let's not mention ISAM, VSAM etc), so the problem lies elsewhere.
Add debug-prints, eg:

Code:
printf ("Debug: index=%d, index-1=%d, elem_size=%d, offset=%lld\n",
	roomno, roomno-1, (int)NAMELENGTH, (long long)offset);

Last edited by NevemTeve; 04-24-2015 at 11:58 PM.
 
Old 04-24-2015, 01:59 PM   #3
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
so it does write over? The test prog I wrote did exactly that.
 
Old 04-24-2015, 03:12 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,722

Rep: Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916
Did you post your the entire program?

Where is your function that actually opens the file i.e openfile?

infile is your file pointer but is not defined.

The big hint is understanding local vs global variables.
 
Old 04-24-2015, 03:34 PM   #5
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
here's the openfile function that is in a seperate file:
Code:
#include "hotel.h"

int infile;

int openfile()
{
	if ( (infile = open("residents", O_RDWR)) == -1) {
		perror("open file");
		exit(1);
	} else
		return infile;
}
And here's the main function also in a seperate file:
Code:
#include "hotel.h"

const char *empty = "EMPTY               ";
char namebuf[NAMELENGTH];

int
main(int argc, char **argv)
{
	int j;
	char *p;
	infile = openfile();

	
/*	printf("Get Occupier 1:\nFree Room 2:\nAddGuest 3:\nFindFree 4:\n\n\nChoose:	*/

	if ( (j = addguest()) == -1) 
		;
	else
		printf("%s was added to room %d\n", namebuf, j);

	return 0;
}
And here's the hotel.h:
Code:
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

#define NAMELENGTH 21
#define NROOMS 10

#define DBUG

char *getoccupier(int);
int openfile();
int findfree();
int freeroom(int);
int isempty(); 
int addguest(); 

extern int infile;
extern char namebuf[NAMELENGTH];
extern const char *empty;
 
Old 04-24-2015, 04:01 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,722

Rep: Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916
You forgot to post the findfree function.

Last edited by michaelk; 04-24-2015 at 04:07 PM.
 
Old 04-24-2015, 04:50 PM   #7
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
Code:
#include "hotel.h" 

int
findfree()
{
	off_t offset;
	int j;
	ssize_t nread;

	for(j = 1; j <= NROOMS; j++)
	{
		offset = (j-1) * NAMELENGTH;
		if ( lseek(infile, offset, SEEK_SET) == -1) {
			perror("lseek");
			exit(1);
		} else {
			if ( (nread = read(infile, namebuf, NAMELENGTH)) <= 0) {
				perror("read?");
				exit(1);
			} else 
				namebuf[nread-1] = '\0';
		}
		if ( (isempty()) == 1) 
			return(j);

	}
	puts("No empty rooms");
	return -1;
}



int
isempty()
{
	if ( (strncmp(namebuf, empty, strlen(empty))) == 0) 
		return 1;
	else 
		return -1;
}

Last edited by doughyi8u; 04-24-2015 at 04:54 PM.
 
Old 04-24-2015, 09:01 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,722

Rep: Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916
Actually each line is 21 bytes. So it appears that the name is being tacked on to the end but actually the line feed is being deleted. write(infile,namebuf,NAMELENGTH); writes 21 bytes, namebuf[21] is never set and the 21 byte of the line is a \n.

I will give you a hint that the getname function has a syntax error somewhere in the code you posted.

Last edited by michaelk; 04-24-2015 at 09:02 PM.
 
Old 04-24-2015, 09:33 PM   #9
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
I can't find the error in getname()
 
Old 04-24-2015, 09:40 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,722

Rep: Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916
One more hint. With the posted code namebuf is never actually being assigned the name entered.
 
Old 04-24-2015, 09:51 PM   #11
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
is it in the while or for loop? I still can't find it.
 
Old 04-24-2015, 09:53 PM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,722

Rep: Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916
Is the residence name actually being written to the file?
Check the syntax for scanf("s", namebuf);
 
Old 04-24-2015, 09:58 PM   #13
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
it is being written to file but pushes what was on that line to the write extending the limit of 20 chars
the scanf("s",namebuf) function was a typo. the program reads the way it should be: scanf("s",namebuf);
 
Old 04-24-2015, 10:02 PM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,722

Rep: Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916Reputation: 5916
Nope.
http://www.tutorialspoint.com/c_stan...tion_scanf.htm
 
Old 04-24-2015, 10:10 PM   #15
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
I used the syntax scanf("s",&namebuf) and get the same results as w/o the & in front of the buffer
 
  


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] [C++] Writing double value to a text file Aquarius_Girl Programming 9 06-03-2011 01:28 PM
writing text to a file gazz1982 Linux - Newbie 1 03-18-2010 06:16 PM
I need HELP Writing text to a FILE ravi2ray Linux - Newbie 4 10-29-2008 10:10 PM
Problem with writing in a text file in C manolakis Programming 5 12-06-2006 10:18 PM
Writing to a text file vb.net mrobertson Programming 5 08-04-2005 12:37 PM

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

All times are GMT -5. The time now is 08:51 AM.

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