Thread: 2 questions surrounding an I/O file

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    2 questions surrounding an I/O file

    I would like to write all the data types as a whole to the disk file. Iv'e heard something about "fillers". Could someone tell me a little bit what a filler is and how to use it?

    And how can I make it so when i open the disk file, or text file, it doesn't display the record number?

    Code:
    void write_records(void);
    void read_records(void);
    
    void main()
    {
       int reply = 0;
       int items = 3;
    
       system("cls");
    
       printf("                          Welcome to my program!");
      
    
       printf("\n\nThis program accepts four different data types and writes them");
       printf(" to a disk file\nwhich can then be read in text format");
       while (reply < items)
       {
          printf("\n\nPlease select from the following menu: ");
          printf("\n%d. This function writes records to a disk file", 1);
          printf("\n%d. This function reads records to a disk file", 2);
          printf("\n%d. ( or %d, ..., etc. ) EXITS the program.", items, (items+1));
          printf ("\n");
          printf("\nTo exit choose option %d, or higher ");
          printf("\nOption number: ", items);
          scanf("%d", &reply);
          printf("\nYou selected option number %d", reply);
    
          if(reply == 1)
          {
             printf("\nThis function writes (a) record/s to a disk file");
             write_records();
          }
    
          if (reply == 2)
          {
             printf("\nThis function reads (a) record/s from a disk file");
             read_records();
          }
    
          if (reply >= items)
          {
             printf(" which exits the program!");
          }
          if ( reply < 1)
          {
             printf("\nChoice %d out of range (retype 1, ..., %d).",reply, items);
          }
       }
    
       printf("\n\nThank you for using this program. Good-bye!");
       printf("\n");
       system("pause");
    }
    
    void write_records()
    {
       FILE *tofile;
       char  name_in[20];
       char  reply;
       char  telephone_number[6];
       int   ID_number;
       int   amount_in, rec_no=1;
       tofile = fopen ("results.txt", "w");
       do
       {
          rec_no++;
    
          printf("\nType in a 5 digit ID number(e.g. 23028): ");
          scanf("%d", &ID_number);
          printf("\Type in your name(e.g. Jan): ");
          scanf("%s", &name_in);
          printf("Type in a 6 figure telephone number(e.g. 939808): ");
          scanf("%s", &telephone_number);
          printf("\Type in a 6 digit amount(e.g 123456): ");
          scanf("%d", &amount_in);
          fprintf(tofile, "\nRecord %d: ID no. = %d, Name = %s, Telephone no. = %s, amount = %d",
          rec_no, ID_number, name_in, telephone_number, amount_in );
          printf("Would you like to input another record (y/n): ");
          reply = getche();
          if ((reply != 'n') && (reply != 'N'))
          {
             printf("\nTo quit entering inputs you need to reply N or n -");
             printf(" your reply was %c", reply);
          }
       }
       while ((reply == 'y') || (reply == 'Y'));
       fclose(tofile);
    }
    
    void read_records()
    {
       FILE *fromfile;
       char content_in[60];
       int rec_no = 0;
       fromfile = fopen ("results.txt", "r");
       while (!feof(fromfile))
       {
          rec_no++;
          //fscan (fromfile, "%s", content_in); //This reads up to a next space
          fgets (content_in, 60, fromfile);
          printf ("\nThese are the records in the file", content_in);
          if (rec_no >= LinesPerScreen)
          {
             printf ("\n");
             system("pause");
             system("cls");
          }
       }
       fclose(fromfile);
    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Please, can somebody give me some tips on this one??

  3. #3

    Post

    Greetings,

    One thing I did see that may have a problem is "scanf();". When dealing with strings, you don't need to send the memory address. Even better, fgets() is more likely used in that situation. For example:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    	int id;
    	char *trail;		// ptr to '\n' if found in name
    	char name[20];
    
    	// Get number
    	scanf("%i", &id);	
    //	scanf("%s", name);	// not &name; no need to send address pointer
    
    	// Better for string handling, and avoids buffer overflow
    	fgets(name, 19, stdin);	// max of 20 including '\0'
    
    	// Always good to remove trailing '\n'
    	if (trail = strrchr(name, '\n')) // trail points to '\n' if found
    		*trail = '\0';	// end string at this point
    
    	// Print our new data
    	printf("%i) %s\n", id, name);
    
    	return 0;
    }
    You can always learn more about fgets() here, and scanf() here.


    I hope this helps. I didn't look at your code throughly, though I will try to a bit later.


    - Stack Overflow
    Last edited by Stack Overflow; 08-30-2004 at 11:29 PM. Reason: Fixed up code a bit.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM