6 Replies - 313 Views - Last Post: 08 June 2012 - 07:28 AM Rate Topic: ***-- 2 Votes

#1 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

File Read Operation

Posted 08 June 2012 - 06:32 AM

Wanted to write a small application that would store some data about some purchase in a text file. Here's the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define SIZE 365
void ctext (char *s);
int checkdata (void);
void crdbase (void);
int main(int argc, char *argv[])
{
	struct list{
		char item[10];
		int cost;
		int month;
	} food[SIZE];
	char ti[26],fread;
	char end[17] = {' ', ' ',' ', ' ','-', '-','-', 'E','N', 'D',' ', '-','-', '-',' ', '\n'};
	time_t now;
	int ch, ent, check, i;
	FILE *ptr;
	time(&now);
	strcpy(ti, ctime(&now));
	system("title FOOD PRICE LIST APPLICATION");
	ctext("----------  FOOD APPLICATION ----------\n\n");
	ctext(ti); 
	puts("\n");
	check=checkdata();
	if(check==1){
		ctext(" ---- DATABASE DOES NOT EXIST ----\n\n");
		ctext(" Press 1 to Create Database or Any Key to Exit: ");
		scanf("%d", &ch);
			if(ch==1)
				crdbase();
			else
				system("cls");
				fflush(stdin);
				system("exit");
	}	
	getchar();
	ptr=fopen("dbase.txt", "a+");
	system("cls");
	ctext("------- DATA ENTRY MODE ------\n\n");
	ctext(" Enter Number Of Entries:");
	scanf("%d", &ent);
	fseek(ptr, -1L, 2);
	fprintf(ptr, "%s", ti);
	for(i=0; i<ent; i++){
	puts("Enter Food Name: ");
	scanf("%s", food[i].item);
	fprintf(ptr, "%s ", food[i].item);
	puts("\nEnter Food Cost: \n");
	scanf("%d", &food[i].cost);
	fflush(stdin);
	fprintf(ptr, "%d ", food[i].cost);
	puts("\nEnter Month: \n");
	scanf("%d", &food[i].month);
	fprintf(ptr, "%d\n", food[i].month);
	fflush(stdin);
	}
	fprintf(ptr, "%s", end);
	fclose(ptr);
	system("cls");
	ptr=fopen("dbase.txt", "r");
	while((fread=getc(ptr)!=EOF)){
		printf("%c", fread);
	}
	fclose(ptr);
	system("pause");
	return 0;
}
int checkdata (void)
{
	FILE *fpc;
	if((fpc=fopen("dbase.txt", "r"))==NULL)
		return (1);
	else 
		return (0);
}
void ctext(char* s)
{

	int len, i, pos;
	len=strlen(s);
	pos=(int)((80-len)/2);
	for (i=0; i!=pos; i++)
	printf(" ");
	printf("%s", s);
}
void crdbase (void)
{
	FILE *fcr;
	fcr=fopen("dbase.txt", "w");
	fclose(fcr);
	puts("\n");
	if((fcr=fopen("dbase.txt", "r"))!=NULL)		
		ctext("Database created successfully ! \n\n");
	else
		ctext("Failed to create Database. Contact developer\n\n");
}



However the file read function isn't working as expected. It's printing gibberish in the screen. Where have I mistaken? Thanks !

Is This A Good Question/Topic? 0
  • +

Replies To: File Read Operation

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,529
  • Joined: 05-May 12

Re: File Read Operation

Posted 08 June 2012 - 06:55 AM

Check your parentheses on line 64:
    while((fread=getc(ptr)!=EOF)){


Was This Post Helpful? 1
  • +
  • -

#3 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: File Read Operation

Posted 08 June 2012 - 07:14 AM

View PostSkydiver, on 08 June 2012 - 06:55 AM, said:

Check your parentheses on line 64:
    while((fread=getc(ptr)!=EOF)){



Thanks a lot. Silly mistake !
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: File Read Operation

Posted 08 June 2012 - 07:15 AM

Also you should not use fread as a variable name. There is a standard function with that name already defined. And using fflush() on input streams is not defined in the standard, thus using it on an input stream is undefined behavior. Plus your code formatting needs serious attention. You need to find an indentation style you like and use it consistently.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: File Read Operation

Posted 08 June 2012 - 07:22 AM

Thanks Jim. I do follow a standard indention style for submitting my code to faculty, this was just a rough sketch so :oops: . I did not know about fread sorry. Will change it instantly and also I would avoid using fflush too. Thanks for helping. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,529
  • Joined: 05-May 12

Re: File Read Operation

Posted 08 June 2012 - 07:23 AM

And while you are fixing those things, lines 74 and 95 leave the file open.
Was This Post Helpful? 1
  • +
  • -

#7 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: File Read Operation

Posted 08 June 2012 - 07:28 AM

View PostSkydiver, on 08 June 2012 - 07:23 AM, said:

And while you are fixing those things, lines 74 and 95 leave the file open.



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

Page 1 of 1