Page 1 of 2 12 LastLast
Results 1 to 10 of 15
  1. #1

    Default Help on C File Handling


    I have trouble with a certain C program.
    The objective is to use a SINGLE(1) file only.
    The program should do like this:

    The program asks the user to input student ID,name, and peso contribution (individually and randomly). Then, the program saves this to "student.dat" file. At another option of the program, the user can retrieve per record of the student inputting the ID and getting the name and peso contribution. And the in the third part, the program generates the total peso contribution of ALL students from the "student.dat" file.

    Does anyone know how to make this? This is going to be written using Turbo C only. Please help.

  2. #2

    Default Re: Help on C File Handling

    Post your C program here.
    I could help you debug and see what is the trouble.

    I presume this is a student project and should not let others make it for you.

    For a start, read these:
    http://www.osix.net/modules/article/?id=751
    http://www.cprogramming.com/tutorial/cfileio.html

  3. #3

    Default Re: Help on C File Handling

    Hehehe... No... this is not a student project. My project is larger and more complicated than this. The file handling is only a part of it. I just don't get the whole "file handling" idea.

    This is a sample program from a book that I modified a bit to get the TOTAL of "owes"/peso. Getting the TOTAL is more important for me than accessing the individual records of each person.

    #include <stdio.h>
    #define INDEX 5

    struct entry{
    char name[20];
    float owes;
    }my_data[INDEX];

    main()
    {
    char letra;
    int x;
    for(x=0;x<INDEX;x++)
    {
    my_data[x].owes=0;
    }
    do
    {
    clrscr();
    printf("\nW-rite R-ead Q-uit");
    letra=getch();
    switch(toupper(letra))
    {
    case('W'):
    writer();
    break;
    case('R'):
    reader();
    break;
    case('Q'):
    break;
    }
    }while((toupper(letra))!='Q');
    }

    writer()
    {
    int x;
    float owe;
    char st[20];
    FILE *file_pointer;
    struct entry my_data[INDEX];



    printf("\nEnter no. ");
    scanf("%d",&x);
    printf("\nEnter name: ");
    scanf("%s",&st);
    strcpy(my_data[x-1].name,st);
    printf("\nOwes? ");
    scanf("%f",&owe);
    my_data[x-1].owes=owe;
    printf("\nWRITER");
    if((file_pointer=fopen("writer.dat","a"))!=NULL)
    fwrite(my_data,sizeof(struct entry),1,file_pointer);
    else printf("Error writing writer.dat\n");
    fclose(file_pointer);
    getch();
    }

    reader()
    {
    FILE *file_pointer;
    struct entry buffer;
    int rec_num,x;
    float total;

    total=0;
    if((file_pointer=fopen("writer.dat","r"))!=NULL)
    {
    printf("\nTotal owes: ");
    fseek(file_pointer,(long)(rec_num*sizeof(struct)), 0);
    fread(&buffer,sizeof(struct entry),INDEX,file_pointer);
    }
    else printf("Error reading writer.dat\n");

    printf("\nRecord 1 %3.2f",my_data[1].owes);
    printf("\nRecord 2 %3.2f",my_data[2].owes);
    printf("\nRecord 3 %3.2f",my_data[3].owes);

    for(x=0;x<INDEX;x++)
    {
    printf("\n%d- %3.2f",x+1,my_data[x].owes);
    total=total+my_data[x].owes;
    }
    printf("\nTotal owes on record %0.2f",total);
    fclose(file_pointer);
    getch();
    return(0);
    }

  4. #4

    Default Re: Help on C File Handling

    the filtered words are "my_data[ x ]".owes

  5. #5

    Default Re: Help on C File Handling

    If this is not a student project, I suggest you should not use direct C file handling data. It is not indexed and retrieval is slow if you have thousands of data lines.

    Try using SQLite. It is embeddable in C.
    It can be downloaded here http://www.sqlite.org/ .

    But I will help you with your code.
    To post your code properly use the [code ] bbcode.
    After a quick look, I don't see any errors here, unless not all 5 (INDEX) records are filled and only 5 records will be totalled.

  6. #6

    Default Re: Help on C File Handling

    I modified the program to make it simpler for demonstration.
    The problem with this is that the program doesn't read properly what has been written. I think I am wrong in using my fread code.

    Code:
    #include <stdio.h>
    #define INDEX 5
    
    struct entry{
    	char name[20];
    	float owes;
    	}my_data[INDEX];
    
    main()
    {
    char letra;
    int x;
    for(x=0;x<INDEX;x++)
    	{
    	my_data[x].owes=0;
    	}
    do
    {
    clrscr();
    printf("\nW-rite  R-ead  Q-uit");
    letra=getch();
    switch(toupper(letra))
    	{
    	case('W'):
    		writer();
    		break;
    	case('R'):
    		reader();
    		break;
    	case('Q'):
    		break;
    	}
    }while((toupper(letra))!='Q');
    }
    
    writer()
    {
    int x;
    float owe;
    char st[20];
    	FILE *file_pointer;
    
    	printf("\nEnter no. ");
    	scanf("%d",&x);
    	printf("\nEnter name: ");
    	scanf("%s",&st);
    	strcpy(my_data[x-1].name,st);
    	printf("\nOwes? ");
    	scanf("%f",&owe);
    	my_data[x-1].owes=owe;
    	printf("\nWRITER");
    	if((file_pointer=fopen("writer.dat","a"))!=NULL)
    		fwrite(my_data,sizeof(struct entry),1,file_pointer);
    	else printf("Error writing writer.dat\n");
    	fclose(file_pointer);
    	getch();
    }
    
    reader()
    {
    	FILE *file_pointer;
    	struct entry buffer[INDEX];
    	int rec_num,x;
    	if((file_pointer=fopen("writer.dat","r"))==NULL)
    		{
    		printf("Error reading writer.dat\n");
    		}
    	else
    		{
    		printf("\nTotal owes: ");
    		fread(&buffer,sizeof(struct entry),INDEX,file_pointer);
    		for(x=0;x<INDEX;x++)
    			{
    			printf("\nRecord %d %3.2f",x,buffer[x].owes);
    			}
    		fclose(file_pointer);
    		}
    	getch();
    	return(0);
    }

  7. #7

    Default Re: Help on C File Handling

    The code only reads the FIRST 5 entries.

    Subsequent writes are not read because the FOPEN type is "a" (append).
    All new writes will be at the end of the file.
    What you could do is use "w" or move the record pointer to the beginning using fseek. But using "w" will erase the file first.

    Or before the read move the record pointer to the last 5 records written.

  8. #8

    Default Re: Help on C File Handling

    Here's the new program.
    I used rewind. However, my_data [ x ].owes displays 0 for all the 5 times during the reading part. It doesn't show the values I inputted for my_data [ x ].owes during the writing.

    Code:
    #include <stdio.h>
    #define INDEX 5
    
    struct entry{
    	char name[20];
    	float owes;
    	}my_data[INDEX];
    
    main()
    {
    char letra;
    int x;
    for(x=0;x<INDEX;x++)
    	{
    	my_data[x].owes=0;
    	}
    do
    {
    clrscr();
    printf("\nW-rite  R-ead  Q-uit");
    letra=getch();
    switch(toupper(letra))
    	{
    	case('W'):
    		writer();
    		break;
    	case('R'):
    		reader();
    		break;
    	case('Q'):
    		break;
    	}
    }while((toupper(letra))!='Q');
    }
    
    writer()
    {
    int x;
    float owe;
    char st[20];
    	FILE *file_pointer;
    	if((file_pointer=fopen("writer.dat","a"))!=NULL)
    		{
    		printf("\nEnter no. ");
    		scanf("%d",&x);
    		printf("\nEnter name: ");
    		scanf("%s",&st);
    		strcpy(my_data[x-1].name,st);
    		printf("\nOwes? ");
    		scanf("%f",&owe);
    		my_data[x-1].owes=owe;
    		printf("\nWRITER");
    		fwrite(my_data,sizeof(struct entry),1,file_pointer);
    		}
    	else printf("Error writing writer.dat\n");
    	fclose(file_pointer);
    	getch();
    }
    
    reader()
    {
    	FILE *file_pointer;
    	struct entry buffer[INDEX];
    	int x;
    	if((file_pointer=fopen("writer.dat","r"))==NULL)
    		{
    		printf("Error reading writer.dat\n");
    		}
    	else
    		{
    		printf("\nOwes: ");
    		rewind(file_pointer);
    		fread(my_data,sizeof(struct entry),INDEX,file_pointer);
    		for(x=0;x<INDEX;x++)
    			{
    			printf("\nRecord %d %3.2f",x,my_data[x].owes);
    			}
    		fclose(file_pointer);
    		}
    	getch();
    	return(0);
    }

  9. #9

    Default Re: Help on C File Handling

    Even with rewind, this still will only read the FIRST 5 records.
    What you could do is use fseek and move the record pointer to the end minus 5 records.
    Code:
    //untested
    
    {
    		printf("\nOwes: ");
    		/* rewind(file_pointer); */
    
            /* move pointer from the end. -5 records, may not work if records written is less than 5 */
            fseek(file_pointer, -5 * sizeof(struct_entry), SEEK_END);  
    
    		fread(my_data,sizeof(struct entry),INDEX,file_pointer);
    		for(x=0;x<INDEX;x++)
    			{
    			printf("\nRecord %d %3.2f",x,my_data[x].owes);
    			}
    		fclose(file_pointer);
    		}

  10. #10

    Default Re: Help on C File Handling

    I tried the code.

    I input some values in write. When I went to read... the values were shown. -OK!
    I quit the program. Then I run it again. I went to read the see the values. They aren't there! Aren't my values supposed to be saved?

  11.    Advertisement

Page 1 of 2 12 LastLast

Similar Threads

 
  1. Need help on compressing video file size
    By warblood360 in forum Apple Devices
    Replies: 1
    Last Post: 07-05-2013, 01:17 AM
  2. pls help on encrypted back up files
    By gabs in forum Computer Hardware
    Replies: 31
    Last Post: 08-07-2010, 01:31 PM
  3. Need help on old WordStar files
    By archnacorda in forum Computer Hardware
    Replies: 0
    Last Post: 07-31-2010, 11:00 AM
  4. help on videocams
    By Bao in forum Gizmos & Gadgets (Old)
    Replies: 27
    Last Post: 03-29-2008, 03:56 PM
  5. Guys need help on Mac OS X and Win XP using Flash drive file Transfer
    By ryan_boy22 in forum Software & Games (Old)
    Replies: 11
    Last Post: 12-26-2007, 02:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top